API
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

PropertyController.cs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. [HttpPost]
  77. public IActionResult Post([FromBody] Property property)
  78. {
  79. using (var scope = new TransactionScope())
  80. {
  81. _Repo.Insert(property);
  82. scope.Complete();
  83. return CreatedAtAction(nameof(Get), new { id = property.Id }, property);
  84. }
  85. }
  86. [HttpPut]
  87. public IActionResult Put([FromBody] Property property)
  88. {
  89. if (property != null)
  90. {
  91. using (var scope = new TransactionScope())
  92. {
  93. _Repo.Update(property);
  94. scope.Complete();
  95. return new OkResult();
  96. }
  97. }
  98. return new NoContentResult();
  99. }
  100. [HttpDelete("{id}")]
  101. public IActionResult Delete(int id)
  102. {
  103. _Repo.RemoveAtId(id);
  104. return new OkResult();
  105. }
  106. }
  107. }