using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using eprintServer.Services; using eprintServer.Requests; using eprintServer.Responses; using eprintServer.Entities; using eprintServer.Interfaces; namespace eprintServer.Controllers; [ApiController] [Route("api/v1/cms/branding")] [Authorize(Roles = "ADMIN,SUB_ADMIN")] public class BrandingController : ControllerBase { private readonly BrandingColorService _brandingService; private readonly IFileStorageService _fileStorageService; public BrandingController(BrandingColorService brandingService, IFileStorageService fileStorageService) { _brandingService = brandingService; _fileStorageService = fileStorageService; } [HttpGet] [AllowAnonymous] public async Task> GetBrandingSettings() { // Service now returns BrandingColor with Logo and Favicon eagerly loaded var settings = await _brandingService.GetBrandingSettingsAsync(); // Map data from the loaded entity, including FileModel objects var response = new BrandingSettingsRequest { PrimaryColor = settings.PrimaryColor, SecondaryColor = settings.SecondaryColor, AccentColor = settings.AccentColor, ButtonColor = settings.ButtonColor, HeaderColor = settings.HeaderColor, PlatformName = settings.PlatformName, PlatformEmail = settings.PlatformEmail, // Mapping the full FileModel object Logo = settings.Logo, Favicon = settings.Favicon }; return Ok(response); } [HttpPut] public async Task> UpdateBrandingSettings([FromBody] UpdateBrandingSettingsRequest request) { // The service now handles checking for null/non-null properties in the request var updatedSettings = await _brandingService.UpdateBrandingSettingsAsync(request); // Map data back, including the existing file objects var response = new BrandingSettingsRequest { PrimaryColor = updatedSettings.PrimaryColor, SecondaryColor = updatedSettings.SecondaryColor, AccentColor = updatedSettings.AccentColor, ButtonColor = updatedSettings.ButtonColor, HeaderColor = updatedSettings.HeaderColor, PlatformName = updatedSettings.PlatformName, PlatformEmail = updatedSettings.PlatformEmail, // Mapping the full FileModel object Logo = updatedSettings.Logo, Favicon = updatedSettings.Favicon }; return Ok(response); } [HttpPost("upload/logo")] public async Task> UploadLogo(IFormFile logo) { if (logo == null || logo.Length == 0) { return BadRequest("A file is required."); } try { const string SubDirectory = "logos"; var fileModel = await _fileStorageService.SaveFileAsync(logo, SubDirectory); var brandingSettings = await _brandingService.GetBrandingSettingsAsync(); brandingSettings.SetLogo(fileModel); await _brandingService.SaveBrandingSettingsAsync(brandingSettings); var response = new FileUploadResponse(fileModel); return Ok(response); } catch (Exception ex) { return StatusCode(500, $"Something went wrong during logo upload: {ex.Message}"); } } [HttpPost("upload/favicon")] public async Task> UploadFavicon(IFormFile favicon) { if (favicon == null || favicon.Length == 0) { return BadRequest("A file is required."); } try { const string SubDirectory = "favicons"; var fileModel = await _fileStorageService.SaveFileAsync(favicon, SubDirectory); var brandingSettings = await _brandingService.GetBrandingSettingsAsync(); brandingSettings.SetFavicon(fileModel); await _brandingService.SaveBrandingSettingsAsync(brandingSettings); var response = new FileUploadResponse(fileModel); return Ok(response); } catch (Exception ex) { return StatusCode(500, $"Something went wrong during favicon upload: {ex.Message}"); } } }