2021-04-07 17:52:37 +02:00

329 lines
12 KiB
C#

using Manager.Interfaces.DTO;
using Manager.Interfaces.Models;
using Manager.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using NSwag.Annotations;
using System;
using System.Collections.Generic;
using System.Linq;
namespace ManagerService.Controllers
{
[Authorize] // TODO Add ROLES (Roles = "Admin")
[ApiController, Route("api/[controller]")]
[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<object>), 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(object), 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");
switch (section.Type) {
case SectionType.Map:
MapDTO mapDTO = JsonConvert.DeserializeObject<MapDTO>(section.Data);
mapDTO.Id = section.Id;
mapDTO.Label = section.Label;
mapDTO.Description = section.Description;
mapDTO.Type = section.Type;
mapDTO.ImageId = section.ImageId;
mapDTO.IsSubSection = section.IsSubSection;
mapDTO.DateCreation = section.DateCreation;
mapDTO.Data = section.Data;
return new OkObjectResult(mapDTO);
case SectionType.Slider:
SliderDTO sliderDTO = JsonConvert.DeserializeObject<SliderDTO>(section.Data);
sliderDTO.Id = section.Id;
sliderDTO.Label = section.Label;
sliderDTO.Description = section.Description;
sliderDTO.Type = section.Type;
sliderDTO.ImageId = section.ImageId;
sliderDTO.IsSubSection = section.IsSubSection;
sliderDTO.DateCreation = section.DateCreation;
sliderDTO.Data = section.Data;
return new OkObjectResult(section.ToDTO());
case SectionType.Menu:
return new OkObjectResult(section.ToDTO());
case SectionType.Web:
return new OkObjectResult(section.ToDTO());
case SectionType.Video:
return new OkObjectResult(section.ToDTO());
default:
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.DateCreation = DateTime.Now;
section.IsSubSection = false;
section.ParentId = null;
section.Type = newSection.Type;
section.Data = newSection.Data; // Include all info from specific section as JSON
/*section.MapType = newSectionMap.MapType;
section.Zoom = newSectionMap.Zoom;
section.Icon = newSectionMap.Icon;
section.Points = newSectionMap.Points.Select(p =>
new GeoPoint() {
Id = p.Id,
Title = p.Title,
Description = p.Description,
Image = p.Image,
Text = p.Text,
Latitude = p.Latitude,
Longitude = p.Longitude
}).ToList();*/
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>
/// Create a new section - slider
/// </summary>
/// <param name="newSection">New section info - slider</param>
[ProducesResponseType(typeof(SliderDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 409)]
[ProducesResponseType(typeof(string), 500)]
[HttpPost("Slider")]
public ObjectResult CreateSlider([FromBody] SliderDTO newSectionSlider)
{
try
{
if (newSectionSlider == null)
throw new ArgumentNullException("Section param is null");
// Todo add some verification ?
Slider sliderSection = new Slider();
sliderSection.Label = newSectionSlider.Label;
sliderSection.ImageId = newSectionSlider.ImageId;
sliderSection.DateCreation = DateTime.Now;
sliderSection.IsSubSection = false;
sliderSection.ParentId = null;
sliderSection.Type = SectionType.Slider;
sliderSection.Images = newSectionSlider.Images.Select(p =>
new Image()
{
Title = p.Title,
Description = p.Description,
Source = p.Source,
}).ToList();
Slider sectionCreated = _sectionService.CreateSlider(sliderSection);
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 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 };
}
}
}
}