using Microsoft.AspNetCore.Mvc;
using System.Transactions;
using UnivateProperties_API.Model.Misc;
using UnivateProperties_API.Repository.Misc;

namespace UnivateProperties_API.Controllers.Misc
{
    [Route("api/[controller]")]
    [ApiController]
    public class CarouselController : ControllerBase
    {
        private readonly ICarouselRepository _Repo;

        public CarouselController(ICarouselRepository repo)
        {
            _Repo = repo;
        }

        [HttpGet]
        public IActionResult Get()
        {
            return new OkObjectResult(_Repo.GetCarouselItems());
        }

        [HttpGet("{id}")]
        public IActionResult Get(int id)
        {
            return new OkObjectResult(_Repo.GetCarousel(id));
        }        

        [HttpPost]
        public IActionResult Post([FromBody] Carousel carousel)
        {
            using (var scope = new TransactionScope())
            {
                _Repo.Insert(carousel);
                scope.Complete();
                return CreatedAtAction(nameof(Get), new { id = carousel.Id }, carousel);
            }
        }

        [HttpPut]
        public IActionResult Put([FromBody] Carousel carousel)
        {
            if (carousel != null)
            {
                using (var scope = new TransactionScope())
                {
                    _Repo.Update(carousel);
                    scope.Complete();
                    return new OkResult();
                }
            }
            return new NoContentResult();
        }

        [HttpDelete("{id}")]
        public IActionResult Delete(int id)
        {
            _Repo.RemoveAtId(id);
            return new OkResult();
        }
    }
}