API
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.

AgentController.cs 2.0KB

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