API
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

PropertyController.cs 2.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using Microsoft.AspNetCore.Mvc;
  2. using System;
  3. using System.Transactions;
  4. using UnivateProperties_API.Containers.Property;
  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/{type}/{keyword}")]
  29. public IActionResult Get(string type, 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. [HttpPost]
  39. public IActionResult Post([FromBody] Property property)
  40. {
  41. using (var scope = new TransactionScope())
  42. {
  43. _Repo.Insert(property);
  44. scope.Complete();
  45. return CreatedAtAction(nameof(Get), new { id = property.Id }, property);
  46. }
  47. }
  48. [HttpPut]
  49. public IActionResult Put([FromBody] Property property)
  50. {
  51. if (property != null)
  52. {
  53. using (var scope = new TransactionScope())
  54. {
  55. _Repo.Update(property);
  56. scope.Complete();
  57. return new OkResult();
  58. }
  59. }
  60. return new NoContentResult();
  61. }
  62. [HttpDelete("{id}")]
  63. public IActionResult Delete(int id)
  64. {
  65. _Repo.RemoveAtId(id);
  66. return new OkResult();
  67. }
  68. }
  69. }