using Microsoft.AspNetCore.Mvc; using System.Transactions; using UnivateProperties_API.Containers.Timeshare; using UnivateProperties_API.Model.Timeshare; using UnivateProperties_API.Repository; using UnivateProperties_API.Repository.Timeshare; namespace UnivateProperties_API.Controllers.Timeshare { [Route("api/[controller]")] [ApiController] public class TimeshareWeekController : ControllerBase { private readonly IRepository _Repo; public TimeshareWeekController(IRepository repo) { _Repo = repo; } [HttpGet] public IActionResult Get() { var items = (_Repo as WeekRepository).GetDtoListAll(); return new OkObjectResult(items); } [HttpGet("{id}")] public IActionResult Get(int id) { var item = (_Repo as WeekRepository).GetMyDetailed(x => x.Id == id); return new OkObjectResult(item); } [HttpGet("getAvailResort")] public IActionResult GetAvailResort() { if (_Repo is WeekRepository) { var item = (_Repo as WeekRepository).GetAvailResort(); return new OkObjectResult(item); } else return new OkResult(); } [HttpGet("getMyWeek/{id}")] public IActionResult GetMyWeek(int id) { if (_Repo is WeekRepository) { var item = (_Repo as WeekRepository).GetMyWeeks(id); return new OkObjectResult(item); } else return new OkResult(); } [HttpGet("getNeedApproval")] public IActionResult GetNeedApproval() { if (_Repo is WeekRepository) { var item = (_Repo as WeekRepository).GetNeedApproval(); return new OkObjectResult(item); } else return new OkResult(); } [HttpGet("getBy")] public IActionResult GetBy(WeekFilterDto week) { var item = (_Repo as WeekRepository).GetBy(week); return new OkObjectResult(item); } [HttpPost] public IActionResult Post([FromBody] TimeshareWeek 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] TimeshareWeek 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(); } [HttpPost("verifyweek/{id}")] public IActionResult Post(int id) { using (var scope = new TransactionScope()) { (_Repo as WeekRepository).VerifyWeek(id); scope.Complete(); return new OkResult(); } } [HttpPost("publishweek/{id}")] public IActionResult Publishweek(int id) { using (var scope = new TransactionScope()) { (_Repo as WeekRepository).VerifyWeek(id); scope.Complete(); return new OkResult(); } } [HttpGet("getTenderWeeks")] public IActionResult GetTenderWeeks() { var item = (_Repo as WeekRepository).GetTenderWeeks(); return new OkObjectResult(item); } } }