API
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

AgencyController.cs 1.7KB

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