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.

UserController.cs 1.7KB

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