using Microsoft.AspNetCore.Mvc; using eprintServer.Services; using eprintServer.Requests; using Microsoft.AspNetCore.Authorization; using eprintServer.Interfaces; using eprintServer.Responses; namespace eprintServer.Controllers; [ApiController] [Route("api/[controller]")] [Authorize(Roles = "ADMIN,SUB_ADMIN")] public class CarouselController : ControllerBase { private readonly CarouselService _service; private readonly IFileStorageService _fileStorageService; public CarouselController(CarouselService service, IFileStorageService fileStorageService) { _service = service; _fileStorageService = fileStorageService; } /* [HttpGet("get-all-list")] public async Task GetAll() => Ok(await _service.GetAllAsync()); */ [HttpGet("get-all-list")] public async Task GetAll([FromQuery] bool? status = null) { var list = await _service.GetAllFIltredAsync(status); return Ok(list); } [HttpGet("get-by-id/{id}")] public async Task GetById(long id) { var item = await _service.GetByIdAsync(id); return item == null ? NotFound() : Ok(item); } [HttpPost] public async Task Create([FromBody] CarouselRequest request) { if (request == null) return BadRequest("Request cannot be null."); int nextOrder; if (request.Order > 0) { bool exists = await _service.DoesOrderExistAsync(request.Order); if (exists) return StatusCode(StatusCodes.Status406NotAcceptable, $"Carousel with order {request.Order} already exists."); nextOrder = request.Order; } else { nextOrder = await _service.GetNextOrderAsync(); } // Call service to create the CarouselItem var result = await _service.AddAsync( request.Title ?? new(), request.Description ?? new(), request.ButtonText ?? new(), request.RedirectUrl ?? string.Empty, nextOrder, request.Status ); return CreatedAtAction(nameof(GetById), new { id = result.Id }, result); } [HttpPatch("update/{id}")] public async Task Update(long id, [FromBody] CarouselRequest request) { var result = await _service.UpdateAsync( id, request.Title, request.Description, request.ButtonText, request.RedirectUrl, request.Order, request.Status ); return result == null ? NotFound() : Ok(result); } [HttpDelete("delete/{id}")] public async Task Delete(long id) { var success = await _service.DeleteAsync(id); return success ? NoContent() : NotFound(); } [HttpPost("upload-carousel-image/{carouselId:long}")] public async Task> UploadCategoryImage(long carouselId, IFormFile image) { if (image == null || image.Length == 0) { return BadRequest("An image file is required."); } try { const string SubDirectory = "carouselImages"; var fileModel = await _fileStorageService.SaveFileAsync(image, SubDirectory); var updatedCategory = await _service.UpdateCarouselImageAsync(carouselId, fileModel); if (updatedCategory == null) { await _fileStorageService.DeleteFileAsync(fileModel.Id); return NotFound($"Carousel with ID {carouselId} not found. Image upload failed."); } var response = new ProductImageUploadResponse(fileModel); return Ok(response); } catch (Exception ex) { return StatusCode(500, $"Something went wrong during category image upload: {ex.Message}"); } } }