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.

PropertyController.cs 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Transactions;
  6. using UnivateProperties_API.Containers.Property;
  7. using UnivateProperties_API.Model.Properties;
  8. using UnivateProperties_API.Repository.Properties;
  9. namespace UnivateProperties_API.Controllers.Properties
  10. {
  11. [Route("api/[controller]")]
  12. [ApiController]
  13. public class PropertyController : ControllerBase
  14. {
  15. private readonly IPropertyRepository _Repo;
  16. public PropertyController(IPropertyRepository repo)
  17. {
  18. _Repo = repo;
  19. }
  20. #region Get Methods
  21. [HttpGet]
  22. public IActionResult Get()
  23. {
  24. return new OkObjectResult(_Repo.GetDisplay());
  25. }
  26. [HttpGet("getDetailed/{id}")]
  27. public IActionResult GetDetailed(int id)
  28. {
  29. return new OkObjectResult(_Repo.GetDetailed(id, true));
  30. }
  31. [HttpGet("getProperty/{id}")]
  32. public IActionResult GetProperty(int id)
  33. {
  34. return new OkObjectResult(_Repo.GetDetailed(id, false));
  35. }
  36. [HttpGet("latestProperties")]
  37. public IActionResult GetLatestProperties()
  38. {
  39. return new OkObjectResult(_Repo.GetLatestDisplay());
  40. }
  41. [HttpGet("GetPropertyList/{by}")]
  42. public IActionResult SearchBy(int by)
  43. {
  44. return new OkObjectResult(_Repo.GetPropertyList(by));
  45. }
  46. [HttpGet("GetLivePropertyList")]
  47. public IActionResult GetLivePropertyList()
  48. {
  49. return new OkObjectResult(_Repo.GetPropertyList());
  50. }
  51. [HttpGet("MayEditProperty/{id}")]
  52. public IActionResult MayEditProperty(int id)
  53. {
  54. return new OkObjectResult(_Repo.MayEdit(id));
  55. }
  56. #endregion
  57. [HttpGet("search")]
  58. public IActionResult Search([FromBody] PropertySearch search)
  59. {
  60. return new OkObjectResult(_Repo.GetDisplay(search));
  61. }
  62. //Will need to come out. Post search not working......:(
  63. [HttpGet("search/{userName}/{keyword}/{salesType}/{propertyUsageType}/{propertyType}/{province}/{city}/{suburb}/{minPrice}/{maxPrice}/{availableFrom}/{propertyId}")]
  64. public IActionResult Search(string userName, string keyword, string salesType, string propertyUsageType, string propertyType, string province, string city, string suburb, decimal minPrice, decimal maxPrice, string availableFrom, int propertyId)
  65. {
  66. var search = new PropertySearch()
  67. {
  68. UserName = userName,
  69. Keyword = keyword,
  70. SalesType = salesType,
  71. PropertyUsageType = propertyUsageType,
  72. PropertyType = propertyType,
  73. Province = province,
  74. City = city,
  75. Suburb = suburb,
  76. MinPrice = minPrice,
  77. MaxPrice = maxPrice,
  78. PropertyId = propertyId
  79. };
  80. if (availableFrom == "undefined")
  81. search.AvailableFrom = DateTime.MinValue;
  82. else
  83. search.AvailableFrom = DateTime.Parse(availableFrom);
  84. return new OkObjectResult(_Repo.GetDisplay(search));
  85. }
  86. [HttpPost]
  87. public IActionResult Post([FromBody] PropertyContainer property)
  88. {
  89. using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
  90. {
  91. _Repo.Insert(property);
  92. scope.Complete();
  93. return CreatedAtAction(nameof(Get), new { id = property.Id }, property);
  94. }
  95. }
  96. [HttpPut]
  97. public IActionResult Put([FromBody] PropertyContainer property)
  98. {
  99. if (property != null)
  100. {
  101. using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
  102. {
  103. _Repo.Update(property);
  104. scope.Complete();
  105. return new OkResult();
  106. }
  107. }
  108. return new NoContentResult();
  109. }
  110. [HttpPut("PublishProperty")]
  111. public IActionResult PublishProperty([FromBody] PropertyDisplay property)
  112. {
  113. _Repo.PublishProperty(property.Id);
  114. return new NoContentResult();
  115. }
  116. [HttpPut("UnpublishProperty")]
  117. public IActionResult UnpublishProperty(PropertyDisplay property)
  118. {
  119. _Repo.UnpublishProperty(property.Id);
  120. return new NoContentResult();
  121. }
  122. [HttpDelete("{id}")]
  123. public IActionResult Delete(int id)
  124. {
  125. _Repo.RemoveAtId(id);
  126. return new OkResult();
  127. }
  128. }
  129. }