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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. using Microsoft.AspNetCore.Mvc;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Transactions;
  5. using UnivateProperties_API.Containers.Property;
  6. using UnivateProperties_API.Model.Properties;
  7. using UnivateProperties_API.Repository.Properties;
  8. namespace UnivateProperties_API.Controllers.Properties
  9. {
  10. [Route("api/[controller]")]
  11. [ApiController]
  12. public class PropertyController : ControllerBase
  13. {
  14. private readonly IPropertyRepository _Repo;
  15. public PropertyController(IPropertyRepository repo)
  16. {
  17. _Repo = repo;
  18. }
  19. #region Get Methods
  20. [HttpGet]
  21. public IActionResult Get()
  22. {
  23. return new OkObjectResult(_Repo.GetDisplay());
  24. }
  25. [HttpGet("{id}")]
  26. public IActionResult Get(int id)
  27. {
  28. return new OkObjectResult(_Repo.GetDetailed(x => x.Id == id));
  29. }
  30. [HttpGet("latestProperties")]
  31. public IActionResult GetLatestProperties()
  32. {
  33. return new OkObjectResult(_Repo.GetLatestDisplay());
  34. }
  35. [HttpGet("{type}/{by}")]
  36. public IActionResult SearchBy(string type, string by)
  37. {
  38. PropertyUsageType pType = PropertyUsageType.Both;
  39. switch (type.ToUpper())
  40. {
  41. case "RESIDENTIAL":
  42. pType = PropertyUsageType.Residential;
  43. break;
  44. case "COMMERCIAL":
  45. pType = PropertyUsageType.Commercial;
  46. break;
  47. }
  48. if (pType != PropertyUsageType.Both)
  49. {
  50. List<int> proptypeIds = (from pt in _Repo.GetPropertyTypes(p => p.UsageType == pType)
  51. select pt.Id).ToList();
  52. if (string.IsNullOrEmpty(by) || by.ToUpper() == "ALL")
  53. {
  54. return new OkObjectResult(_Repo.GetPropertyList(x => proptypeIds.Contains(x.PropertyTypeId)));
  55. }
  56. else
  57. {
  58. //Needs to change to search on individule/Agent
  59. return new OkObjectResult(_Repo.GetPropertyList(x => proptypeIds.Contains(x.PropertyTypeId)));
  60. }
  61. }
  62. else
  63. return new NoContentResult();
  64. }
  65. [HttpGet("GetEditDisplay/{id}")]
  66. public IActionResult GetEditDisplay(int id)
  67. {
  68. return new OkObjectResult(_Repo.GetPropertyList(x => x.Id == id));
  69. }
  70. #endregion
  71. [HttpPost("search")]
  72. public IActionResult Search([FromBody] PropertySearch search)
  73. {
  74. return new OkObjectResult(_Repo.GetDisplay(search));
  75. }
  76. //Will need to come out. Post search not working......:(
  77. [HttpGet("search/{userName}/{keyword}/{salesType}/{propertyUsageType}/{propertyType}/{province}/{city}/{suburb}")]
  78. public IActionResult Search(string userName, string keyword, string salesType, string propertyUsageType, string propertyType, string province, string city, string suburb)
  79. {
  80. var search = new PropertySearch()
  81. {
  82. UserName = userName,
  83. Keyword = keyword,
  84. SalesType = salesType,
  85. PropertyUsageType = propertyUsageType,
  86. PropertyType = propertyType,
  87. Province = province,
  88. City = city,
  89. Suburb = suburb
  90. };
  91. return new OkObjectResult(_Repo.GetDisplay(search));
  92. }
  93. [HttpPost]
  94. public IActionResult Post([FromBody] Property property)
  95. {
  96. using (var scope = new TransactionScope())
  97. {
  98. _Repo.Insert(property);
  99. scope.Complete();
  100. return CreatedAtAction(nameof(Get), new { id = property.Id }, property);
  101. }
  102. }
  103. [HttpPut]
  104. public IActionResult Put([FromBody] Property property)
  105. {
  106. if (property != null)
  107. {
  108. using (var scope = new TransactionScope())
  109. {
  110. _Repo.Update(property);
  111. scope.Complete();
  112. return new OkResult();
  113. }
  114. }
  115. return new NoContentResult();
  116. }
  117. [HttpDelete("{id}")]
  118. public IActionResult Delete(int id)
  119. {
  120. _Repo.RemoveAtId(id);
  121. return new OkResult();
  122. }
  123. }
  124. }