API
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

CarouselController.cs 1.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Transactions;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using UnivateProperties_API.Model.Misc;
  9. using UnivateProperties_API.Repository;
  10. namespace UnivateProperties_API.Controllers.Misc
  11. {
  12. [Route("api/[controller]")]
  13. [ApiController]
  14. public class CarouselController : ControllerBase
  15. {
  16. private readonly IRepository<Carousel> _Repo;
  17. public CarouselController(IRepository<Carousel> repo)
  18. {
  19. _Repo = repo;
  20. }
  21. [HttpGet]
  22. public IActionResult Get()
  23. {
  24. return new OkObjectResult(_Repo.GetAll());
  25. }
  26. [HttpGet("{id}")]
  27. public IActionResult Get(int id)
  28. {
  29. return new OkObjectResult(_Repo.GetDetailed(x => x.Id == id));
  30. }
  31. [HttpPost]
  32. public IActionResult Post([FromBody] Carousel carousel)
  33. {
  34. using (var scope = new TransactionScope())
  35. {
  36. _Repo.Insert(carousel);
  37. scope.Complete();
  38. return CreatedAtAction(nameof(Get), new { id = carousel.Id }, carousel);
  39. }
  40. }
  41. [HttpPut]
  42. public IActionResult Put([FromBody] Carousel carousel)
  43. {
  44. if (carousel != null)
  45. {
  46. using (var scope = new TransactionScope())
  47. {
  48. _Repo.Update(carousel);
  49. scope.Complete();
  50. return new OkResult();
  51. }
  52. }
  53. return new NoContentResult();
  54. }
  55. [HttpDelete("{id}")]
  56. public IActionResult Delete(int id)
  57. {
  58. _Repo.RemoveAtId(id);
  59. return new OkResult();
  60. }
  61. }
  62. }