API
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

PropertyController.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #region Get Methods
  19. [HttpGet]
  20. public IActionResult Get()
  21. {
  22. return new OkObjectResult(_Repo.GetDisplay());
  23. }
  24. [HttpGet("{id}")]
  25. public IActionResult Get(int id)
  26. {
  27. return new OkObjectResult(_Repo.GetDetailed(x => x.Id == id));
  28. }
  29. [HttpGet("search/{keyword}")]
  30. public IActionResult Search(string Keyword)
  31. {
  32. return new OkObjectResult(_Repo.GetDisplay(Keyword));
  33. }
  34. [HttpGet("search/{type}/{propertytype}/{province}/{city}/{suburb}/{proptype}")]
  35. public IActionResult Get(string type, string propertyType, string province, string city, string suburb, string propType)
  36. {
  37. return new OkObjectResult(_Repo.GetDisplay(type, propertyType, province, city, suburb, propType));
  38. }
  39. [HttpGet("latestProperties")]
  40. public IActionResult GetLatestProperties()
  41. {
  42. return new OkObjectResult(_Repo.GetLatestDisplay());
  43. }
  44. [HttpGet("{type}/{by}")]
  45. public IActionResult SearchBy(string type, string by)
  46. {
  47. PropertyUsageType pType = PropertyUsageType.Both;
  48. switch (type.ToUpper())
  49. {
  50. case "RESIDENTIAL":
  51. pType = PropertyUsageType.Residential;
  52. break;
  53. case "COMMERCIAL":
  54. pType = PropertyUsageType.Commercial;
  55. break;
  56. }
  57. if (pType != PropertyUsageType.Both)
  58. {
  59. List<int> proptypeIds = (from pt in _Repo.GetPropertyTypes(p => p.UsageType == pType)
  60. select pt.Id).ToList();
  61. if (string.IsNullOrEmpty(by) || by.ToUpper() == "ALL")
  62. {
  63. return new OkObjectResult(_Repo.Get(x => proptypeIds.Contains(x.PropertyTypeId)));
  64. }
  65. else
  66. {
  67. return new OkObjectResult(_Repo.Get(x => proptypeIds.Contains(x.PropertyTypeId) && x.CreatedBy == by));
  68. }
  69. }
  70. else
  71. return new NoContentResult();
  72. }
  73. [HttpGet("GetEditDisplay/{id}")]
  74. public IActionResult GetEditDisplay(int id)
  75. {
  76. return new OkObjectResult(_Repo.GetEditDisplay(x => x.Id == id));
  77. }
  78. #endregion
  79. [HttpPost]
  80. public IActionResult Post([FromBody] Property property)
  81. {
  82. using (var scope = new TransactionScope())
  83. {
  84. _Repo.Insert(property);
  85. scope.Complete();
  86. return CreatedAtAction(nameof(Get), new { id = property.Id }, property);
  87. }
  88. }
  89. [HttpPut]
  90. public IActionResult Put([FromBody] Property property)
  91. {
  92. if (property != null)
  93. {
  94. using (var scope = new TransactionScope())
  95. {
  96. _Repo.Update(property);
  97. scope.Complete();
  98. return new OkResult();
  99. }
  100. }
  101. return new NoContentResult();
  102. }
  103. [HttpDelete("{id}")]
  104. public IActionResult Delete(int id)
  105. {
  106. _Repo.RemoveAtId(id);
  107. return new OkResult();
  108. }
  109. }
  110. }