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

AgentController.cs 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. [HttpGet("single/{userId}")]
  35. public IActionResult GetByUserId(int userId)
  36. {
  37. return new OkObjectResult(_Repo.Get(x => x.UserId == userId));
  38. }
  39. [HttpPost()]
  40. public IActionResult Post([FromBody] AgentDto agentDto)
  41. {
  42. using (var scope = new TransactionScope())
  43. {
  44. Agent agent = agentDto.Agent;
  45. MyCommon.CreatePasswordHash(agentDto.Password, out byte[] passwordHash, out byte[] passwordSalt);
  46. agent.User.PasswordHash = passwordHash;
  47. agent.User.PasswordSalt = passwordSalt;
  48. _Repo.Insert(agent);
  49. scope.Complete();
  50. return CreatedAtAction(nameof(Get), new { id = agentDto.Agent.Id }, agentDto.Agent);
  51. }
  52. }
  53. [HttpPost("AgentFromUser")]
  54. public IActionResult Post(int id, [FromBody] Agent agent)
  55. {
  56. using (var scope = new TransactionScope())
  57. {
  58. _Repo.Insert(agent);
  59. scope.Complete();
  60. return CreatedAtAction(nameof(Get), new { id = agent.Id }, agent);
  61. }
  62. }
  63. [HttpPut()]
  64. public IActionResult Put([FromBody] Agent agent)
  65. {
  66. if (agent != null)
  67. {
  68. using (var scope = new TransactionScope())
  69. {
  70. _Repo.Update(agent);
  71. scope.Complete();
  72. return new OkResult();
  73. }
  74. }
  75. return new NoContentResult();
  76. }
  77. [HttpDelete("{id}")]
  78. public IActionResult Delete(int id)
  79. {
  80. _Repo.RemoveAtId(id);
  81. return new OkResult();
  82. }
  83. }
  84. }