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
{
public class DisplayDTO
public class ConfigurationDTO
{
public string Id { 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 Description { get; set; } // Dictionary<string, object> with all languages
public string ImageId { get; set; } // == RessourceId
public string ConfigurationId { get; set; }
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 !

View File

@ -7,9 +7,9 @@ using System.Text;
namespace Manager.Interfaces.Models
{
/// <summary>
/// Display Information
/// Configuration Information
/// </summary>
public class Display
public class Configuration
{
[BsonId]
[BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)]
@ -26,7 +26,7 @@ namespace Manager.Interfaces.Models
public string SecondaryColor { get; set; }
[BsonElement("SectionIds")]
public List<string> SectionIds { get; set; }
public List<string> SectionIds { get; set; } // Get children
[BsonElement("Languages")]
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")]
public DateTime DateCreation { get; set; }
public DisplayDTO ToDTO()
public ConfigurationDTO ToDTO()
{
return new DisplayDTO()
return new ConfigurationDTO()
{
Id = Id,
Label = Label,
DateCreation = DateCreation,
PrimaryColor = PrimaryColor,
Languages = Languages,
SectionIds = SectionIds, // Maybe get direct from db
SecondaryColor = SecondaryColor
};
}

View File

@ -23,6 +23,10 @@ namespace Manager.Interfaces.Models
[BsonElement("Description")]
public string Description { get; set; } // Dictionary<string, object> with all languages
[BsonElement("ConfigurationId")]
[BsonRequired]
public string ConfigurationId { get; set; } // Parent id
[BsonElement("ImageId")]
[BsonRequired]
public string ImageId { get; set; }
@ -51,8 +55,14 @@ namespace Manager.Interfaces.Models
{
Id = Id,
Label = Label,
Description = Description,
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")
[ApiController, Route("api/[controller]")]
[OpenApiTag("Display", Description = "Display management")]
public class DisplayController : ControllerBase
[OpenApiTag("Configuration", Description = "Configuration management")]
public class ConfigurationController : ControllerBase
{
private DisplayDatabaseService _displayService;
private readonly ILogger<DisplayController> _logger;
private ConfigurationDatabaseService _configurationService;
private readonly ILogger<ConfigurationController> _logger;
public DisplayController(ILogger<DisplayController> logger, DisplayDatabaseService displayService)
public ConfigurationController(ILogger<ConfigurationController> logger, ConfigurationDatabaseService configurationService)
{
_logger = logger;
_displayService = displayService;
_configurationService = configurationService;
}
/// <summary>
/// Get a list of all display (summary)
/// Get a list of all configuration (summary)
/// </summary>
[ProducesResponseType(typeof(List<DisplayDTO>), 200)]
[ProducesResponseType(typeof(List<ConfigurationDTO>), 200)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet]
public ObjectResult Get()
{
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)
{
@ -49,11 +49,11 @@ namespace ManagerService.Controllers
/// <summary>
/// Get a specific display
/// Get a specific display configuration
/// </summary>
/// <param name="id">id display</param>
/// <param name="id">id configuration</param>
[AllowAnonymous]
[ProducesResponseType(typeof(DisplayDTO), 200)]
[ProducesResponseType(typeof(ConfigurationDTO), 200)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpGet("{id}")]
@ -61,12 +61,12 @@ namespace ManagerService.Controllers
{
try
{
Display display = _displayService.GetById(id);
Configuration configuration = _configurationService.GetById(id);
if (display == null)
throw new KeyNotFoundException("This display was not found");
if (configuration == null)
throw new KeyNotFoundException("This configuration was not found");
return new OkObjectResult(display.ToDTO());
return new OkObjectResult(configuration.ToDTO());
}
catch (KeyNotFoundException ex)
{
@ -79,33 +79,33 @@ namespace ManagerService.Controllers
}
/// <summary>
/// Create a new display
/// Create a new configuration
/// </summary>
/// <param name="newDisplay">New display info</param>
/// <param name="newConfiguration">New configuration info</param>
[ProducesResponseType(typeof(RessourceDetailDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 409)]
[ProducesResponseType(typeof(string), 500)]
[HttpPost]
public ObjectResult Create([FromBody] DisplayDTO newDisplay)
public ObjectResult Create([FromBody] ConfigurationDTO newConfiguration)
{
try
{
if (newDisplay == null)
throw new ArgumentNullException("Display param is null");
if (newConfiguration == null)
throw new ArgumentNullException("Configuration 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.Languages = newDisplay.Languages;
display.DateCreation = DateTime.Now;
Configuration configuration = new Configuration();
configuration.Label = newConfiguration.Label;
configuration.PrimaryColor = newConfiguration.PrimaryColor;
configuration.SecondaryColor = newConfiguration.SecondaryColor;
configuration.SectionIds = newConfiguration.SectionIds;
configuration.Languages = newConfiguration.Languages;
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)
{
@ -123,36 +123,36 @@ namespace ManagerService.Controllers
/// <summary>
/// Update a display
/// Update a configuration
/// </summary>
/// <param name="updatedDisplay">Display to update</param>
[ProducesResponseType(typeof(DisplayDTO), 200)]
/// <param name="updatedConfiguration">Configuration to update</param>
[ProducesResponseType(typeof(ConfigurationDTO), 200)]
[ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 500)]
[HttpPut]
public ObjectResult Update([FromBody] DisplayDTO updatedDisplay)
public ObjectResult Update([FromBody] ConfigurationDTO updatedConfiguration)
{
try
{
if (updatedDisplay == null)
throw new ArgumentNullException("Display param is null");
if (updatedConfiguration == null)
throw new ArgumentNullException("configuration param is null");
Display display = _displayService.GetById(updatedDisplay.Id);
Configuration configuration = _configurationService.GetById(updatedConfiguration.Id);
if (display == null)
throw new KeyNotFoundException("Display does not exist");
if (configuration == null)
throw new KeyNotFoundException("Configuration does not exist");
// Todo add some verification ?
display.Label = updatedDisplay.Label;
display.PrimaryColor = updatedDisplay.PrimaryColor;
display.SecondaryColor = updatedDisplay.SecondaryColor;
display.Languages = updatedDisplay.Languages;
display.SectionIds = updatedDisplay.SectionIds;
configuration.Label = updatedConfiguration.Label;
configuration.PrimaryColor = updatedConfiguration.PrimaryColor;
configuration.SecondaryColor = updatedConfiguration.SecondaryColor;
configuration.Languages = updatedConfiguration.Languages;
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)
{
@ -170,9 +170,9 @@ namespace ManagerService.Controllers
/// <summary>
/// Delete a display
/// Delete a configuration
/// </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), 400)]
[ProducesResponseType(typeof(string), 404)]
@ -183,14 +183,14 @@ namespace ManagerService.Controllers
try
{
if (id == null)
throw new ArgumentNullException("Display param is null");
throw new ArgumentNullException("Configuration param is null");
if (!_displayService.IsExist(id))
throw new KeyNotFoundException("Display does not exist");
if (!_configurationService.IsExist(id))
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)

View File

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

View File

@ -1,6 +1,7 @@
{
"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": {
"LogLevel": {