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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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("api/[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("GetByProperty/{id}")]
  22. public IActionResult GetByProperty(int id)
  23. {
  24. return new OkObjectResult(_Repo.GetByProperty(id));
  25. }
  26. [HttpGet("{id}")]
  27. public IActionResult Get(int id)
  28. {
  29. return new OkObjectResult(_Repo.Get(x => x.Id == id));
  30. }
  31. [HttpGet("getby/{value}")]
  32. public IActionResult Get(string value)
  33. {
  34. return new OkObjectResult(_Repo.GetBy(value));
  35. }
  36. [HttpPost]
  37. public IActionResult Post([FromBody] City city)
  38. {
  39. using (var scope = new TransactionScope())
  40. {
  41. _Repo.Insert(city);
  42. scope.Complete();
  43. return CreatedAtAction(nameof(Get), new { id = city.Id }, city);
  44. }
  45. }
  46. [HttpPut]
  47. public IActionResult Put([FromBody] City city)
  48. {
  49. if (city != null)
  50. {
  51. using (var scope = new TransactionScope())
  52. {
  53. _Repo.Update(city);
  54. scope.Complete();
  55. return new OkResult();
  56. }
  57. }
  58. return new NoContentResult();
  59. }
  60. [HttpDelete("{id}")]
  61. public IActionResult Delete(int id)
  62. {
  63. _Repo.RemoveAtId(id);
  64. return new OkResult();
  65. }
  66. }
  67. }