API
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

AgentController.cs 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System.Linq;
  2. using System.Transactions;
  3. using Microsoft.AspNetCore.Mvc;
  4. using UnivateProperties_API.Containers.Users;
  5. using UnivateProperties_API.Helpers;
  6. using UnivateProperties_API.Model.Users;
  7. using UnivateProperties_API.Repository;
  8. namespace User_API.Controllers
  9. {
  10. [Route("api/[controller]")]
  11. [ApiController]
  12. public class AgentController : ControllerBase
  13. {
  14. private readonly IRepository<Agent> _Repo;
  15. public AgentController(IRepository<Agent> repo)
  16. {
  17. _Repo = repo;
  18. }
  19. [HttpGet]
  20. public IActionResult Get()
  21. {
  22. return new OkObjectResult(_Repo.GetAll());
  23. }
  24. [HttpGet("GetByAgency/{id}")]
  25. public IActionResult GetByAgency(int id)
  26. {
  27. return new OkObjectResult(_Repo.Get(x => x.AgencyId == id).ToList());
  28. }
  29. [HttpGet("{id}")]
  30. public IActionResult Get(int id)
  31. {
  32. return new OkObjectResult(_Repo.Get(x => x.Id == id));
  33. }
  34. [HttpPost()]
  35. public IActionResult Post([FromBody] AgentDto agentDto)
  36. {
  37. using (var scope = new TransactionScope())
  38. {
  39. Agent agent = agentDto.Agent;
  40. MyCommon.CreatePasswordHash(agentDto.Password, out byte[] passwordHash, out byte[] passwordSalt);
  41. agent.User.PasswordHash = passwordHash;
  42. agent.User.PasswordSalt = passwordSalt;
  43. _Repo.Insert(agent);
  44. scope.Complete();
  45. return CreatedAtAction(nameof(Get), new { id = agentDto.Agent.Id }, agentDto.Agent);
  46. }
  47. }
  48. [HttpPut()]
  49. public IActionResult Put([FromBody] Agent agent)
  50. {
  51. if (agent != null)
  52. {
  53. using (var scope = new TransactionScope())
  54. {
  55. _Repo.Update(agent);
  56. scope.Complete();
  57. return new OkResult();
  58. }
  59. }
  60. return new NoContentResult();
  61. }
  62. [HttpDelete("{id}")]
  63. public IActionResult Delete(int id)
  64. {
  65. _Repo.RemoveAtId(id);
  66. return new OkResult();
  67. }
  68. }
  69. }