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.

IndividualController.cs 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. using System.Transactions;
  2. using Microsoft.AspNetCore.Mvc;
  3. using UnivateProperties_API.Model.Users;
  4. using UnivateProperties_API.Repository;
  5. using UnivateProperties_API.Repository.Users;
  6. namespace User_API.Controllers
  7. {
  8. [Route("api/[controller]")]
  9. [ApiController]
  10. public class IndividualController : ControllerBase
  11. {
  12. private readonly IRepository<Individual> _Repo;
  13. public IndividualController(IRepository<Individual> repo)
  14. {
  15. _Repo = repo;
  16. }
  17. //Gets data from the DB
  18. [HttpGet]
  19. public IActionResult Get()
  20. {
  21. return new OkObjectResult(_Repo.GetAll());
  22. }
  23. //Gets data from DB by Id
  24. [HttpGet("{id}")]
  25. public IActionResult Get(int id)
  26. {
  27. return new OkObjectResult(_Repo.Get(x => x.Id == id));
  28. }
  29. [HttpGet("getIndividual/{id}")]
  30. public IActionResult GetIndividual(int id)
  31. {
  32. return new OkObjectResult((_Repo as IndividualRepository).GetIndividual(id));
  33. }
  34. [HttpGet("getAllIndividuals")]
  35. public IActionResult GetAllIndividuals()
  36. {
  37. return new OkObjectResult((_Repo as IndividualRepository).GetAllIndividuals());
  38. }
  39. //Updates to DB
  40. [HttpPut()]
  41. public IActionResult Put([FromBody] Individual individual)
  42. {
  43. if (individual != null)
  44. {
  45. using (var scope = new TransactionScope())
  46. {
  47. _Repo.Update(individual);
  48. scope.Complete();
  49. return new OkResult();
  50. }
  51. }
  52. return new NoContentResult();
  53. }
  54. //Updates DB by removing said Id
  55. [HttpDelete("{id}")]
  56. public IActionResult Delete(int id)
  57. {
  58. _Repo.RemoveAtId(id);
  59. return new OkResult();
  60. }
  61. }
  62. }