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.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. //Updates to DB
  30. [HttpPut()]
  31. public IActionResult Put([FromBody] Individual individual)
  32. {
  33. if (individual != null)
  34. {
  35. using (var scope = new TransactionScope())
  36. {
  37. _Repo.Update(individual);
  38. scope.Complete();
  39. return new OkResult();
  40. }
  41. }
  42. return new NoContentResult();
  43. }
  44. //Updates DB by removing said Id
  45. [HttpDelete("{id}")]
  46. public IActionResult Delete(int id)
  47. {
  48. _Repo.RemoveAtId(id);
  49. return new OkResult();
  50. }
  51. }
  52. }