using System.Transactions; using Microsoft.AspNetCore.Mvc; using UnivateProperties_API.Model.Communication; using UnivateProperties_API.Repository; namespace UnivateProperties_API.Controllers.Communication { [Route("api/[controller]")] [ApiController] public class MailSourceController : ControllerBase { private readonly IRepository _Repo; public MailSourceController(IRepository repo) { _Repo = repo; } [HttpGet] public IActionResult Get() { return new OkObjectResult(_Repo.GetAll()); } [HttpGet("{id}")] public IActionResult Get(int id) { return new OkObjectResult(_Repo.GetDetailed(x => x.Id == id)); } [HttpPost] public IActionResult Post([FromBody] MailSource mailSource) { using (var scope = new TransactionScope()) { _Repo.Insert(mailSource); scope.Complete(); return CreatedAtAction(nameof(Get), new { id = mailSource.Id }, mailSource); } } [HttpPut] public IActionResult Put([FromBody] MailSource mailSource) { if (mailSource != null) { using (var scope = new TransactionScope()) { _Repo.Update(mailSource); scope.Complete(); return new OkResult(); } } return new NoContentResult(); } [HttpDelete("{id}")] public IActionResult Delete(int id) { _Repo.RemoveAtId(id); return new OkResult(); } } }