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.
 
 
 
 

62 rivejä
1.8 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. [HttpGet("{id}")]
  28. public IActionResult Get(Guid id)
  29. {
  30. var pl = plantsService.GetById(id);
  31. return Ok(Mapper.Map<SavePlantViewModel>(pl));
  32. }
  33. [HttpPost]
  34. public IActionResult Add(SavePlantViewModel model)
  35. {
  36. var dbModel = Mapper.Map<Plant>(model);
  37. var imageName=SaveImageFromBase64(model.Base64Icon);
  38. dbModel.ImageFileName = imageName;
  39. plantsService.Add(dbModel);
  40. return Ok();
  41. }
  42. [HttpPut]
  43. public IActionResult Update(PlantViewModel model)
  44. {
  45. var dbModel = plantsService.GetById(model.Id.Value);
  46. Mapper.Map(model, dbModel);
  47. plantsService.Update(dbModel);
  48. return Ok();
  49. }
  50. [HttpDelete("{id}")]
  51. public IActionResult Delete(Guid id)
  52. {
  53. plantsService.Delete(id);
  54. return Ok();
  55. }
  56. }
  57. }