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 _logger; public SectionController(ILogger logger, SectionDatabaseService sectionService) { _logger = logger; _sectionService = sectionService; } /// /// Get a list of all section (summary) /// [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet] public ObjectResult Get() { try { List
sections = _sectionService.GetAll(); return new OkObjectResult(sections.Select(r => r.ToDTO())); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get a list of all subsection (summary) of a specific section /// /// section id [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{id}/subsections")] public ObjectResult GetAllSectionSubSections(string id) { try { List
sections = _sectionService.GetAllSubSection(id); return new OkObjectResult(sections.Select(r => r.ToDTO())); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get a specific section /// /// section id [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 }; } } /// /// Create a new section /// /// New section info [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 }; } } /// /// Update a section /// /// Section to update [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 }; } } /// /// Delete a section /// /// Id of section 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("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 }; } } } }