using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Manager.Interfaces.DTO; using Manager.Interfaces.Models; using Manager.Services; using ManagerService.Service.Services; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using NSwag.Annotations; namespace ManagerService.Controllers { [Authorize] // TODO Add ROLES (Roles = "Admin") [Route("[controller]")] [ApiController] [OpenApiTag("Display", Description = "Display management")] public class DisplayController : ControllerBase { private DisplayDatabaseService _displayService; private readonly ILogger _logger; public DisplayController(ILogger logger, DisplayDatabaseService displayService) { _logger = logger; _displayService = displayService; } /// /// Get a list of all display (summary) /// [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet] public ObjectResult Get() { try { List displays = _displayService.GetAll(); return new OkObjectResult(displays.Select(r => r.ToDTO())); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get a specific display /// /// id display [AllowAnonymous] [ProducesResponseType(typeof(DisplayDTO), 200)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{id}")] public ObjectResult GetDetail(string id) { try { Display display = _displayService.GetById(id); if (display == null) throw new KeyNotFoundException("This display was not found"); return new OkObjectResult(display.ToDTO()); } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) {}; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Create a new display /// /// New display info [ProducesResponseType(typeof(RessourceDetailDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 409)] [ProducesResponseType(typeof(string), 500)] [HttpPost] public ObjectResult Create([FromBody] DisplayDTO newDisplay) { try { if (newDisplay == null) throw new ArgumentNullException("Display param is null"); // Todo add some verification ? Display display = new Display(); display.Label = newDisplay.Label; display.PrimaryColor = newDisplay.PrimaryColor; display.SecondaryColor = newDisplay.SecondaryColor; display.SectionIds = newDisplay.SectionIds; display.DateCreation = DateTime.Now; Display displayCreated = _displayService.Create(display); return new OkObjectResult(displayCreated.ToDTO()); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) {}; } catch (InvalidOperationException ex) { return new ConflictObjectResult(ex.Message) {}; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Update a display /// /// Display to update [ProducesResponseType(typeof(DisplayDTO), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpPut] public ObjectResult Update([FromBody] DisplayDTO updatedDisplay) { try { if (updatedDisplay == null) throw new ArgumentNullException("Display param is null"); Display display = _displayService.GetById(updatedDisplay.Id); if (display == null) throw new KeyNotFoundException("Display does not exist"); // Todo add some verification ? display.Label = updatedDisplay.Label; display.PrimaryColor = updatedDisplay.PrimaryColor; display.SecondaryColor = updatedDisplay.SecondaryColor; display.SectionIds = updatedDisplay.SectionIds; Display displayModified = _displayService.Update(updatedDisplay.Id, display); return new OkObjectResult(displayModified.ToDTO()); } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) {}; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) {}; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Delete a display /// /// Id of display to delete [ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpDelete("{id}")] public ObjectResult Delete(string id) { try { if (id == null) throw new ArgumentNullException("Display param is null"); if (!_displayService.IsExist(id)) throw new KeyNotFoundException("Display does not exist"); _displayService.Remove(id); return new ObjectResult("The display has been deleted") { StatusCode = 202 }; } catch (ArgumentNullException ex) { return new BadRequestObjectResult(ex.Message) { }; } catch (KeyNotFoundException ex) { return new NotFoundObjectResult(ex.Message) { }; } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } } }