2021-07-14 20:36:13 +02:00

603 lines
24 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 ConfigurationDatabaseService _configurationService;
private readonly ILogger<SectionController> _logger;
public SectionController(ILogger<SectionController> logger, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService)
{
_logger = logger;
_sectionService = sectionService;
_configurationService = configurationService;
}
/// <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 section from a specific configuration
/// </summary>
/// <param name="id">configuration id</param>
[AllowAnonymous]
[ProducesResponseType(typeof(List<SectionDTO>), 200)]
[ProducesResponseType(typeof(string), 500)]
[ProducesResponseType(typeof(string), 400)]
[HttpGet("configuration/{id}")]
public ObjectResult GetFromConfiguration(string id)
{
try
{
if (id == null)
throw new ArgumentNullException("Param is null");
List<Section> sections = _sectionService.GetAllFromConfiguration(id);
return new OkObjectResult(sections.Select(r => r.ToDTO()));
}
catch (ArgumentNullException ex)
{
return new BadRequestObjectResult(ex.Message) { };
}
catch (Exception ex)
{
return new ObjectResult(ex.Message) { StatusCode = 500 };
}
}
/// <summary>
/// Delete all section from a specific configuration
/// </summary>
/// <param name="id">configuration id</param>
[ProducesResponseType(typeof(string), 202)]
[ProducesResponseType(typeof(string), 500)]
[ProducesResponseType(typeof(string), 400)]
[HttpDelete("configuration/{id}")]
public ObjectResult DeleteAllForConfiguration(string id)
{
try
{
if (id == null)
throw new ArgumentNullException("Param is null");
_sectionService.DeleteAllFromConfiguration(id);
return new ObjectResult("All section from the specified configuration has been deleted") { StatusCode = 202 };
}
catch (ArgumentNullException ex)
{
return new BadRequestObjectResult(ex.Message) { };
}
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)]
[ProducesResponseType(typeof(string), 400)]
[HttpGet("{id}/subsections")]
public ObjectResult GetAllSectionSubSections(string id)
{
try
{
if (id == null)
throw new ArgumentNullException("Param is null");
List<Section> sections = _sectionService.GetAllSubSection(id);
return new OkObjectResult(sections.Select(r => r.ToDTO()));
}
catch (ArgumentNullException ex)
{
return new BadRequestObjectResult(ex.Message) { };
}
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());
/*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");
if (newSection.ConfigurationId == null)
throw new ArgumentNullException("Configuration param is null");
if (!_configurationService.IsExist(newSection.ConfigurationId) )
throw new KeyNotFoundException("Configuration does not exist");
// Todo add some verification ?
Section section = new Section();
section.Label = newSection.Label;
section.ImageId = newSection.ImageId;
section.ImageSource = newSection.ImageSource;
section.ConfigurationId = newSection.ConfigurationId;
section.DateCreation = DateTime.Now;
section.IsSubSection = newSection.IsSubSection;
section.ParentId = newSection.ParentId;
section.Type = newSection.Type;
section.Title = new List<TranslationDTO>();
section.Description = new List<TranslationDTO>();
section.Order = _sectionService.GetAllFromConfiguration(newSection.ConfigurationId).Count;
// Preparation
List<string> languages = new List<string> { "FR", "NL", "EN", "DE" };//_configurationService.GetById(newSection.ConfigurationId).Languages;
var mapDTO = new MapDTO(); // For menu dto
var sliderDTO = new SliderDTO(); // For menu dto
foreach (var language in languages)
{
TranslationDTO title = new TranslationDTO();
TranslationDTO description = new TranslationDTO();
title.Language = language.ToUpper();
description.Language = language.ToUpper();
switch (language.ToUpper())
{
case "FR":
title.Value = "Titre en français";
description.Value = "Description en français";
break;
case "EN":
title.Value = "Title in english";
description.Value = "Description en anglais";
break;
case "NL":
title.Value = "Titre in dutch";
description.Value = "Description en néerlandais";
break;
case "DE":
title.Value = "Titre en allemand";
description.Value = "Description en allemand";
break;
}
section.Title.Add(title);
section.Description.Add(description);
}
section.Title = section.Title.OrderBy(t => t.Language).ToList();
section.Description = section.Description.OrderBy(d => d.Language).ToList();
switch (newSection.Type) {
case SectionType.Map:
mapDTO = new MapDTO();
mapDTO.MapType = MapTypeApp.hybrid;
mapDTO.Zoom = 18;
mapDTO.Points = new List<GeoPointDTO>() {
new GeoPointDTO() {
Id = 0,
Title = section.Title,
Description = section.Description,
Latitude = "50.416639",
Longitude= "4.879169",
Images = new List<ImageGeoPoint>()
}
};
section.Data = JsonConvert.SerializeObject(mapDTO); // Include all info from specific section as JSON
break;
case SectionType.Slider:
sliderDTO = new SliderDTO();
ImageDTO imageDTO = new ImageDTO();
imageDTO.Title = section.Title;
imageDTO.Description = section.Description;
imageDTO.Source = null;
sliderDTO.Images = new List<ImageDTO>();
sliderDTO.Images.Add(imageDTO);
section.Data = JsonConvert.SerializeObject(sliderDTO); // Include all info from specific section as JSON
break;
case SectionType.Video:
VideoDTO videoDTO = new VideoDTO();
videoDTO.Source = "";
section.Data = JsonConvert.SerializeObject(videoDTO); // Include all info from specific section as JSON
break;
case SectionType.Web:
WebDTO webDTO = new WebDTO();
webDTO.Source = "";
section.Data = JsonConvert.SerializeObject(webDTO); // Include all info from specific section as JSON
break;
case SectionType.Menu:
MenuDTO menuDTO = new MenuDTO();
menuDTO.Sections = new List<SectionDTO>();
/*SectionDTO section0DTO = new SectionDTO();
section0DTO.IsSubSection = true;
section0DTO.Label = newSection.Label;
section0DTO.ImageId = newSection.ImageId;
section0DTO.ConfigurationId = newSection.ConfigurationId;
section0DTO.DateCreation = DateTime.Now;
section0DTO.ParentId = null;
section0DTO.Type = SectionType.Map;
section0DTO.Data = JsonConvert.SerializeObject(mapDTO);
SectionDTO section1DTO = new SectionDTO();
section0DTO.IsSubSection = true;
section0DTO.Label = newSection.Label;
section0DTO.ImageId = newSection.ImageId;
section0DTO.ConfigurationId = newSection.ConfigurationId;
section0DTO.DateCreation = DateTime.Now;
section0DTO.ParentId = null;
section0DTO.Type = SectionType.Slider;
section1DTO.IsSubSection = true;
section1DTO.Data = JsonConvert.SerializeObject(sliderDTO);*/
/*menuDTO.Sections.Add(section0DTO);
menuDTO.Sections.Add(section1DTO);*/
section.Data = JsonConvert.SerializeObject(menuDTO); // Include all info from specific section as JSON
break;
}
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.Title = updatedSection.Title;
section.Description = updatedSection.Description;
section.Type = updatedSection.Type;
section.ImageId = updatedSection.ImageId;
section.ImageSource = updatedSection.ImageSource;
section.ConfigurationId = updatedSection.ConfigurationId;
section.IsSubSection = updatedSection.IsSubSection;
section.ParentId = updatedSection.ParentId;
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>
/// Update sections order
/// </summary>
/// <param name="updatedSectionsOrder">New sections order</param>
[ProducesResponseType(typeof(string), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpPut("order")]
public ObjectResult UpdateOrder([FromBody] List<SectionDTO> updatedSectionsOrder)
{
try
{
if (updatedSectionsOrder == null)
throw new ArgumentNullException("Sections param is null");
foreach (var section in updatedSectionsOrder)
{
if (!_sectionService.IsExist(section.Id))
throw new KeyNotFoundException($"Section {section.Label} with id {section.Id} does not exist");
}
foreach (var updatedSection in updatedSectionsOrder)
{
Section section = _sectionService.GetById(updatedSection.Id);
section.Order = updatedSection.Order;
_sectionService.Update(section.Id, section);
}
return new ObjectResult("Sections order has been successfully modified") { StatusCode = 200 };
}
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 };
}
}
/// <summary>
/// Useless, just to generate dto code
/// </summary>
[ProducesResponseType(typeof(MapDTO), 200)]
[HttpGet("MapDTO")]
public ObjectResult GetMapDTO()
{
return new ObjectResult("MapDTO") { StatusCode = 200 };
}
/// <summary>
/// Useless, just to generate dto code
/// </summary>
[ProducesResponseType(typeof(SliderDTO), 200)]
[HttpGet("SliderDTO")]
public ObjectResult GetSliderDTO()
{
return new ObjectResult("SliderDTO") { StatusCode = 200 };
}
/// <summary>
/// Useless, just to generate dto code
/// </summary>
[ProducesResponseType(typeof(VideoDTO), 200)]
[HttpGet("VideoDTO")]
public ObjectResult GetVideoDTO()
{
return new ObjectResult("VideoDTO") { StatusCode = 200 };
}
/// <summary>
/// Useless, just to generate dto code
/// </summary>
[ProducesResponseType(typeof(WebDTO), 200)]
[HttpGet("WebDTO")]
public ObjectResult GetWebDTO()
{
return new ObjectResult("WebDTO") { StatusCode = 200 };
}
/// <summary>
/// Useless, just to generate dto code
/// </summary>
[ProducesResponseType(typeof(MenuDTO), 200)]
[HttpGet("MenuDTO")]
public ObjectResult GetMenuDTO()
{
return new ObjectResult("MenuDTO") { StatusCode = 200 };
}
}
}