diff --git a/Manager.Interfaces/DTO/DisplayDTO.cs b/Manager.Interfaces/DTO/DisplayDTO.cs new file mode 100644 index 0000000..045b717 --- /dev/null +++ b/Manager.Interfaces/DTO/DisplayDTO.cs @@ -0,0 +1,16 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.DTO +{ + public class DisplayDTO + { + public string Id { get; set; } + public string Label { get; set; } + public List SectionIds { get; set; } + public string PrimaryColor { get; set; } + public string SecondaryColor { get; set; } + public DateTime DateCreation { get; set; } + } +} diff --git a/Manager.Interfaces/DTO/RessourceDTO.cs b/Manager.Interfaces/DTO/RessourceDTO.cs new file mode 100644 index 0000000..e04e32f --- /dev/null +++ b/Manager.Interfaces/DTO/RessourceDTO.cs @@ -0,0 +1,14 @@ +using Manager.Interfaces.Models; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.DTO +{ + public class RessourceDTO + { + public string Id { get; set; } + public RessourceType Type { get; set; } + public string Label { get; set; } + } +} diff --git a/Manager.Interfaces/DTO/RessourceDetailDTO.cs b/Manager.Interfaces/DTO/RessourceDetailDTO.cs new file mode 100644 index 0000000..faeb7ed --- /dev/null +++ b/Manager.Interfaces/DTO/RessourceDetailDTO.cs @@ -0,0 +1,16 @@ +using Manager.Interfaces.Models; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.DTO +{ + public class RessourceDetailDTO + { + public string Id { get; set; } + public RessourceType Type { get; set; } + public string Label { get; set; } + public DateTime DateCreation { get; set; } + public string Data { get; set; } + } +} diff --git a/Manager.Interfaces/DTO/SectionDTO.cs b/Manager.Interfaces/DTO/SectionDTO.cs new file mode 100644 index 0000000..ee2558f --- /dev/null +++ b/Manager.Interfaces/DTO/SectionDTO.cs @@ -0,0 +1,18 @@ +using Manager.Interfaces.Models; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.DTO +{ + public class SectionDTO + { + public string Id { get; set; } + public string Label { get; set; } // Dictionary with all languages + public string ImageId { get; set; } // == RessourceId + public string Data { get; set; } // == RessourceId + public bool IsSubSection { get; set; } // true if part of menu type + public string ParentId { get; set; } // only if it's an subsection + public SectionType Type { get; set; } // !! If IsSubSection == true => Type can't not be menu ! + } +} diff --git a/Manager.Interfaces/DTO/SubSection/Map.cs b/Manager.Interfaces/DTO/SubSection/Map.cs new file mode 100644 index 0000000..b3f1932 --- /dev/null +++ b/Manager.Interfaces/DTO/SubSection/Map.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.DTO.SubSection +{ + public class Map + { + public int Zoom { get; set; } // Default = 18 + public MapType Type { get; set; } // Default = Hybrid + public List Points { get; set; } + public string Icon { get; set; } // url to ressource id (local) or on internet + } + + public class GeoPoint + { + public int Id { get; set; } + public string Title { get; set; } // Dictionary with all languages + public string Description { get; set; } // Dictionary with all languages + public string Image { get; set; } // url to ressource id (local) or on internet + public string Text { get; set; } // Dictionary with all languages + public string Latitude { get; set; } + public string Longitude { get; set; } + } + + public enum MapType { + none, + normal, + satellite, + terrain, + hybrid + } +} diff --git a/Manager.Interfaces/DTO/SubSection/Menu.cs b/Manager.Interfaces/DTO/SubSection/Menu.cs new file mode 100644 index 0000000..102bfd4 --- /dev/null +++ b/Manager.Interfaces/DTO/SubSection/Menu.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.DTO.SubSection +{ + public class Menu + { + public string Title { get; set; } // Dictionary with all languages + public List Sections { get; set; } + } +} diff --git a/Manager.Interfaces/DTO/SubSection/Slider.cs b/Manager.Interfaces/DTO/SubSection/Slider.cs new file mode 100644 index 0000000..906e6ec --- /dev/null +++ b/Manager.Interfaces/DTO/SubSection/Slider.cs @@ -0,0 +1,18 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.DTO.SubSection +{ + public class Slider + { + public string Title { get; set; } // Dictionary with all languages + public List Images { get; set; } + } + + public class Image { + public string Title { get; set; } // Dictionary with all languages + public string Description { get; set; } // Dictionary with all languages + public string Source { get; set; } // url to ressource id (local) or on internet + } +} diff --git a/Manager.Interfaces/DTO/SubSection/Video.cs b/Manager.Interfaces/DTO/SubSection/Video.cs new file mode 100644 index 0000000..25a37e8 --- /dev/null +++ b/Manager.Interfaces/DTO/SubSection/Video.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.DTO.SubSection +{ + public class Video + { + public string Title { get; set; } // Dictionary with all languages + public string Source { get; set; } // url to ressource id (local) or on internet + } +} diff --git a/Manager.Interfaces/DTO/SubSection/Web.cs b/Manager.Interfaces/DTO/SubSection/Web.cs new file mode 100644 index 0000000..ede322e --- /dev/null +++ b/Manager.Interfaces/DTO/SubSection/Web.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.DTO.SubSection +{ + public class Web + { + public string Title { get; set; } // Dictionary with all languages + public string Source { get; set; } // url to ressource id (local) or on internet + } +} diff --git a/Manager.Interfaces/Models/Display.cs b/Manager.Interfaces/Models/Display.cs new file mode 100644 index 0000000..d85ff2c --- /dev/null +++ b/Manager.Interfaces/Models/Display.cs @@ -0,0 +1,49 @@ +using Manager.Interfaces.DTO; +using MongoDB.Bson.Serialization.Attributes; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.Models +{ + /// + /// Display Information + /// + public class Display + { + [BsonId] + [BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)] + public string Id { get; set; } + + [BsonElement("Label")] + [BsonRequired] + public string Label { get; set; } + + [BsonElement("PrimaryColor")] + public string PrimaryColor { get; set; } + + [BsonElement("SecondaryColor")] + public string SecondaryColor { get; set; } + + [BsonElement("SectionIds")] + public List SectionIds { get; set; } + + [BsonElement("Languages")] + public List Languages { get; set; } // fr, en, de, nl => Sélection dans une liste déjà établie dans l'application ! + + [BsonElement("DateCreation")] + public DateTime DateCreation { get; set; } + + public DisplayDTO ToDTO() + { + return new DisplayDTO() + { + Id = Id, + Label = Label, + DateCreation = DateCreation, + PrimaryColor = PrimaryColor, + SecondaryColor = SecondaryColor + }; + } + } +} diff --git a/Manager.Interfaces/Models/Ressource.cs b/Manager.Interfaces/Models/Ressource.cs new file mode 100644 index 0000000..6859d2b --- /dev/null +++ b/Manager.Interfaces/Models/Ressource.cs @@ -0,0 +1,65 @@ +using Manager.Interfaces.DTO; +using MongoDB.Bson.Serialization.Attributes; +using System; +using System.Collections.Generic; +using System.Text; + + +namespace Manager.Interfaces.Models +{ + /// + /// Ressource Information + /// + public class Ressource + { + [BsonId] + [BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)] + public string Id { get; set; } + + [BsonElement("Type")] + [BsonRequired] + public RessourceType Type { get; set; } + + [BsonElement("Label")] + [BsonRequired] + public string Label { get; set; } + + [BsonElement("DateCreation")] + public DateTime DateCreation { get; set; } + + [BsonElement("Data")] + [BsonRequired] + public string Data { get; set; } + + public RessourceDTO ToDTO() + { + return new RessourceDTO() + { + Id = Id, + Label = Label, + Type = Type + }; + } + + public RessourceDetailDTO ToDetailDTO() + { + return new RessourceDetailDTO() + { + Id = Id, + Label = Label, + Type = Type, + Data = Data, + DateCreation = DateCreation + }; + } + + } + + public enum RessourceType + { + Image, + Video, + ImageUrl, + VideoUrl + } +} diff --git a/Manager.Interfaces/Models/Section.cs b/Manager.Interfaces/Models/Section.cs new file mode 100644 index 0000000..d797477 --- /dev/null +++ b/Manager.Interfaces/Models/Section.cs @@ -0,0 +1,64 @@ +using Manager.Interfaces.DTO; +using MongoDB.Bson.Serialization.Attributes; +using System; +using System.Collections.Generic; +using System.Text; + + +namespace Manager.Interfaces.Models +{ + /// + /// Section Information + /// + public class Section + { + [BsonId] + [BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)] + public string Id { get; set; } + + [BsonElement("Label")] + [BsonRequired] + public string Label { get; set; } // Dictionary with all languages + + [BsonElement("ImageId")] + [BsonRequired] + public string ImageId { get; set; } + + [BsonElement("Type")] + [BsonRequired] + public SectionType Type { get; set; } + + [BsonElement("IsSubSection")] + [BsonRequired] + public bool IsSubSection { get; set; } + + [BsonElement("ParentId")] + public string ParentId { get; set; } // only if it's an subsection + + [BsonElement("Data")] + public string Data { get; set; } // Json encapsulated info + + [BsonElement("DateCreation")] + public DateTime DateCreation { get; set; } + + public SectionDTO ToDTO() + { + return new SectionDTO() + { + Id = Id, + Label = Label, + Type = Type, + ImageId = ImageId + }; + } + } + + public enum SectionType + { + Map, + Slider, + Video, + Web, + Menu + } +} diff --git a/ManagerService/Controllers/DisplayController.cs b/ManagerService/Controllers/DisplayController.cs new file mode 100644 index 0000000..df12c1e --- /dev/null +++ b/ManagerService/Controllers/DisplayController.cs @@ -0,0 +1,209 @@ +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("Display", Description = "Display management")] + public class DisplayController : ControllerBase + { + private DisplayDatabaseService _displayService; + private readonly ILogger _logger; + + public DisplayController(ILogger logger, DisplayDatabaseService displayService) + { + _logger = logger; + _displayService = displayService; + } + + /// + /// Get a list of all display (summary) + /// + [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(typeof(string), 500)] + [HttpGet] + public ObjectResult Get() + { + try + { + List displays = _displayService.GetAll(); + + return new OkObjectResult(displays.Select(r => r.ToDTO())); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + + /// + /// Get a specific display + /// + /// id display + [AllowAnonymous] + [ProducesResponseType(typeof(DisplayDTO), 200)] + [ProducesResponseType(typeof(string), 404)] + [ProducesResponseType(typeof(string), 500)] + [HttpGet("{id}")] + public ObjectResult GetDetail(string id) + { + try + { + Display display = _displayService.GetById(id); + + if (display == null) + throw new KeyNotFoundException("This display was not found"); + + return new OkObjectResult(display.ToDTO()); + } + catch (KeyNotFoundException ex) + { + return new NotFoundObjectResult(ex.Message) {}; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Create a new display + /// + /// New display info + [ProducesResponseType(typeof(RessourceDetailDTO), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 409)] + [ProducesResponseType(typeof(string), 500)] + [HttpPost] + public ObjectResult Create([FromBody] DisplayDTO newDisplay) + { + try + { + if (newDisplay == null) + throw new ArgumentNullException("Display param is null"); + + // Todo add some verification ? + Display display = new Display(); + display.Label = newDisplay.Label; + display.PrimaryColor = newDisplay.PrimaryColor; + display.SecondaryColor = newDisplay.SecondaryColor; + display.SectionIds = newDisplay.SectionIds; + display.DateCreation = DateTime.Now; + + Display displayCreated = _displayService.Create(display); + + return new OkObjectResult(displayCreated.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 display + /// + /// Display to update + [ProducesResponseType(typeof(DisplayDTO), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 404)] + [ProducesResponseType(typeof(string), 500)] + [HttpPut] + public ObjectResult Update([FromBody] DisplayDTO updatedDisplay) + { + try + { + if (updatedDisplay == null) + throw new ArgumentNullException("Display param is null"); + + Display display = _displayService.GetById(updatedDisplay.Id); + + if (display == null) + throw new KeyNotFoundException("Display does not exist"); + + // Todo add some verification ? + display.Label = updatedDisplay.Label; + display.PrimaryColor = updatedDisplay.PrimaryColor; + display.SecondaryColor = updatedDisplay.SecondaryColor; + display.SectionIds = updatedDisplay.SectionIds; + + Display displayModified = _displayService.Update(updatedDisplay.Id, display); + + return new OkObjectResult(displayModified.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 display + /// + /// Id of display 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("Display param is null"); + + if (!_displayService.IsExist(id)) + throw new KeyNotFoundException("Display does not exist"); + + _displayService.Remove(id); + + return new ObjectResult("The display 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 }; + } + } + } +} diff --git a/ManagerService/Controllers/RessourceController.cs b/ManagerService/Controllers/RessourceController.cs new file mode 100644 index 0000000..4f22449 --- /dev/null +++ b/ManagerService/Controllers/RessourceController.cs @@ -0,0 +1,247 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Drawing.Imaging; +using System.IO; +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("Ressource", Description = "Ressource management")] + public class RessourceController : ControllerBase + { + private RessourceDatabaseService _ressourceService; + private readonly ILogger _logger; + + public RessourceController(ILogger logger, RessourceDatabaseService ressourceService) + { + _logger = logger; + _ressourceService = ressourceService; + } + + /// + /// Get a list of all ressources (summary) + /// + [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(typeof(string), 500)] + [HttpGet] + public ObjectResult Get() + { + try + { + List ressources = _ressourceService.GetAll(); + + return new OkObjectResult(ressources.Select(r => r.ToDTO())); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + + /// + /// Get a specific ressource + /// + /// id ressource + [AllowAnonymous] + [ProducesResponseType(typeof(RessourceDetailDTO), 200)] + [ProducesResponseType(typeof(string), 404)] + [ProducesResponseType(typeof(string), 500)] + [HttpGet("{id}/detail")] + public ObjectResult GetDetail(string id) + { + try + { + Ressource ressource = _ressourceService.GetById(id); + + if (ressource == null) + throw new KeyNotFoundException("This ressource was not found"); + + return new OkObjectResult(ressource.ToDetailDTO()); + } + catch (KeyNotFoundException ex) + { + return new NotFoundObjectResult(ex.Message) {}; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Show a specific ressource (as a picture or video stream) + /// + /// id ressource + [AllowAnonymous] + [ProducesResponseType(typeof(FileStreamResult), 200)] + [ProducesResponseType(typeof(string), 404)] + [ProducesResponseType(typeof(string), 500)] + [HttpGet("{id}")] + public ActionResult Show(string id) + { + try + { + Ressource ressource = _ressourceService.GetById(id); + + if (ressource == null) + throw new KeyNotFoundException("This ressource was not found"); + + switch (ressource.Type) { + case RessourceType.Image: + return new FileStreamResult(System.IO.File.OpenRead(ressource.Data), "image/jpeg"); + case RessourceType.Video: + return PhysicalFile(@"d:\test\somemovie.mp4", "application/octet-stream"); // TODO ! + default: + return new OkObjectResult(ressource.ToDetailDTO()); + } + } + catch (KeyNotFoundException ex) + { + return new NotFoundObjectResult(ex.Message) { }; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Create a new ressource + /// + /// New ressource info + [ProducesResponseType(typeof(RessourceDetailDTO), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 409)] + [ProducesResponseType(typeof(string), 500)] + [HttpPost] + public ObjectResult Create([FromBody] RessourceDetailDTO newRessource) + { + try + { + if (newRessource == null) + throw new ArgumentNullException("Ressource param is null"); + + // Todo add some verification ? + Ressource ressource = new Ressource(); + ressource.Label = newRessource.Label; + ressource.Type = newRessource.Type; + ressource.DateCreation = DateTime.Now; + ressource.Data = newRessource.Data; + + Ressource ressourceCreated = _ressourceService.Create(ressource); + + return new OkObjectResult(ressourceCreated.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 ressource + /// + /// Ressource to update + [ProducesResponseType(typeof(RessourceDetailDTO), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 404)] + [ProducesResponseType(typeof(string), 500)] + [HttpPut] + public ObjectResult Update([FromBody] RessourceDetailDTO updatedRessource) + { + try + { + if (updatedRessource == null) + throw new ArgumentNullException("Ressource param is null"); + + Ressource ressource = _ressourceService.GetById(updatedRessource.Id); + + if (ressource == null) + throw new KeyNotFoundException("Ressource does not exist"); + + // Todo add some verification ? + ressource.Label = updatedRessource.Label; + ressource.Type = updatedRessource.Type; + ressource.Data = updatedRessource.Data; + + Ressource ressourceModified = _ressourceService.Update(updatedRessource.Id, ressource); + + return new OkObjectResult(ressourceModified.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 ressource + /// + /// Id of ressource 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("Ressource param is null"); + + if (!_ressourceService.IsExist(id)) + throw new KeyNotFoundException("Ressource does not exist"); + + _ressourceService.Remove(id); + + return new ObjectResult("The ressource 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 }; + } + } + } +} diff --git a/ManagerService/Controllers/SectionController.cs b/ManagerService/Controllers/SectionController.cs new file mode 100644 index 0000000..c426dcc --- /dev/null +++ b/ManagerService/Controllers/SectionController.cs @@ -0,0 +1,234 @@ +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 }; + } + } + } +} diff --git a/ManagerService/Controllers/UserController.cs b/ManagerService/Controllers/UserController.cs index 6b43e24..a0764c2 100644 --- a/ManagerService/Controllers/UserController.cs +++ b/ManagerService/Controllers/UserController.cs @@ -9,12 +9,14 @@ 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("User", Description = "User management")] public class UserController : ControllerBase { private UserDatabaseService _userService; @@ -57,7 +59,7 @@ namespace ManagerService.Controllers [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 500)] [HttpGet("{id}")] - public ObjectResult Get(string id) + public ObjectResult GetDetail(string id) { try { diff --git a/ManagerService/Services/DisplayDatabaseService.cs b/ManagerService/Services/DisplayDatabaseService.cs new file mode 100644 index 0000000..2fc2acb --- /dev/null +++ b/ManagerService/Services/DisplayDatabaseService.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Manager.Interfaces.Models; +using Microsoft.Extensions.Configuration; +using MongoDB.Driver; + +namespace Manager.Services +{ + public class DisplayDatabaseService + { + private readonly IMongoCollection _Displays; + + public DisplayDatabaseService(IConfiguration config) + { + var client = new MongoClient(config.GetConnectionString("TabletDb")); + var database = client.GetDatabase("TabletDb"); + _Displays = database.GetCollection("Displays"); + } + public List GetAll() + { + return _Displays.Find(d => true).ToList(); + } + + public Display GetById(string id) + { + return _Displays.Find(d => d.Id == id).FirstOrDefault(); + } + + public bool IsExist(string id) + { + return _Displays.Find(d => d.Id == id).FirstOrDefault() != null ? true : false; + } + + public Display Create(Display display) + { + _Displays.InsertOne(display); + return display; + } + + public Display Update(string id, Display displayIn) + { + _Displays.ReplaceOne(d => d.Id == id, displayIn); + return displayIn; + } + + public void Remove(string id) + { + _Displays.DeleteOne(d => d.Id == id); + } + + } +} diff --git a/ManagerService/Services/RessourceDatabaseService.cs b/ManagerService/Services/RessourceDatabaseService.cs new file mode 100644 index 0000000..be4e923 --- /dev/null +++ b/ManagerService/Services/RessourceDatabaseService.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Manager.Interfaces.Models; +using Microsoft.Extensions.Configuration; +using MongoDB.Driver; + +namespace Manager.Services +{ + public class RessourceDatabaseService + { + private readonly IMongoCollection _Ressources; + + public RessourceDatabaseService(IConfiguration config) + { + var client = new MongoClient(config.GetConnectionString("TabletDb")); + var database = client.GetDatabase("TabletDb"); + _Ressources = database.GetCollection("Ressources"); + } + public List GetAll() + { + return _Ressources.Find(r => true).ToList(); + } + + public Ressource GetByType(RessourceType type) + { + return _Ressources.Find(r => r.Type == type).FirstOrDefault(); + } + + public Ressource GetById(string id) + { + return _Ressources.Find(r => r.Id == id).FirstOrDefault(); + } + + public bool IsExist(string id) + { + return _Ressources.Find(r => r.Id == id).FirstOrDefault() != null ? true : false; + } + + public Ressource Create(Ressource ressource) + { + _Ressources.InsertOne(ressource); + return ressource; + } + + public Ressource Update(string id, Ressource ressourceIn) + { + _Ressources.ReplaceOne(r => r.Id == id, ressourceIn); + return ressourceIn; + } + + public void Remove(string id) + { + _Ressources.DeleteOne(r => r.Id == id); + } + + } +} diff --git a/ManagerService/Services/SectionDatabaseService.cs b/ManagerService/Services/SectionDatabaseService.cs new file mode 100644 index 0000000..44c24ba --- /dev/null +++ b/ManagerService/Services/SectionDatabaseService.cs @@ -0,0 +1,59 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using Manager.Interfaces.Models; +using Microsoft.Extensions.Configuration; +using MongoDB.Driver; + +namespace Manager.Services +{ + public class SectionDatabaseService + { + private readonly IMongoCollection
_Sections; + + public SectionDatabaseService(IConfiguration config) + { + var client = new MongoClient(config.GetConnectionString("TabletDb")); + var database = client.GetDatabase("TabletDb"); + _Sections = database.GetCollection
("Sections"); + } + public List
GetAll() + { + return _Sections.Find(s => !s.IsSubSection).ToList(); + } + + public List
GetAllSubSection(string parentId) + { + return _Sections.Find(s => s.IsSubSection && s.ParentId == parentId).ToList(); + } + + public Section GetById(string id) + { + return _Sections.Find
(s => s.Id == id).FirstOrDefault(); + } + + public bool IsExist(string id) + { + return _Sections.Find
(s => s.Id == id).FirstOrDefault() != null ? true : false; + } + + public Section Create(Section section) + { + _Sections.InsertOne(section); + return section; + } + + public Section Update(string id, Section sectionIn) + { + _Sections.ReplaceOne(s => s.Id == id, sectionIn); + return sectionIn; + } + + public void Remove(string id) + { + _Sections.DeleteOne(s => s.Id == id); + } + + } +}