You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

56 lines
1.6 KiB

  1. using Domain.BaseData;
  2. using Infrastructure;
  3. using Microsoft.AspNetCore.Authorization;
  4. using Microsoft.AspNetCore.Mvc;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using Models.BaseData;
  7. using Services.BaseData;
  8. using System;
  9. using System.Collections.Generic;
  10. using System.Linq;
  11. namespace Api.Controllers
  12. {
  13. [Authorize(Roles = Consts.Developer)]
  14. public class PlantsController : BaseController
  15. {
  16. private readonly IPlantsService plantsService;
  17. public PlantsController(IServiceProvider serviceProvider) : base(serviceProvider)
  18. {
  19. plantsService = serviceProvider.GetService<IPlantsService>();
  20. }
  21. [HttpGet]
  22. public IActionResult List()
  23. {
  24. var dbList = plantsService.GetQueryable().ToList();
  25. return Ok(Mapper.Map<List<PlantViewModel>>(dbList));
  26. }
  27. [HttpPost]
  28. public IActionResult Add(SavePlantViewModel model)
  29. {
  30. var dbModel = Mapper.Map<Plant>(model);
  31. var imageName=SaveImageFromBase64(model.Base64Icon);
  32. dbModel.ImageFileName = imageName;
  33. plantsService.Add(dbModel);
  34. return Ok();
  35. }
  36. [HttpPut]
  37. public IActionResult Update(PlantViewModel model)
  38. {
  39. var dbModel = plantsService.GetById(model.Id.Value);
  40. Mapper.Map(model, dbModel);
  41. plantsService.Update(dbModel);
  42. return Ok();
  43. }
  44. [HttpDelete("{id}")]
  45. public IActionResult Delete(Guid id)
  46. {
  47. plantsService.Delete(id);
  48. return Ok();
  49. }
  50. }
  51. }