diff --git a/Manager.Interfaces/DTO/DeviceDTO.cs b/Manager.Interfaces/DTO/DeviceDTO.cs index 51a091b..04f9d7a 100644 --- a/Manager.Interfaces/DTO/DeviceDTO.cs +++ b/Manager.Interfaces/DTO/DeviceDTO.cs @@ -9,7 +9,16 @@ namespace Manager.Interfaces.DTO public string Id { get; set; } public string Name { get; set; } public string IpAddress { get; set; } + public string ConfigurationId { get; set; } public bool Connected{ get; set; } public DateTime DateCreation{ get; set; } } + + public class DeviceDetailDTO : DeviceDTO + { + public string ConnectionLevel { get; set; } + public DateTime LastConnectionLevel { get; set; } + public string BatteryLevel { get; set; } + public DateTime LastBatteryLevel { get; set; } + } } diff --git a/Manager.Interfaces/DTO/SubSection/MapDTO.cs b/Manager.Interfaces/DTO/SubSection/MapDTO.cs index 0490e6b..a0ae931 100644 --- a/Manager.Interfaces/DTO/SubSection/MapDTO.cs +++ b/Manager.Interfaces/DTO/SubSection/MapDTO.cs @@ -16,8 +16,8 @@ namespace Manager.Interfaces.DTO public class GeoPointDTO { public int Id { get; set; } - public Dictionary Title { get; set; } // Dictionary with all languages - public Dictionary Description { get; set; } // Dictionary with all languages + 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 ImageType { get; set; } // url or ressource public string Text { get; set; } // Dictionary with all languages diff --git a/Manager.Interfaces/DTO/SubSection/MenuDTO.cs b/Manager.Interfaces/DTO/SubSection/MenuDTO.cs index ca1011f..f9cf955 100644 --- a/Manager.Interfaces/DTO/SubSection/MenuDTO.cs +++ b/Manager.Interfaces/DTO/SubSection/MenuDTO.cs @@ -4,7 +4,7 @@ using System.Text; namespace Manager.Interfaces.DTO { - public class MenuDTO : SectionDTO + public class MenuDTO { //public string Title { get; set; } // Dictionary with all languages public List Sections { get; set; } diff --git a/Manager.Interfaces/DTO/SubSection/SliderDTO.cs b/Manager.Interfaces/DTO/SubSection/SliderDTO.cs index a37b203..a05a597 100644 --- a/Manager.Interfaces/DTO/SubSection/SliderDTO.cs +++ b/Manager.Interfaces/DTO/SubSection/SliderDTO.cs @@ -4,7 +4,7 @@ using System.Text; namespace Manager.Interfaces.DTO { - public class SliderDTO : SectionDTO + public class SliderDTO { public List Images { get; set; } } diff --git a/Manager.Interfaces/DTO/SubSection/VideoDTO.cs b/Manager.Interfaces/DTO/SubSection/VideoDTO.cs index 137e73d..a34e8c3 100644 --- a/Manager.Interfaces/DTO/SubSection/VideoDTO.cs +++ b/Manager.Interfaces/DTO/SubSection/VideoDTO.cs @@ -4,7 +4,7 @@ using System.Text; namespace Manager.Interfaces.DTO { - public class VideoDTO : SectionDTO + public class VideoDTO { //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/WebDTO.cs b/Manager.Interfaces/DTO/SubSection/WebDTO.cs index 1ceb28b..40621a3 100644 --- a/Manager.Interfaces/DTO/SubSection/WebDTO.cs +++ b/Manager.Interfaces/DTO/SubSection/WebDTO.cs @@ -4,7 +4,7 @@ using System.Text; namespace Manager.Interfaces.DTO { - public class WebDTO : SectionDTO + public class WebDTO { //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/Device.cs b/Manager.Interfaces/Models/Device.cs index 921111b..191262c 100644 --- a/Manager.Interfaces/Models/Device.cs +++ b/Manager.Interfaces/Models/Device.cs @@ -56,6 +56,24 @@ namespace Manager.Interfaces.Models Name = Name, IpAddress = IpAddress, Connected = Connected, + ConfigurationId = ConfigurationId, + DateCreation = DateCreation + }; + } + + public DeviceDetailDTO ToDetailDTO() + { + return new DeviceDetailDTO() + { + Id = Id, + Name = Name, + IpAddress = IpAddress, + Connected = Connected, + ConfigurationId = ConfigurationId, + ConnectionLevel = ConnectionLevel, + LastConnectionLevel = LastConnectionLevel, + BatteryLevel = BatteryLevel, + LastBatteryLevel = LastBatteryLevel, DateCreation = DateCreation }; } diff --git a/Manager.Interfaces/Models/SubSection/Map.cs b/Manager.Interfaces/Models/SubSection/Map.cs index c62804f..39f5a3c 100644 --- a/Manager.Interfaces/Models/SubSection/Map.cs +++ b/Manager.Interfaces/Models/SubSection/Map.cs @@ -30,10 +30,10 @@ namespace Manager.Interfaces.Models { return new MapDTO() { - Id = Id, + /*Id = Id, Label = Label, Type = Type, - ImageId = ImageId, + ImageId = ImageId,*/ MapType = MapType, Zoom = Zoom, Points = Points.Select(p => p.ToDTO()).ToList(), diff --git a/Manager.Interfaces/Models/SubSection/Slider.cs b/Manager.Interfaces/Models/SubSection/Slider.cs index 4d94cef..d757862 100644 --- a/Manager.Interfaces/Models/SubSection/Slider.cs +++ b/Manager.Interfaces/Models/SubSection/Slider.cs @@ -20,10 +20,10 @@ namespace Manager.Interfaces.Models { return new SliderDTO() { - Id = Id, + /*Id = Id, Label = Label, Type = Type, - ImageId = ImageId, + ImageId = ImageId,*/ Images = Images.Select(p => p.ToDTO()).ToList(), }; } diff --git a/ManagerService/Controllers/DeviceController.cs b/ManagerService/Controllers/DeviceController.cs new file mode 100644 index 0000000..dea80f7 --- /dev/null +++ b/ManagerService/Controllers/DeviceController.cs @@ -0,0 +1,217 @@ +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") + [ApiController, Route("api/[controller]")] + [OpenApiTag("Device", Description = "Device management")] + public class DeviceController : ControllerBase + { + private DeviceDatabaseService _deviceService; + private readonly ILogger _logger; + + public DeviceController(ILogger logger, DeviceDatabaseService deviceService) + { + _logger = logger; + _deviceService = deviceService; + } + + /// + /// Get a list of all devices + /// + [ProducesResponseType(typeof(List), 200)] + [ProducesResponseType(typeof(string), 500)] + [HttpGet] + public ObjectResult Get() + { + try + { + List devices = _deviceService.GetAll(); + + return new OkObjectResult(devices.Select(d => d.ToDTO())); + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + + /// + /// Get a specific device + /// + /// id device + [AllowAnonymous] + [ProducesResponseType(typeof(DeviceDetailDTO), 200)] + [ProducesResponseType(typeof(string), 404)] + [ProducesResponseType(typeof(string), 500)] + [HttpGet("{id}/detail")] + public ObjectResult GetDetail(string id) + { + try + { + Device device = _deviceService.GetById(id); + + if (device == null) + throw new KeyNotFoundException("This device was not found"); + + return new OkObjectResult(device.ToDetailDTO()); + } + catch (KeyNotFoundException ex) + { + return new NotFoundObjectResult(ex.Message) { }; + } + catch (Exception ex) + { + return new ObjectResult(ex.Message) { StatusCode = 500 }; + } + } + + /// + /// Create a new device + /// + /// New device info + [ProducesResponseType(typeof(DeviceDetailDTO), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 409)] + [ProducesResponseType(typeof(string), 500)] + [HttpPost] + public ObjectResult Create([FromBody] DeviceDetailDTO newDevice) + { + try + { + if (newDevice == null) + throw new ArgumentNullException("Device param is null"); + + // Todo add some verification ? + Device device = new Device(); + device.Name = newDevice.Name; + device.IpAddress = newDevice.IpAddress; + device.Connected = newDevice.Connected; + device.ConnectionLevel = newDevice.ConnectionLevel; + device.LastConnectionLevel = newDevice.LastConnectionLevel; + device.BatteryLevel = newDevice.BatteryLevel; + device.LastBatteryLevel = newDevice.LastBatteryLevel; + device.DateCreation = DateTime.Now; + + Device deviceCreated = _deviceService.Create(device); + + return new OkObjectResult(deviceCreated.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 device + /// + /// Device to update + [ProducesResponseType(typeof(DeviceDetailDTO), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 404)] + [ProducesResponseType(typeof(string), 500)] + [HttpPut] + public ObjectResult Update([FromBody] DeviceDetailDTO updatedDevice) + { + try + { + if (updatedDevice == null) + throw new ArgumentNullException("Device param is null"); + + Device device = _deviceService.GetById(updatedDevice.Id); + + if (device == null) + throw new KeyNotFoundException("Device does not exist"); + + // Todo add some verification ? + device.Name = updatedDevice.Name; + device.IpAddress = updatedDevice.IpAddress; + device.Connected = updatedDevice.Connected; + device.ConnectionLevel = updatedDevice.ConnectionLevel; + device.LastConnectionLevel = updatedDevice.LastConnectionLevel; + device.BatteryLevel = updatedDevice.BatteryLevel; + device.LastBatteryLevel = updatedDevice.LastBatteryLevel; + + Device deviceModified = _deviceService.Update(updatedDevice.Id, device); + + return new OkObjectResult(deviceModified.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 device + /// + /// Id of device 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("Device param is null"); + + if (!_deviceService.IsExist(id)) + throw new KeyNotFoundException("Device does not exist"); + + _deviceService.Remove(id); + + return new ObjectResult("The device 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 index ed766d0..c2d3634 100644 --- a/ManagerService/Controllers/SectionController.cs +++ b/ManagerService/Controllers/SectionController.cs @@ -77,6 +77,35 @@ namespace ManagerService.Controllers } } + /// + /// 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 /// @@ -201,43 +230,49 @@ namespace ManagerService.Controllers section.ParentId = null; section.Type = newSection.Type; + // Preparation + Dictionary titles = new Dictionary(); + Dictionary descriptions = new Dictionary(); + List languages = _configurationService.GetById(newSection.ConfigurationId).Languages; + + var mapDTO = new MapDTO(); // For menu dto + var sliderDTO = new SliderDTO(); // For menu dto + + foreach (var language in languages) + { + switch (language.ToUpper()) + { + case "FR": + titles.Add(language, "Titre en français"); + descriptions.Add(language, "Description en français"); + break; + case "EN": + titles.Add(language, "Title in english"); + descriptions.Add(language, "Description en anglais"); + break; + case "NL": + titles.Add(language, "Titre in dutch"); + descriptions.Add(language, "Description en néerlandais"); + break; + case "DE": + titles.Add(language, "Titre en allemand"); + descriptions.Add(language, "Description en allemand"); + break; + } + } switch (newSection.Type) { case SectionType.Map: - MapDTO mapDTO = new MapDTO(); + mapDTO = new MapDTO(); mapDTO.Icon = ""; mapDTO.MapType = MapType.hybrid; mapDTO.Zoom = 18; - List languages = _configurationService.GetById(newSection.ConfigurationId).Languages; - Dictionary titles = new Dictionary(); - Dictionary descriptions = new Dictionary(); - - foreach (var language in languages) { - switch (language.ToUpper()) { - case "FR": - titles.Add(language, "Titre en français"); - descriptions.Add(language, "Description en français"); - break; - case "EN": - titles.Add(language, "Title in english"); - descriptions.Add(language, "Description en anglais"); - break; - case "NL": - titles.Add(language, "Titre in dutch"); - descriptions.Add(language, "Description en néerlandais"); - break; - case "DE": - titles.Add(language, "Titre en allemand"); - descriptions.Add(language, "Description en allemand"); - break; - } - } mapDTO.Points = new List() { new GeoPointDTO() { Id = 0, - Title = titles, - Description = descriptions, + Title = JsonConvert.SerializeObject(titles), + Description = JsonConvert.SerializeObject(descriptions), Image = "", // TODO sample ImageType = "url", // TODO Latitude = "50.416639", @@ -246,36 +281,59 @@ namespace ManagerService.Controllers } }; - section.Data = JsonConvert.SerializeObject(mapDTO); ; // Include all info from specific section as JSON + section.Data = JsonConvert.SerializeObject(mapDTO); // Include all info from specific section as JSON break; case SectionType.Slider: - section.Data = newSection.Data; // Include all info from specific section as JSON + sliderDTO = new SliderDTO(); + ImageDTO imageDTO = new ImageDTO(); + imageDTO.Title = JsonConvert.SerializeObject(titles); + imageDTO.Description = JsonConvert.SerializeObject(descriptions); + imageDTO.Source = ""; + sliderDTO.Images.Add(imageDTO); + + section.Data = JsonConvert.SerializeObject(sliderDTO); // Include all info from specific section as JSON break; case SectionType.Video: - section.Data = newSection.Data; // Include all info from specific section as JSON + VideoDTO videoDTO = new VideoDTO(); + videoDTO.Source = ""; + section.Data = JsonConvert.SerializeObject(videoDTO); // Include all info from specific section as JSON break; case SectionType.Web: - section.Data = newSection.Data; // Include all info from specific section as JSON + WebDTO webDTO = new WebDTO(); + webDTO.Source = ""; + section.Data = JsonConvert.SerializeObject(webDTO); // Include all info from specific section as JSON break; case SectionType.Menu: - section.Data = newSection.Data; // Include all info from specific section as JSON + 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; } - /*section.MapType = newSectionMap.MapType; - section.Zoom = newSectionMap.Zoom; - section.Icon = newSectionMap.Icon; - section.Points = newSectionMap.Points.Select(p => - new GeoPoint() { - Id = p.Id, - Title = p.Title, - Description = p.Description, - Image = p.Image, - Text = p.Text, - Latitude = p.Latitude, - Longitude = p.Longitude - }).ToList();*/ - Section sectionCreated = _sectionService.Create(section); return new OkObjectResult(sectionCreated.ToDTO()); diff --git a/ManagerService/Services/DeviceDatabaseService.cs b/ManagerService/Services/DeviceDatabaseService.cs new file mode 100644 index 0000000..2c049ac --- /dev/null +++ b/ManagerService/Services/DeviceDatabaseService.cs @@ -0,0 +1,60 @@ +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 DeviceDatabaseService + { + private readonly IMongoCollection _Devices; + + public DeviceDatabaseService(IConfiguration config) + { + var client = new MongoClient(config.GetConnectionString("TabletDb")); + var database = client.GetDatabase("TabletDb"); + _Devices = database.GetCollection("Devices"); + } + + public List GetAll() + { + return _Devices.Find(d => true).ToList(); + } + + public List GetAllConnected() + { + return _Devices.Find(d => d.Connected).ToList(); + } + + public Device GetById(string id) + { + return _Devices.Find(d => d.Id == id).FirstOrDefault(); + } + + public bool IsExist(string id) + { + return _Devices.Find(d => d.Id == id).FirstOrDefault() != null ? true : false; + } + + public Device Create(Device device) + { + _Devices.InsertOne(device); + return device; + } + + public Device Update(string id, Device deviceIn) + { + _Devices.ReplaceOne(d => d.Id == id, deviceIn); + return deviceIn; + } + + public void Remove(string id) + { + _Devices.DeleteOne(d => d.Id == id); + } + + } +} diff --git a/ManagerService/Services/SectionDatabaseService.cs b/ManagerService/Services/SectionDatabaseService.cs index c76c71c..01f78c2 100644 --- a/ManagerService/Services/SectionDatabaseService.cs +++ b/ManagerService/Services/SectionDatabaseService.cs @@ -61,5 +61,10 @@ namespace Manager.Services _Sections.DeleteOne(s => s.Id == id); } + public void DeleteAllFromConfiguration(string configurationId) + { + _Sections.DeleteMany(s => s.ConfigurationId == configurationId); + } + } } diff --git a/ManagerService/Startup.cs b/ManagerService/Startup.cs index 3d37f57..560c700 100644 --- a/ManagerService/Startup.cs +++ b/ManagerService/Startup.cs @@ -104,6 +104,7 @@ namespace ManagerService services.AddScoped(); services.AddScoped(); services.AddScoped(); + services.AddScoped(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.