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.

AccountController.cs 1.7KB

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