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.

UserRoleController.cs 1.7KB

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