| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 | using Microsoft.AspNetCore.Mvc;
using System.Transactions;
using UnivateProperties_API.Model.Financial;
using UnivateProperties_API.Repository;
namespace UnivateProperties_API.Controllers.Financial
{
    [Route("api/[controller]")]
    [ApiController]
    public class PaymentController : ControllerBase
    {
        private readonly IRepository<Payment> _Repo;
        public PaymentController(IRepository<Payment> _repo)
        {
            _Repo = _repo;
        }
        
        [HttpGet]
        public IActionResult Get()
        {
            return new OkObjectResult(_Repo.GetAll());
        }
        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            return new OkObjectResult(_Repo.Get(x => x.Id == id));
        }
        
        [HttpPost]
        public IActionResult Post([FromBody] Payment payment)
        {
            using (var scope = new TransactionScope())
            {
                _Repo.Insert(payment);
                scope.Complete();
                return CreatedAtAction(nameof(Get), new { id = payment.Id }, payment);
            }
        }
        
        [HttpPut("{id}")]
        public IActionResult Put([FromBody] Payment payment)
        {
            if (payment != null)
            {
                using (var scope = new TransactionScope())
                {
                    _Repo.Update(payment);
                    scope.Complete();
                    return new OkResult();
                }
            }
            return new NoContentResult();
        }       
    }
}
 |