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.

TemplateController.cs 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using System.Transactions;
  6. using Microsoft.AspNetCore.Http;
  7. using Microsoft.AspNetCore.Mvc;
  8. using UnivateProperties_API.Model.Communication;
  9. using UnivateProperties_API.Repository;
  10. namespace UnivateProperties_API.Controllers.Communication
  11. {
  12. [Route("api/[controller]")]
  13. [ApiController]
  14. public class TemplateController : ControllerBase
  15. {
  16. private readonly IRepository<Template> _Repo;
  17. public TemplateController(IRepository<Template> repo)
  18. {
  19. _Repo = repo;
  20. }
  21. [HttpGet]
  22. public IActionResult Get()
  23. {
  24. var items = _Repo.GetAll();
  25. return new OkObjectResult(items);
  26. }
  27. [HttpGet("{id}")]
  28. public IActionResult Get(int id)
  29. {
  30. var item = _Repo.Get(x => x.Id == id);
  31. return new OkObjectResult(item);
  32. }
  33. [HttpPost]
  34. public IActionResult Post([FromBody] Template item)
  35. {
  36. using (var scope = new TransactionScope())
  37. {
  38. _Repo.Insert(item);
  39. scope.Complete();
  40. return CreatedAtAction(nameof(Get), new { id = item.Id }, item);
  41. }
  42. }
  43. [HttpPut("{id}")]
  44. public IActionResult Put([FromBody] Template item)
  45. {
  46. if (item != null)
  47. {
  48. using (var scope = new TransactionScope())
  49. {
  50. _Repo.Update(item);
  51. scope.Complete();
  52. return new OkResult();
  53. }
  54. }
  55. return new NoContentResult();
  56. }
  57. [HttpDelete("{id}")]
  58. public IActionResult Delete(int id)
  59. {
  60. _Repo.RemoveAtId(id);
  61. return new OkResult();
  62. }
  63. }
  64. }