using Manager.Helpers; using Manager.Interfaces.DTO; using Manager.Interfaces.Models; using Manager.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.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 _logger; private readonly IConfiguration _configuration; public SectionController(IConfiguration configuration, ILogger logger, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService) { _logger = logger; _configuration = configuration; _sectionService = sectionService; _configurationService = configurationService; } /// /// Get a list of all section (summary) /// /// id instance [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 500)] [HttpGet] public ObjectResult Get([FromQuery] string instanceId) { try { List
sections = _sectionService.GetAll(instanceId); /* CLEAN ARTICLE AUDIO - Init new field AudioIds */ /*foreach (var article in sections.Where(s => s.Type == SectionType.Article)) { try { ArticleDTO articleDTO = JsonConvert.DeserializeObject(article.Data); List languages = _configuration.GetSection("SupportedLanguages").Get>(); 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(r => r.ToDTO())); } catch (Exception ex) { return new ObjectResult(ex.Message) { StatusCode = 500 }; } } /// /// Get a list of all section from a specific configuration /// /// configuration id [AllowAnonymous] [ProducesResponseType(typeof(List), 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"); if (_configurationService.IsExist(id)) { List
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 }; } } /// /// Delete all section from a specific configuration /// /// configuration id [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 }; } } /// /// Get a list of all subsection (summary) of a specific section /// /// section id [ProducesResponseType(typeof(List), 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
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 }; } } /// /// 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()); /*switch (section.Type) { case SectionType.Map: MapDTO mapDTO = JsonConvert.DeserializeObject(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(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 }; } } /// /// Get all section with beacon /// /// Instance id [AllowAnonymous] [ProducesResponseType(typeof(List), 200)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("beacons/{id}")] public ObjectResult GetAllBeaconsForInstance(string instanceId) { try { List
sections = _sectionService.GetAll(instanceId); if (sections.Count == 0) throw new KeyNotFoundException("No section has beacon"); 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 }; } } /// /// 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"); 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.InstanceId = newSection.instanceId; 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(); section.Description = new List(); section.Order = _sectionService.GetAllFromConfiguration(newSection.configurationId).Count; section.IsBeacon = newSection.isBeacon; section.BeaconId = newSection.beaconId; section.Latitude = newSection.latitude; section.Longitude = newSection.longitude; section.MeterZoneGPS = newSection.meterZoneGPS; // Preparation List languages = _configuration.GetSection("SupportedLanguages").Get>(); var contentArticle = new List(); var mapDTO = new MapDTO(); // For menu dto var sliderDTO = new SliderDTO(); // For menu dto section.Title = LanguageInit.Init("Title", languages); section.Description = LanguageInit.Init("Description", languages); contentArticle = LanguageInit.Init("Content", languages); /*foreach (var language in languages) { TranslationDTO title = new TranslationDTO(); TranslationDTO description = new TranslationDTO(); TranslationDTO content = new TranslationDTO(); title.language = language.ToUpper(); description.language = language.ToUpper(); content.language = language.ToUpper(); switch (language.ToUpper()) { case "FR": title.value = "Titre en français"; description.value = "Description en français"; content.value = "Contenu en français"; break; case "EN": title.value = "Title in english"; description.value = "Description en anglais"; content.value = "Contenu en anglais"; break; case "NL": title.value = "Titre in dutch"; description.value = "Description en néerlandais"; content.value = "Contenu en néerlandais"; break; case "DE": title.value = "Titre en allemand"; description.value = "Description en allemand"; content.value = "Contenu en allemand"; break; } section.Title.Add(title); section.Description.Add(description); contentArticle.Add(content); }*/ 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(); section.Data = JsonConvert.SerializeObject(mapDTO); // Include all info from specific section as JSON break; case SectionType.Slider: sliderDTO = new SliderDTO(); sliderDTO.images = new List(); 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 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; case SectionType.Quizz: QuizzDTO quizzDTO = new QuizzDTO(); quizzDTO.questions = new List(); section.Data = JsonConvert.SerializeObject(quizzDTO); // Include all info from specific section as JSON break; case SectionType.Article: ArticleDTO articleDTO = new ArticleDTO(); articleDTO.images = new List(); articleDTO.content = contentArticle; articleDTO.audioIds = LanguageInit.Init("Audio", languages, true); section.Data = JsonConvert.SerializeObject(articleDTO); // 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 }; } } /*/// /// Create a new section - slider /// /// New section info - slider [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 }; } }*/ /// /// 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.InstanceId = updatedSection.instanceId; 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.IsBeacon = updatedSection.isBeacon; section.BeaconId = updatedSection.beaconId; section.Latitude = updatedSection.latitude; section.Longitude = updatedSection.longitude; section.MeterZoneGPS = updatedSection.meterZoneGPS; Section sectionModified = _sectionService.Update(updatedSection.id, section); // TODO MqttClientService.PublishMessage($"config/{sectionModified.ConfigurationId}", JsonConvert.SerializeObject(new PlayerMessageDTO() { configChanged = true })); 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 }; } } /// /// Update sections order /// /// New sections order [ProducesResponseType(typeof(string), 200)] [ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpPut("order")] public ObjectResult UpdateOrder([FromBody] List 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); } if (updatedSectionsOrder.Count > 0) { // TODO 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 }; } } /// /// 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 }; } } /// /// Useless, just to generate dto code /// [ProducesResponseType(typeof(MapDTO), 200)] [HttpGet("MapDTO")] public ObjectResult GetMapDTO() { return new ObjectResult("MapDTO") { StatusCode = 200 }; } /// /// Useless, just to generate dto code /// [ProducesResponseType(typeof(SliderDTO), 200)] [HttpGet("SliderDTO")] public ObjectResult GetSliderDTO() { return new ObjectResult("SliderDTO") { StatusCode = 200 }; } /// /// Useless, just to generate dto code /// [ProducesResponseType(typeof(VideoDTO), 200)] [HttpGet("VideoDTO")] public ObjectResult GetVideoDTO() { return new ObjectResult("VideoDTO") { StatusCode = 200 }; } /// /// Useless, just to generate dto code /// [ProducesResponseType(typeof(WebDTO), 200)] [HttpGet("WebDTO")] public ObjectResult GetWebDTO() { return new ObjectResult("WebDTO") { StatusCode = 200 }; } /// /// Useless, just to generate dto code /// [ProducesResponseType(typeof(MenuDTO), 200)] [HttpGet("MenuDTO")] public ObjectResult GetMenuDTO() { return new ObjectResult("MenuDTO") { StatusCode = 200 }; } /// /// Useless, just to generate dto code /// [ProducesResponseType(typeof(PlayerMessageDTO), 200)] [HttpGet("PlayerMessageDTO")] public ObjectResult PlayerMessageDTO() { return new ObjectResult("PlayerMessageDTO") { StatusCode = 200 }; } /// /// Useless, just to generate dto code /// [ProducesResponseType(typeof(QuizzDTO), 200)] [HttpGet("QuizzDTO")] public ObjectResult GetQuizzDTO() { return new ObjectResult("QuizzDTO") { StatusCode = 200 }; } /// /// Useless, just to generate dto code /// [ProducesResponseType(typeof(ArticleDTO), 200)] [HttpGet("ArticleDTO")] public ObjectResult GetArticleDTO() { return new ObjectResult("ArticleDTO") { StatusCode = 200 }; } } }