847 lines
33 KiB
C#
847 lines
33 KiB
C#
using Manager.DTOs;
|
|
using Manager.Helpers;
|
|
using Manager.Interfaces.Models;
|
|
using Manager.Services;
|
|
using ManagerService.Data;
|
|
using ManagerService.Data.SubSection;
|
|
using ManagerService.DTOs;
|
|
using ManagerService.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.Logging;
|
|
using Mqtt.Client.AspNetCore.Services;
|
|
using Newtonsoft.Json;
|
|
using NSwag.Annotations;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using System.Text.Json;
|
|
|
|
namespace ManagerService.Controllers
|
|
{
|
|
[Authorize] // TODO Add ROLES (Roles = "Admin")
|
|
[ApiController, Route("api/[controller]")]
|
|
[OpenApiTag("Section", Description = "Section management")]
|
|
public class SectionController : ControllerBase
|
|
{
|
|
private readonly MyInfoMateDbContext _myInfoMateDbContext;
|
|
|
|
private SectionDatabaseService _sectionService;
|
|
private ConfigurationDatabaseService _configurationService;
|
|
private readonly ILogger<SectionController> _logger;
|
|
private readonly IConfiguration _configuration;
|
|
IHexIdGeneratorService idService = new HexIdGeneratorService();
|
|
|
|
public SectionController(IConfiguration configuration, ILogger<SectionController> logger, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService, MyInfoMateDbContext myInfoMateDbContext)
|
|
{
|
|
_logger = logger;
|
|
_configuration = configuration;
|
|
_sectionService = sectionService;
|
|
_configurationService = configurationService;
|
|
_myInfoMateDbContext = myInfoMateDbContext;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get a list of all section (summary)
|
|
/// </summary>
|
|
/// <param name="id">id instance</param>
|
|
[ProducesResponseType(typeof(List<SectionDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[ProducesResponseType(typeof(string), 400)]
|
|
[HttpGet]
|
|
public ObjectResult Get([FromQuery] string instanceId)
|
|
{
|
|
try
|
|
{
|
|
if (instanceId == null)
|
|
throw new ArgumentNullException("Param is null");
|
|
|
|
//List<OldSection> sections = _sectionService.GetAll(instanceId);
|
|
List<Section> sections = _myInfoMateDbContext.Sections.Where(s => s.InstanceId == instanceId).ToList();
|
|
|
|
|
|
/* CLEAN ARTICLE AUDIO - Init new field AudioIds */
|
|
|
|
/*foreach (var article in sections.Where(s => s.Type == SectionType.Article))
|
|
{
|
|
try
|
|
{
|
|
ArticleDTO articleDTO = JsonConvert.DeserializeObject<ArticleDTO>(article.Data);
|
|
List<string> languages = _configuration.GetSection("SupportedLanguages").Get<List<string>>();
|
|
articleDTO.audioIds = LanguageInit.Init("Audio", languages, true);
|
|
article.Data = JsonConvert.SerializeObject(articleDTO); // Include all info from specific section as JSON
|
|
|
|
Section sectionModified = _sectionService.Update(article.Id, article);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
|
|
}
|
|
}*/
|
|
|
|
return new OkObjectResult(sections.Select(s => s.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");
|
|
|
|
Configuration configuration = _myInfoMateDbContext.Configurations.FirstOrDefault(c => c.Id == id);
|
|
|
|
if (configuration != null)
|
|
{
|
|
List<Section> sections = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == id && !s.IsSubSection).ToList();
|
|
//List<OldSection> sections = _sectionService.GetAllFromConfiguration(id);
|
|
|
|
return new OkObjectResult(sections.Select(r => r.ToDTO()));
|
|
}
|
|
else
|
|
return new NotFoundObjectResult("Configuration not found");
|
|
}
|
|
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);
|
|
List<Section> sections = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == id).ToList();
|
|
// TODO test
|
|
_myInfoMateDbContext.RemoveRange(sections);
|
|
|
|
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 = _myInfoMateDbContext.Sections.Where(s => s.ParentId == id && s.IsSubSection).ToList();
|
|
//List<OldSection> 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(object), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("{id}")]
|
|
public ObjectResult GetDetail(string id)
|
|
{
|
|
try
|
|
{
|
|
//OldSection section = _sectionService.GetById(id);
|
|
Section section = _myInfoMateDbContext.Sections.FirstOrDefault(s => s.Id == id);
|
|
|
|
if (section == null)
|
|
throw new KeyNotFoundException("This section was not found");
|
|
|
|
var dto = SectionFactory.ToDTO(section);
|
|
|
|
return new OkObjectResult(dto);
|
|
/*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>
|
|
/// Get all section with beacon
|
|
/// </summary>
|
|
/// <param name="instanceId">Instance id</param>
|
|
[AllowAnonymous]
|
|
[ProducesResponseType(typeof(List<SectionDTO>), 200)]
|
|
[ProducesResponseType(typeof(string), 404)]
|
|
[ProducesResponseType(typeof(string), 500)]
|
|
[HttpGet("beacons/{instanceId}")]
|
|
public ObjectResult GetAllBeaconsForInstance(string instanceId)
|
|
{
|
|
try
|
|
{
|
|
List<Section> sections = _myInfoMateDbContext.Sections.Where(s => s.InstanceId == instanceId && s.IsBeacon && s.BeaconId != null).ToList();
|
|
//List<OldSection> sections = _sectionService.GetAll(instanceId);
|
|
|
|
//sections = sections.Where(s => s.IsBeacon && s.BeaconId != null).ToList();
|
|
|
|
return new OkObjectResult(sections.Select(s => s.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");
|
|
|
|
var configuration = _myInfoMateDbContext.Configurations.FirstOrDefault(c => c.Id == newSection.configurationId);
|
|
if (configuration == null)
|
|
throw new KeyNotFoundException("Configuration does not exist");
|
|
|
|
// Todo add some verification ?
|
|
Section section = new Section();
|
|
|
|
// Preparation
|
|
List<string> languages = _configuration.GetSection("SupportedLanguages").Get<List<string>>();
|
|
|
|
switch (newSection.type) {
|
|
case SectionType.Map:
|
|
section = new SectionMap
|
|
{
|
|
MapMapType = MapTypeApp.hybrid,
|
|
MapTypeMapbox = MapTypeMapBox.standard,
|
|
MapMapProvider = MapProvider.Google,
|
|
MapZoom = 18,
|
|
MapPoints = new List<GeoPoint>(),
|
|
MapCategories = new List<CategorieDTO>()
|
|
};
|
|
break;
|
|
case SectionType.Slider:
|
|
section = new SectionSlider
|
|
{
|
|
SliderContents = new List<ContentDTO>()
|
|
};
|
|
break;
|
|
case SectionType.Video:
|
|
section = new SectionVideo
|
|
{
|
|
VideoSource = "",
|
|
};
|
|
break;
|
|
case SectionType.Web:
|
|
section = new SectionWeb
|
|
{
|
|
WebSource = "",
|
|
};
|
|
break;
|
|
case SectionType.Menu:
|
|
section = new SectionMenu
|
|
{
|
|
MenuSections = new List<Section>(),
|
|
};
|
|
break;
|
|
case SectionType.Quiz:
|
|
section = new SectionQuiz
|
|
{
|
|
QuizQuestions = new List<QuizQuestion>(),
|
|
// TODO levels ?
|
|
};
|
|
break;
|
|
case SectionType.Article:
|
|
section = new SectionArticle
|
|
{
|
|
ArticleContents = new List<ContentDTO>(),
|
|
ArticleContent = LanguageInit.Init("Content", languages),
|
|
ArticleAudioIds = LanguageInit.Init("Audio", languages, true)
|
|
};
|
|
break;
|
|
case SectionType.PDF:
|
|
section = new SectionPdf
|
|
{
|
|
PDFOrderedTranslationAndResources = []
|
|
};
|
|
break;
|
|
case SectionType.Puzzle:
|
|
section = new SectionPuzzle
|
|
{
|
|
PuzzleMessageDebut = [],
|
|
PuzzleMessageFin = []
|
|
};
|
|
break;
|
|
case SectionType.Agenda:
|
|
section = new SectionAgenda
|
|
{
|
|
AgendaResourceIds = new List<TranslationDTO>()
|
|
};
|
|
break;
|
|
case SectionType.Weather:
|
|
section = new SectionWeather();
|
|
break;
|
|
}
|
|
|
|
section.InstanceId = newSection.instanceId;
|
|
section.Label = newSection.label;
|
|
section.ImageId = newSection.imageId;
|
|
section.ImageSource = newSection.imageSource;
|
|
section.ConfigurationId = newSection.configurationId;
|
|
section.DateCreation = DateTime.Now.ToUniversalTime();
|
|
section.IsSubSection = newSection.isSubSection;
|
|
section.ParentId = newSection.parentId;
|
|
section.Type = newSection.type;
|
|
|
|
// TODO !!
|
|
if (configuration.IsMobile)
|
|
{
|
|
section.Order = _myInfoMateDbContext.Sections.Count(s => s.ConfigurationId == newSection.configurationId && !s.IsSubSection && (s.Type == SectionType.Article || s.Type == SectionType.Quiz)) + 1;
|
|
}
|
|
else
|
|
{
|
|
section.Order = _myInfoMateDbContext.Sections.Count(s => s.ConfigurationId == newSection.configurationId && !s.IsSubSection) + 1;
|
|
}
|
|
|
|
section.IsBeacon = newSection.isBeacon;
|
|
section.BeaconId = newSection.beaconId;
|
|
section.Latitude = newSection.latitude;
|
|
section.Longitude = newSection.longitude;
|
|
section.MeterZoneGPS = newSection.meterZoneGPS;
|
|
|
|
section.Title = LanguageInit.Init("Title", languages);
|
|
section.Description = LanguageInit.Init("Description", languages);
|
|
|
|
section.Id = idService.GenerateHexId();
|
|
//_sectionService.Create(section);
|
|
_myInfoMateDbContext.Add(section);
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
return new OkObjectResult(section.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] dynamic updatedSection)
|
|
{
|
|
try
|
|
{
|
|
if (updatedSection.ValueKind == JsonValueKind.Null)
|
|
throw new ArgumentNullException("Section param is null");
|
|
|
|
SectionDTO sectionDTO;
|
|
if (updatedSection is JsonElement jsonElement)
|
|
{
|
|
if (jsonElement.ValueKind == JsonValueKind.Null)
|
|
throw new ArgumentNullException("Section param is null");
|
|
|
|
// Désérialisation de jsonElement en SectionDTO
|
|
sectionDTO = JsonConvert.DeserializeObject<SectionDTO>(jsonElement.ToString());
|
|
}
|
|
else
|
|
{
|
|
throw new InvalidOperationException("Expected a JsonElement");
|
|
}
|
|
|
|
Section existingSection = _myInfoMateDbContext.Sections.FirstOrDefault(s => s.Id == sectionDTO.id);
|
|
|
|
if (existingSection == null)
|
|
throw new KeyNotFoundException("Section does not exist");
|
|
|
|
if (existingSection.Type != sectionDTO.type)
|
|
return BadRequest("Type mismatch: cannot change section type");
|
|
|
|
var updatedSectionDB = SectionFactory.Create(updatedSection, sectionDTO);
|
|
|
|
// Todo add some verification ?
|
|
/*section.InstanceId = updatedSection.instanceId;
|
|
section.Label = updatedSection.label;
|
|
section.Title = updatedSection.title.Select(t => new Translation().FromDTO(t)).ToList(); // TODO CHECK
|
|
section.Description = updatedSection.description.Select(t => new Translation().FromDTO(t)).ToList();// TODO CHECK
|
|
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.IsBeacon = updatedSection.isBeacon;
|
|
section.BeaconId = updatedSection.beaconId;
|
|
section.Latitude = updatedSection.latitude;
|
|
section.Longitude = updatedSection.longitude;
|
|
section.MeterZoneGPS = updatedSection.meterZoneGPS;*/
|
|
|
|
switch (updatedSectionDB)
|
|
{
|
|
case SectionAgenda agenda:
|
|
// TODO in future events
|
|
break;
|
|
|
|
case SectionMap map:
|
|
// TODO Endpoint categories
|
|
// TODO Endpoint points
|
|
|
|
map.MapCategories = updatedSectionDB.MapCategories;
|
|
//_myInfoMateDbContext.Entry(map).Property(p => p.MapCategories).IsModified = true;
|
|
break;
|
|
|
|
case SectionMenu menu:
|
|
// TODO Endpoint Sections menu
|
|
//menu.Sections = new List<Section>();
|
|
break;
|
|
|
|
case SectionQuiz quiz:
|
|
// TODO Endpoint QuizQuestions
|
|
quiz.QuizQuestions = [];
|
|
break;
|
|
|
|
// Ajoute d'autres types ici
|
|
}
|
|
_myInfoMateDbContext.Entry(existingSection).CurrentValues.SetValues(updatedSectionDB);
|
|
_myInfoMateDbContext.SaveChanges();
|
|
//Section sectionModified = _sectionService.Update(updatedSection.id, section);
|
|
|
|
MqttClientService.PublishMessage($"config/{existingSection.ConfigurationId}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true }));
|
|
|
|
return new OkObjectResult(SectionFactory.ToDTO(existingSection));
|
|
}
|
|
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)
|
|
{
|
|
// TODO REWRITE LOGIC..
|
|
try
|
|
{
|
|
if (updatedSectionsOrder == null)
|
|
throw new ArgumentNullException("Sections param is null");
|
|
|
|
foreach (var section in updatedSectionsOrder)
|
|
{
|
|
var sectionDB = _myInfoMateDbContext.Sections.FirstOrDefault(s => s.Id == section.id);
|
|
if (sectionDB == null)
|
|
throw new KeyNotFoundException($"Section {section.label} with id {section.id} does not exist");
|
|
}
|
|
|
|
foreach (var updatedSection in updatedSectionsOrder)
|
|
{
|
|
var section = _myInfoMateDbContext.Sections.FirstOrDefault(s => s.Id == updatedSection.id);
|
|
//OldSection section = _sectionService.GetById(updatedSection.id);
|
|
section.Order = updatedSection.order.GetValueOrDefault();
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
//_sectionService.Update(section.Id, section);
|
|
}
|
|
|
|
if (updatedSectionsOrder.Count > 0) {
|
|
MqttClientService.PublishMessage($"config/{updatedSectionsOrder[0].configurationId}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true }));
|
|
}
|
|
|
|
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");
|
|
|
|
var section = _myInfoMateDbContext.Sections.FirstOrDefault(s => s.Id == id);
|
|
if (section == null)
|
|
throw new KeyNotFoundException("Section does not exist");
|
|
|
|
_myInfoMateDbContext.Remove(section);
|
|
//_sectionService.Remove(id);
|
|
|
|
var configuration = _myInfoMateDbContext.Configurations.FirstOrDefault(c => c.Id == section.ConfigurationId);
|
|
|
|
if (configuration.IsMobile)
|
|
{
|
|
// TODO !!
|
|
// update order only with article and quiz (FOR NOW)
|
|
List<Section> sections = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == section.ConfigurationId && !s.IsSubSection && (s.Type == SectionType.Article || s.Type == SectionType.Quiz)).ToList();
|
|
int i = 1;
|
|
List<Section> orderedSection = sections.OrderBy(s => s.Order).ToList();
|
|
foreach (var sectionDb in orderedSection)
|
|
{
|
|
sectionDb.Order = i;
|
|
i++;
|
|
}
|
|
}
|
|
else {
|
|
// TODO !!
|
|
// update order from rest // TODO TEST
|
|
List<Section> sections = _myInfoMateDbContext.Sections.Where(s => s.ConfigurationId == section.ConfigurationId && !s.IsSubSection).ToList();
|
|
int i = 1;
|
|
List<Section> orderedSection = sections.OrderBy(s => s.Order).ToList();
|
|
foreach (var sectionDb in orderedSection)
|
|
{
|
|
sectionDb.Order = i;
|
|
i++;
|
|
}
|
|
}
|
|
|
|
|
|
_myInfoMateDbContext.SaveChanges();
|
|
|
|
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 };
|
|
}
|
|
|
|
/// <summary>
|
|
/// Useless, just to generate dto code
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(PlayerMessageDTO), 200)]
|
|
[HttpGet("PlayerMessageDTO")]
|
|
public ObjectResult PlayerMessageDTO()
|
|
{
|
|
return new ObjectResult("PlayerMessageDTO") { StatusCode = 200 };
|
|
}
|
|
|
|
/// <summary>
|
|
/// Useless, just to generate dto code
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(QuizDTO), 200)]
|
|
[HttpGet("QuizDTO")]
|
|
public ObjectResult GetQuizDTO()
|
|
{
|
|
return new ObjectResult("QuizDTO") { StatusCode = 200 };
|
|
}
|
|
|
|
/// <summary>
|
|
/// Useless, just to generate dto code
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(ArticleDTO), 200)]
|
|
[HttpGet("ArticleDTO")]
|
|
public ObjectResult GetArticleDTO()
|
|
{
|
|
return new ObjectResult("ArticleDTO") { StatusCode = 200 };
|
|
}
|
|
|
|
/// <summary>
|
|
/// Useless, just to generate dto code
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(PdfDTO), 200)]
|
|
[HttpGet("PdfDTO")]
|
|
public ObjectResult GetPdfDTO()
|
|
{
|
|
return new ObjectResult("PdfDTO") { StatusCode = 200 };
|
|
}
|
|
|
|
/// <summary>
|
|
/// Useless, just to generate dto code
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(PuzzleDTO), 200)]
|
|
[HttpGet("PuzzleDTO")]
|
|
public ObjectResult GetPuzzleDTO()
|
|
{
|
|
return new ObjectResult("PuzzleDTO") { StatusCode = 200 };
|
|
}
|
|
|
|
/// <summary>
|
|
/// Useless, just to generate dto code
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(AgendaDTO), 200)]
|
|
[HttpGet("AgendaDTO")]
|
|
public ObjectResult GetAgendaDTO()
|
|
{
|
|
return new ObjectResult("AgendaDTO") { StatusCode = 200 };
|
|
}
|
|
|
|
/// <summary>
|
|
/// Useless, just to generate dto code
|
|
/// </summary>
|
|
[ProducesResponseType(typeof(WeatherDTO), 200)]
|
|
[HttpGet("WeatherDTO")]
|
|
public ObjectResult GetWeatherDTO()
|
|
{
|
|
return new ObjectResult("WeatherDTO") { StatusCode = 200 };
|
|
}
|
|
}
|
|
}
|