API
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

IndividualController.cs 1.6KB

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