2021-04-06 18:17:23 +02:00

235 lines
7.8 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")
[Route("[controller]")]
[ApiController]
[OpenApiTag("Section", Description = "Section management")]
public class SectionController : ControllerBase
{
private SectionDatabaseService _sectionService;
private readonly ILogger<SectionController> _logger;
public SectionController(ILogger<SectionController> logger, SectionDatabaseService sectionService)
{
_logger = logger;
_sectionService = sectionService;
}
/// <summary>
/// Get a list of all section (summary)
/// </summary>
[ProducesResponseType(typeof(List<SectionDTO>), 200)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet]
public ObjectResult Get()
{
try
{
List<Section> sections = _sectionService.GetAll();
return new OkObjectResult(sections.Select(r => r.ToDTO()));
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Get a list of all subsection (summary) of a specific section
/// </summary>
/// <param name="id">section id</param>
[ProducesResponseType(typeof(List<SectionDTO>), 200)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("{id}/subsections")]
public ObjectResult GetAllSectionSubSections(string id)
{
try
{
List<Section> sections = _sectionService.GetAllSubSection(id);
return new OkObjectResult(sections.Select(r => r.ToDTO()));
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Get a specific section
/// </summary>
/// <param name="id">section id</param>
[AllowAnonymous]
[ProducesResponseType(typeof(SectionDTO), 200)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("{id}")]
public ObjectResult GetDetail(string id)
{
try
{
Section section = _sectionService.GetById(id);
if (section == null)
throw new KeyNotFoundException("This section was not found");
return new OkObjectResult(section.ToDTO());
}
catch (KeyNotFoundException ex)
{
return new NotFoundObjectResult(ex.Message) {};
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Create a new section
/// </summary>
/// <param name="newSection">New section info</param>
[ProducesResponseType(typeof(SectionDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 409)]
[ProducesResponseType(typeof(string), 500)]
[HttpPost]
public ObjectResult Create([FromBody] SectionDTO newSection)
{
try
{
if (newSection == null)
throw new ArgumentNullException("Section param is null");
// Todo add some verification ?
Section section = new Section();
section.Label = newSection.Label;
section.ImageId = newSection.ImageId;
section.IsSubSection = newSection.IsSubSection;
section.ParentId = newSection.ParentId;
section.Type = newSection.Type;
section.Data = newSection.Data;
section.DateCreation = DateTime.Now;
Section sectionCreated = _sectionService.Create(section);
return new OkObjectResult(sectionCreated.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 section
/// </summary>
/// <param name="updatedSection">Section to update</param>
[ProducesResponseType(typeof(SectionDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpPut]
public ObjectResult Update([FromBody] SectionDTO updatedSection)
{
try
{
if (updatedSection == null)
throw new ArgumentNullException("Section param is null");
Section section = _sectionService.GetById(updatedSection.Id);
if (section == null)
throw new KeyNotFoundException("Section does not exist");
// Todo add some verification ?
section.Label = updatedSection.Label;
section.ImageId = updatedSection.ImageId;
section.IsSubSection = updatedSection.IsSubSection;
section.ParentId = updatedSection.ParentId;
section.Type = updatedSection.Type;
section.Data = updatedSection.Data;
Section sectionModified = _sectionService.Update(updatedSection.Id, section);
return new OkObjectResult(sectionModified.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 section
/// </summary>
/// <param name="id">Id of section 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("Section param is null");
if (!_sectionService.IsExist(id))
throw new KeyNotFoundException("Section does not exist");
_sectionService.Remove(id);
return new ObjectResult("The section 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 };
}
}
}
}