211 lines
7.1 KiB
C#
211 lines
7.1 KiB
C#
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")
|
|
[ApiController, Route("api/[controller]")]
|
|
[OpenApiTag("Display", Description = "Display management")]
|
|
public class DisplayController : ControllerBase
|
|
{
|
|
private DisplayDatabaseService _displayService;
|
|
private readonly ILogger<DisplayController> _logger;
|
|
|
|
public DisplayController(ILogger<DisplayController> logger, DisplayDatabaseService displayService)
|
|
{
|
|
_logger = logger;
|
|
_displayService = displayService;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a list of all display (summary)
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(List<DisplayDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet]
|
|
public ObjectResult Get()
|
|
{
|
|
try
|
|
{
|
|
List<Display> displays = _displayService.GetAll();
|
|
|
|
return new OkObjectResult(displays.Select(r => r.ToDTO()));
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
return new ObjectResult(ex.Message) { StatusCode = 500 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Get a specific display
|
|
/// </summary>
|
|
/// <param name="id">id display</param>
|
|
[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 };
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Create a new display
|
|
/// </summary>
|
|
/// <param name="newDisplay">New display info</param>
|
|
[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.Languages = newDisplay.Languages;
|
|
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 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Update a display
|
|
/// </summary>
|
|
/// <param name="updatedDisplay">Display to update</param>
|
|
[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.Languages = updatedDisplay.Languages;
|
|
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 };
|
|
}
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Delete a display
|
|
/// </summary>
|
|
/// <param name="id">Id of display to delete</param>
|
|
[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 };
|
|
}
|
|
}
|
|
}
|
|
}
|