API
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

SuburbController.cs 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 SuburbController : ControllerBase
  10. {
  11. private readonly ISuburbRepository _Repo;
  12. public SuburbController(ISuburbRepository 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("{province}/{city}")]
  27. public IActionResult Get(string Province, string City)
  28. {
  29. return new OkObjectResult(_Repo.GetBy(Province, City));
  30. }
  31. [HttpPost]
  32. public IActionResult Post([FromBody] Suburb suburb)
  33. {
  34. using (var scope = new TransactionScope())
  35. {
  36. _Repo.Insert(suburb);
  37. scope.Complete();
  38. return CreatedAtAction(nameof(Get), new { id = suburb.Id }, suburb);
  39. }
  40. }
  41. [HttpPut]
  42. public IActionResult Put([FromBody] Suburb suburb)
  43. {
  44. if (suburb != null)
  45. {
  46. using (var scope = new TransactionScope())
  47. {
  48. _Repo.Update(suburb);
  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. }