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

PropertyController.cs 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Microsoft.AspNetCore.Mvc;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Transactions;
  5. using UnivateProperties_API.Model.Property;
  6. using UnivateProperties_API.Repository.Properties;
  7. namespace UnivateProperties_API.Controllers.Properties
  8. {
  9. [Route("Property/[controller]")]
  10. [ApiController]
  11. public class PropertyController : ControllerBase
  12. {
  13. private readonly IPropertyRepository _Repo;
  14. public PropertyController(IPropertyRepository repo)
  15. {
  16. _Repo = repo;
  17. }
  18. [HttpGet]
  19. public IActionResult Get()
  20. {
  21. return new OkObjectResult(_Repo.GetDisplay());
  22. }
  23. [HttpGet("{id}")]
  24. public IActionResult Get(int id)
  25. {
  26. return new OkObjectResult(_Repo.GetDetailed(x => x.Id == id));
  27. }
  28. [HttpGet("search/{type}/{keyword}")]
  29. public IActionResult Get(string type, string Keyword)
  30. {
  31. return new OkObjectResult(_Repo.GetDisplay(Keyword));
  32. }
  33. [HttpGet("search/{type}/{propertytype}/{province}/{city}/{suburb}/{proptype}")]
  34. public IActionResult Get(string type, string propertyType, string province, string city, string suburb, string propType)
  35. {
  36. return new OkObjectResult(_Repo.GetDisplay(type, propertyType, province, city, suburb, propType));
  37. }
  38. [HttpGet("{type}/{by}")]
  39. public IActionResult SearchBy(string type, string by)
  40. {
  41. PropertyUsageType pType = PropertyUsageType.Both;
  42. switch (type.ToUpper())
  43. {
  44. case "RESIDENTIAL":
  45. pType = PropertyUsageType.Residential;
  46. break;
  47. case "COMMERCIAL":
  48. pType = PropertyUsageType.Commercial;
  49. break;
  50. }
  51. if (pType != PropertyUsageType.Both)
  52. {
  53. List<int> proptypeIds = (from pt in _Repo.GetPropertyTypes(p => p.UsageType == pType)
  54. select pt.Id).ToList();
  55. if (string.IsNullOrEmpty(by) || by.ToUpper() == "ALL")
  56. {
  57. return new OkObjectResult(_Repo.Get(x => proptypeIds.Contains(x.PropertyTypeId)));
  58. }
  59. else
  60. {
  61. return new OkObjectResult(_Repo.Get(x => proptypeIds.Contains(x.PropertyTypeId) && x.CreatedBy == by));
  62. }
  63. }
  64. else
  65. return new NoContentResult();
  66. }
  67. [HttpPost]
  68. public IActionResult Post([FromBody] Property property)
  69. {
  70. using (var scope = new TransactionScope())
  71. {
  72. _Repo.Insert(property);
  73. scope.Complete();
  74. return CreatedAtAction(nameof(Get), new { id = property.Id }, property);
  75. }
  76. }
  77. [HttpPut]
  78. public IActionResult Put([FromBody] Property property)
  79. {
  80. if (property != null)
  81. {
  82. using (var scope = new TransactionScope())
  83. {
  84. _Repo.Update(property);
  85. scope.Complete();
  86. return new OkResult();
  87. }
  88. }
  89. return new NoContentResult();
  90. }
  91. [HttpDelete("{id}")]
  92. public IActionResult Delete(int id)
  93. {
  94. _Repo.RemoveAtId(id);
  95. return new OkResult();
  96. }
  97. }
  98. }