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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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/{keyword}")]
  29. public IActionResult Search(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("latestProperties")]
  39. public IActionResult GetLatestProperties()
  40. {
  41. return new OkObjectResult(_Repo.GetLatestDisplay());
  42. }
  43. [HttpGet("{type}/{by}")]
  44. public IActionResult SearchBy(string type, string by)
  45. {
  46. PropertyUsageType pType = PropertyUsageType.Both;
  47. switch (type.ToUpper())
  48. {
  49. case "RESIDENTIAL":
  50. pType = PropertyUsageType.Residential;
  51. break;
  52. case "COMMERCIAL":
  53. pType = PropertyUsageType.Commercial;
  54. break;
  55. }
  56. if (pType != PropertyUsageType.Both)
  57. {
  58. List<int> proptypeIds = (from pt in _Repo.GetPropertyTypes(p => p.UsageType == pType)
  59. select pt.Id).ToList();
  60. if (string.IsNullOrEmpty(by) || by.ToUpper() == "ALL")
  61. {
  62. return new OkObjectResult(_Repo.Get(x => proptypeIds.Contains(x.PropertyTypeId)));
  63. }
  64. else
  65. {
  66. return new OkObjectResult(_Repo.Get(x => proptypeIds.Contains(x.PropertyTypeId) && x.CreatedBy == by));
  67. }
  68. }
  69. else
  70. return new NoContentResult();
  71. }
  72. [HttpPost]
  73. public IActionResult Post([FromBody] Property property)
  74. {
  75. using (var scope = new TransactionScope())
  76. {
  77. _Repo.Insert(property);
  78. scope.Complete();
  79. return CreatedAtAction(nameof(Get), new { id = property.Id }, property);
  80. }
  81. }
  82. [HttpPut]
  83. public IActionResult Put([FromBody] Property property)
  84. {
  85. if (property != null)
  86. {
  87. using (var scope = new TransactionScope())
  88. {
  89. _Repo.Update(property);
  90. scope.Complete();
  91. return new OkResult();
  92. }
  93. }
  94. return new NoContentResult();
  95. }
  96. [HttpDelete("{id}")]
  97. public IActionResult Delete(int id)
  98. {
  99. _Repo.RemoveAtId(id);
  100. return new OkResult();
  101. }
  102. }
  103. }