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

AgentController.cs 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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 AgentController : ControllerBase
  11. {
  12. private readonly IRepository<Agent> _Repo;
  13. public AgentController(IRepository<Agent> 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] Agent agent)
  29. {
  30. using (var scope = new TransactionScope())
  31. {
  32. _Repo.Insert(agent);
  33. scope.Complete();
  34. return CreatedAtAction(nameof(Get), new { id = agent.Id }, agent);
  35. }
  36. }
  37. [HttpPut()]
  38. public IActionResult Put([FromBody] Agent agent)
  39. {
  40. if (agent != null)
  41. {
  42. using (var scope = new TransactionScope())
  43. {
  44. _Repo.Update(agent);
  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. }