API
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CityController.cs 1.8KB

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