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.

PropertyImageController.cs 2.0KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System.Transactions;
  2. using Microsoft.AspNetCore.Mvc;
  3. using UnivateProperties_API.Model.Properties;
  4. using UnivateProperties_API.Repository.Properties;
  5. namespace UnivateProperties_API.Controllers.Properties
  6. {
  7. [Route("api/[controller]")]
  8. [ApiController]
  9. public class PropertyImageController : ControllerBase
  10. {
  11. private readonly IPropertyImageRepository _Repo;
  12. public PropertyImageController(IPropertyImageRepository repo)
  13. {
  14. _Repo = repo;
  15. }
  16. [HttpGet]
  17. public IActionResult Get()
  18. {
  19. return new OkObjectResult(_Repo.GetAll());
  20. }
  21. [HttpGet("{id}")]
  22. public IActionResult Get(int id)
  23. {
  24. return new OkObjectResult(_Repo.GetDetailed(x => x.Id == id));
  25. }
  26. [HttpGet("{Property}/{PropertyId}", Name = "GetImagesByProperty")]
  27. public IActionResult Get(string Property, int PropertyId) //Property string is more of a placeholder here.
  28. {
  29. return new OkObjectResult(_Repo.GetImages(PropertyId));
  30. }
  31. [HttpPost]
  32. public IActionResult Post([FromBody] PropertyImage propertyImage)
  33. {
  34. using (var scope = new TransactionScope())
  35. {
  36. _Repo.Insert(propertyImage);
  37. scope.Complete();
  38. return CreatedAtAction(nameof(Get), new { id = propertyImage.Id }, propertyImage);
  39. }
  40. }
  41. [HttpPut]
  42. public IActionResult Put([FromBody] PropertyImage propertyImage)
  43. {
  44. if (propertyImage != null)
  45. {
  46. using (var scope = new TransactionScope())
  47. {
  48. _Repo.Update(propertyImage);
  49. scope.Complete();
  50. return new OkResult();
  51. }
  52. }
  53. return new NoContentResult();
  54. }
  55. [HttpDelete("{id}")]
  56. public IActionResult Delete(int id)
  57. {
  58. _Repo.RemoveAtId(id);
  59. return new OkResult();
  60. }
  61. }
  62. }