Display to configuration + update dtos + appsettings mongodb connection string

This commit is contained in:
Thomas Fransolet 2021-04-10 22:56:42 +02:00
parent 9e54ba1886
commit c5b025af1e
10 changed files with 141 additions and 118 deletions

View File

@ -4,7 +4,7 @@ using System.Text;
namespace Manager.Interfaces.DTO namespace Manager.Interfaces.DTO
{ {
public class DisplayDTO public class ConfigurationDTO
{ {
public string Id { get; set; } public string Id { get; set; }
public string Label { get; set; } public string Label { get; set; }

View File

@ -11,6 +11,7 @@ namespace Manager.Interfaces.DTO
public string Label { get; set; } // Dictionary<string, object> with all languages public string Label { get; set; } // Dictionary<string, object> with all languages
public string Description { get; set; } // Dictionary<string, object> with all languages public string Description { get; set; } // Dictionary<string, object> with all languages
public string ImageId { get; set; } // == RessourceId public string ImageId { get; set; } // == RessourceId
public string ConfigurationId { get; set; }
public bool IsSubSection { get; set; } // true if part of menu type public bool IsSubSection { get; set; } // true if part of menu type
public string ParentId { get; set; } // only if it's an subsection 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 ! public SectionType Type { get; set; } // !! If IsSubSection == true => Type can't not be menu !

View File

@ -7,9 +7,9 @@ using System.Text;
namespace Manager.Interfaces.Models namespace Manager.Interfaces.Models
{ {
/// <summary> /// <summary>
/// Display Information /// Configuration Information
/// </summary> /// </summary>
public class Display public class Configuration
{ {
[BsonId] [BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)] [BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
@ -26,7 +26,7 @@ namespace Manager.Interfaces.Models
public string SecondaryColor { get; set; } public string SecondaryColor { get; set; }
[BsonElement("SectionIds")] [BsonElement("SectionIds")]
public List<string> SectionIds { get; set; } public List<string> SectionIds { get; set; } // Get children
[BsonElement("Languages")] [BsonElement("Languages")]
public List<string> Languages { get; set; } // fr, en, de, nl => Sélection dans une liste déjà établie dans l'application ! public List<string> Languages { get; set; } // fr, en, de, nl => Sélection dans une liste déjà établie dans l'application !
@ -34,14 +34,16 @@ namespace Manager.Interfaces.Models
[BsonElement("DateCreation")] [BsonElement("DateCreation")]
public DateTime DateCreation { get; set; } public DateTime DateCreation { get; set; }
public DisplayDTO ToDTO() public ConfigurationDTO ToDTO()
{ {
return new DisplayDTO() return new ConfigurationDTO()
{ {
Id = Id, Id = Id,
Label = Label, Label = Label,
DateCreation = DateCreation, DateCreation = DateCreation,
PrimaryColor = PrimaryColor, PrimaryColor = PrimaryColor,
Languages = Languages,
SectionIds = SectionIds, // Maybe get direct from db
SecondaryColor = SecondaryColor SecondaryColor = SecondaryColor
}; };
} }

View File

@ -23,6 +23,10 @@ namespace Manager.Interfaces.Models
[BsonElement("Description")] [BsonElement("Description")]
public string Description { get; set; } // Dictionary<string, object> with all languages public string Description { get; set; } // Dictionary<string, object> with all languages
[BsonElement("ConfigurationId")]
[BsonRequired]
public string ConfigurationId { get; set; } // Parent id
[BsonElement("ImageId")] [BsonElement("ImageId")]
[BsonRequired] [BsonRequired]
public string ImageId { get; set; } public string ImageId { get; set; }
@ -51,8 +55,14 @@ namespace Manager.Interfaces.Models
{ {
Id = Id, Id = Id,
Label = Label, Label = Label,
Description = Description,
Type = Type, Type = Type,
ImageId = ImageId ImageId = ImageId,
ConfigurationId = ConfigurationId,
IsSubSection = IsSubSection,
ParentId = ParentId,
Data = Data,
DateCreation = DateCreation
}; };
} }
} }

View File

@ -15,31 +15,31 @@ namespace ManagerService.Controllers
{ {
[Authorize] // TODO Add ROLES (Roles = "Admin") [Authorize] // TODO Add ROLES (Roles = "Admin")
[ApiController, Route("api/[controller]")] [ApiController, Route("api/[controller]")]
[OpenApiTag("Display", Description = "Display management")] [OpenApiTag("Configuration", Description = "Configuration management")]
public class DisplayController : ControllerBase public class ConfigurationController : ControllerBase
{ {
private DisplayDatabaseService _displayService; private ConfigurationDatabaseService _configurationService;
private readonly ILogger<DisplayController> _logger; private readonly ILogger<ConfigurationController> _logger;
public DisplayController(ILogger<DisplayController> logger, DisplayDatabaseService displayService) public ConfigurationController(ILogger<ConfigurationController> logger, ConfigurationDatabaseService configurationService)
{ {
_logger = logger; _logger = logger;
_displayService = displayService; _configurationService = configurationService;
} }
/// <summary> /// <summary>
/// Get a list of all display (summary) /// Get a list of all configuration (summary)
/// </summary> /// </summary>
[ProducesResponseType(typeof(List<DisplayDTO>), 200)] [ProducesResponseType(typeof(List<ConfigurationDTO>), 200)]
[ProducesResponseType(typeof(string), 500)] [ProducesResponseType(typeof(string), 500)]
[HttpGet] [HttpGet]
public ObjectResult Get() public ObjectResult Get()
{ {
try try
{ {
List<Display> displays = _displayService.GetAll(); List<Configuration> configurations = _configurationService.GetAll();
return new OkObjectResult(displays.Select(r => r.ToDTO())); return new OkObjectResult(configurations.Select(r => r.ToDTO()));
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -49,11 +49,11 @@ namespace ManagerService.Controllers
/// <summary> /// <summary>
/// Get a specific display /// Get a specific display configuration
/// </summary> /// </summary>
/// <param name="id">id display</param> /// <param name="id">id configuration</param>
[AllowAnonymous] [AllowAnonymous]
[ProducesResponseType(typeof(DisplayDTO), 200)] [ProducesResponseType(typeof(ConfigurationDTO), 200)]
[ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)] [ProducesResponseType(typeof(string), 500)]
[HttpGet("{id}")] [HttpGet("{id}")]
@ -61,12 +61,12 @@ namespace ManagerService.Controllers
{ {
try try
{ {
Display display = _displayService.GetById(id); Configuration configuration = _configurationService.GetById(id);
if (display == null) if (configuration == null)
throw new KeyNotFoundException("This display was not found"); throw new KeyNotFoundException("This configuration was not found");
return new OkObjectResult(display.ToDTO()); return new OkObjectResult(configuration.ToDTO());
} }
catch (KeyNotFoundException ex) catch (KeyNotFoundException ex)
{ {
@ -79,33 +79,33 @@ namespace ManagerService.Controllers
} }
/// <summary> /// <summary>
/// Create a new display /// Create a new configuration
/// </summary> /// </summary>
/// <param name="newDisplay">New display info</param> /// <param name="newConfiguration">New configuration info</param>
[ProducesResponseType(typeof(RessourceDetailDTO), 200)] [ProducesResponseType(typeof(RessourceDetailDTO), 200)]
[ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 409)] [ProducesResponseType(typeof(string), 409)]
[ProducesResponseType(typeof(string), 500)] [ProducesResponseType(typeof(string), 500)]
[HttpPost] [HttpPost]
public ObjectResult Create([FromBody] DisplayDTO newDisplay) public ObjectResult Create([FromBody] ConfigurationDTO newConfiguration)
{ {
try try
{ {
if (newDisplay == null) if (newConfiguration == null)
throw new ArgumentNullException("Display param is null"); throw new ArgumentNullException("Configuration param is null");
// Todo add some verification ? // Todo add some verification ?
Display display = new Display(); Configuration configuration = new Configuration();
display.Label = newDisplay.Label; configuration.Label = newConfiguration.Label;
display.PrimaryColor = newDisplay.PrimaryColor; configuration.PrimaryColor = newConfiguration.PrimaryColor;
display.SecondaryColor = newDisplay.SecondaryColor; configuration.SecondaryColor = newConfiguration.SecondaryColor;
display.SectionIds = newDisplay.SectionIds; configuration.SectionIds = newConfiguration.SectionIds;
display.Languages = newDisplay.Languages; configuration.Languages = newConfiguration.Languages;
display.DateCreation = DateTime.Now; configuration.DateCreation = DateTime.Now;
Display displayCreated = _displayService.Create(display); Configuration configurationCreated = _configurationService.Create(configuration);
return new OkObjectResult(displayCreated.ToDTO()); return new OkObjectResult(configurationCreated.ToDTO());
} }
catch (ArgumentNullException ex) catch (ArgumentNullException ex)
{ {
@ -123,36 +123,36 @@ namespace ManagerService.Controllers
/// <summary> /// <summary>
/// Update a display /// Update a configuration
/// </summary> /// </summary>
/// <param name="updatedDisplay">Display to update</param> /// <param name="updatedConfiguration">Configuration to update</param>
[ProducesResponseType(typeof(DisplayDTO), 200)] [ProducesResponseType(typeof(ConfigurationDTO), 200)]
[ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)] [ProducesResponseType(typeof(string), 500)]
[HttpPut] [HttpPut]
public ObjectResult Update([FromBody] DisplayDTO updatedDisplay) public ObjectResult Update([FromBody] ConfigurationDTO updatedConfiguration)
{ {
try try
{ {
if (updatedDisplay == null) if (updatedConfiguration == null)
throw new ArgumentNullException("Display param is null"); throw new ArgumentNullException("configuration param is null");
Display display = _displayService.GetById(updatedDisplay.Id); Configuration configuration = _configurationService.GetById(updatedConfiguration.Id);
if (display == null) if (configuration == null)
throw new KeyNotFoundException("Display does not exist"); throw new KeyNotFoundException("Configuration does not exist");
// Todo add some verification ? // Todo add some verification ?
display.Label = updatedDisplay.Label; configuration.Label = updatedConfiguration.Label;
display.PrimaryColor = updatedDisplay.PrimaryColor; configuration.PrimaryColor = updatedConfiguration.PrimaryColor;
display.SecondaryColor = updatedDisplay.SecondaryColor; configuration.SecondaryColor = updatedConfiguration.SecondaryColor;
display.Languages = updatedDisplay.Languages; configuration.Languages = updatedConfiguration.Languages;
display.SectionIds = updatedDisplay.SectionIds; configuration.SectionIds = updatedConfiguration.SectionIds;
Display displayModified = _displayService.Update(updatedDisplay.Id, display); Configuration configurationModified = _configurationService.Update(updatedConfiguration.Id, configuration);
return new OkObjectResult(displayModified.ToDTO()); return new OkObjectResult(configurationModified.ToDTO());
} }
catch (ArgumentNullException ex) catch (ArgumentNullException ex)
{ {
@ -170,9 +170,9 @@ namespace ManagerService.Controllers
/// <summary> /// <summary>
/// Delete a display /// Delete a configuration
/// </summary> /// </summary>
/// <param name="id">Id of display to delete</param> /// <param name="id">Id of configuration to delete</param>
[ProducesResponseType(typeof(string), 202)] [ProducesResponseType(typeof(string), 202)]
[ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 404)]
@ -183,14 +183,14 @@ namespace ManagerService.Controllers
try try
{ {
if (id == null) if (id == null)
throw new ArgumentNullException("Display param is null"); throw new ArgumentNullException("Configuration param is null");
if (!_displayService.IsExist(id)) if (!_configurationService.IsExist(id))
throw new KeyNotFoundException("Display does not exist"); throw new KeyNotFoundException("Configuration does not exist");
_displayService.Remove(id); _configurationService.Remove(id);
return new ObjectResult("The display has been deleted") { StatusCode = 202 }; return new ObjectResult("The configuration has been deleted") { StatusCode = 202 };
} }
catch (ArgumentNullException ex) catch (ArgumentNullException ex)

View File

@ -18,12 +18,14 @@ namespace ManagerService.Controllers
public class SectionController : ControllerBase public class SectionController : ControllerBase
{ {
private SectionDatabaseService _sectionService; private SectionDatabaseService _sectionService;
private ConfigurationDatabaseService _configurationService;
private readonly ILogger<SectionController> _logger; private readonly ILogger<SectionController> _logger;
public SectionController(ILogger<SectionController> logger, SectionDatabaseService sectionService) public SectionController(ILogger<SectionController> logger, SectionDatabaseService sectionService, ConfigurationDatabaseService configurationService)
{ {
_logger = logger; _logger = logger;
_sectionService = sectionService; _sectionService = sectionService;
_configurationService = configurationService;
} }
/// <summary> /// <summary>
@ -145,10 +147,17 @@ namespace ManagerService.Controllers
if (newSection == null) if (newSection == null)
throw new ArgumentNullException("Section param is 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 ? // Todo add some verification ?
Section section = new Section(); Section section = new Section();
section.Label = newSection.Label; section.Label = newSection.Label;
section.ImageId = newSection.ImageId; section.ImageId = newSection.ImageId;
section.ConfigurationId = newSection.ConfigurationId;
section.DateCreation = DateTime.Now; section.DateCreation = DateTime.Now;
section.IsSubSection = false; section.IsSubSection = false;
section.ParentId = null; section.ParentId = null;

View File

@ -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 ConfigurationDatabaseService
{
private readonly IMongoCollection<Configuration> _Configurations;
public ConfigurationDatabaseService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("TabletDb"));
var database = client.GetDatabase("TabletDb");
_Configurations = database.GetCollection<Configuration>("Configurations");
}
public List<Configuration> GetAll()
{
return _Configurations.Find(d => true).ToList();
}
public Configuration GetById(string id)
{
return _Configurations.Find<Configuration>(d => d.Id == id).FirstOrDefault();
}
public bool IsExist(string id)
{
return _Configurations.Find<Configuration>(d => d.Id == id).FirstOrDefault() != null ? true : false;
}
public Configuration Create(Configuration configuration)
{
_Configurations.InsertOne(configuration);
return configuration;
}
public Configuration Update(string id, Configuration configurationIn)
{
_Configurations.ReplaceOne(d => d.Id == id, configurationIn);
return configurationIn;
}
public void Remove(string id)
{
_Configurations.DeleteOne(d => d.Id == id);
}
}
}

View File

@ -1,54 +0,0 @@
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<Display> _Displays;
public DisplayDatabaseService(IConfiguration config)
{
var client = new MongoClient(config.GetConnectionString("TabletDb"));
var database = client.GetDatabase("TabletDb");
_Displays = database.GetCollection<Display>("Displays");
}
public List<Display> GetAll()
{
return _Displays.Find(d => true).ToList();
}
public Display GetById(string id)
{
return _Displays.Find<Display>(d => d.Id == id).FirstOrDefault();
}
public bool IsExist(string id)
{
return _Displays.Find<Display>(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);
}
}
}

View File

@ -102,7 +102,7 @@ namespace ManagerService
services.AddScoped<TokensService>(); services.AddScoped<TokensService>();
services.AddScoped<UserDatabaseService>(); services.AddScoped<UserDatabaseService>();
services.AddScoped<SectionDatabaseService>(); services.AddScoped<SectionDatabaseService>();
services.AddScoped<DisplayDatabaseService>(); services.AddScoped<ConfigurationDatabaseService>();
services.AddScoped<RessourceDatabaseService>(); services.AddScoped<RessourceDatabaseService>();
} }

View File

@ -1,6 +1,7 @@
{ {
"ConnectionStrings": { "ConnectionStrings": {
"TabletDb": "mongodb://admin:MioTech4ever!@localhost:27017" //TO CHANGE //"TabletDb": "mongodb://admin:MioTech4ever!@localhost:27017", //DEV
"TabletDb": "mongodb://admin:mdlf2021!@192.168.31.96:27017" //PROD
}, },
"Logging": { "Logging": {
"LogLevel": { "LogLevel": {