API
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

AgentController.cs 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. byte[] passwordHash, passwordSalt;
  35. MyCommon.CreatePasswordHash(agentDto.Password, out passwordHash, out passwordSalt);
  36. agent.User.PasswordHash = passwordHash;
  37. agent.User.PasswordSalt = passwordSalt;
  38. _Repo.Insert(agent);
  39. scope.Complete();
  40. return CreatedAtAction(nameof(Get), new { id = agentDto.Agent.Id }, agentDto.Agent);
  41. }
  42. }
  43. [HttpPut()]
  44. public IActionResult Put([FromBody] Agent agent)
  45. {
  46. if (agent != null)
  47. {
  48. using (var scope = new TransactionScope())
  49. {
  50. _Repo.Update(agent);
  51. scope.Complete();
  52. return new OkResult();
  53. }
  54. }
  55. return new NoContentResult();
  56. }
  57. [HttpDelete("{id}")]
  58. public IActionResult Delete(int id)
  59. {
  60. _Repo.RemoveAtId(id);
  61. return new OkResult();
  62. }
  63. }
  64. }