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