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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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("{type}/{by}")]
  42. public IActionResult SearchBy(string type, int by)
  43. {
  44. return new OkObjectResult(_Repo.GetPropertyList(type, by));
  45. }
  46. [HttpGet("MayEditProperty/{id}")]
  47. public IActionResult MayEditProperty(int id)
  48. {
  49. return new OkObjectResult(_Repo.MayEdit(id));
  50. }
  51. #endregion
  52. [HttpGet("search")]
  53. public IActionResult Search([FromBody] PropertySearch search)
  54. {
  55. return new OkObjectResult(_Repo.GetDisplay(search));
  56. }
  57. //Will need to come out. Post search not working......:(
  58. [HttpGet("search/{userName}/{keyword}/{salesType}/{propertyUsageType}/{propertyType}/{province}/{city}/{suburb}/{minPrice}/{maxPrice}/{availableFrom}/{propertyId}")]
  59. 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)
  60. {
  61. var search = new PropertySearch()
  62. {
  63. UserName = userName,
  64. Keyword = keyword,
  65. SalesType = salesType,
  66. PropertyUsageType = propertyUsageType,
  67. PropertyType = propertyType,
  68. Province = province,
  69. City = city,
  70. Suburb = suburb,
  71. MinPrice = minPrice,
  72. MaxPrice = maxPrice,
  73. PropertyId = propertyId
  74. };
  75. if (availableFrom == "undefined")
  76. search.AvailableFrom = DateTime.MinValue;
  77. else
  78. search.AvailableFrom = DateTime.Parse(availableFrom);
  79. return new OkObjectResult(_Repo.GetDisplay(search));
  80. }
  81. [HttpPost]
  82. public IActionResult Post([FromBody] PropertyContainer property)
  83. {
  84. using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
  85. {
  86. _Repo.Insert(property);
  87. scope.Complete();
  88. return CreatedAtAction(nameof(Get), new { id = property.Id }, property);
  89. }
  90. }
  91. [HttpPut]
  92. public IActionResult Put([FromBody] PropertyContainer property)
  93. {
  94. if (property != null)
  95. {
  96. using (var scope = new TransactionScope(TransactionScopeOption.Suppress))
  97. {
  98. _Repo.Update(property);
  99. scope.Complete();
  100. return new OkResult();
  101. }
  102. }
  103. return new NoContentResult();
  104. }
  105. [HttpDelete("{id}")]
  106. public IActionResult Delete(int id)
  107. {
  108. _Repo.RemoveAtId(id);
  109. return new OkResult();
  110. }
  111. }
  112. }