From 1f8fe1b41543ae4d224fd0cc21d9f06aec55deee Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Fri, 30 Apr 2021 20:02:26 +0200 Subject: [PATCH] Add device dto and model + add get section from configId --- Manager.Interfaces/DTO/DeviceDTO.cs | 15 +++++ Manager.Interfaces/Models/Device.cs | 63 +++++++++++++++++++ .../Controllers/SectionController.cs | 37 +++++++++++ .../Services/SectionDatabaseService.cs | 6 ++ 4 files changed, 121 insertions(+) create mode 100644 Manager.Interfaces/DTO/DeviceDTO.cs create mode 100644 Manager.Interfaces/Models/Device.cs diff --git a/Manager.Interfaces/DTO/DeviceDTO.cs b/Manager.Interfaces/DTO/DeviceDTO.cs new file mode 100644 index 0000000..51a091b --- /dev/null +++ b/Manager.Interfaces/DTO/DeviceDTO.cs @@ -0,0 +1,15 @@ +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.DTO +{ + public class DeviceDTO + { + public string Id { get; set; } + public string Name { get; set; } + public string IpAddress { get; set; } + public bool Connected{ get; set; } + public DateTime DateCreation{ get; set; } + } +} diff --git a/Manager.Interfaces/Models/Device.cs b/Manager.Interfaces/Models/Device.cs new file mode 100644 index 0000000..921111b --- /dev/null +++ b/Manager.Interfaces/Models/Device.cs @@ -0,0 +1,63 @@ +using Manager.Interfaces.DTO; +using MongoDB.Bson.Serialization.Attributes; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Manager.Interfaces.Models +{ + /// + /// Device Information (Tablet) + /// + public class Device + { + [BsonId] + [BsonRepresentation(MongoDB.Bson.BsonType.ObjectId)] + public string Id { get; set; } + + [BsonElement("Name")] + public string Name { get; set; } + + [BsonElement("IpAddress")] + [BsonRequired] + public string IpAddress { get; set; } + + [BsonElement("ConfigurationId")] + [BsonRequired] + public string ConfigurationId { get; set; } + + [BsonElement("Connected")] + [BsonRequired] + public bool Connected { get; set; } + + [BsonElement("DateCreation")] + public DateTime DateCreation { get; set; } + + // BatteryLevel in case of powered devices + [BsonElement("BatteryLevel")] + public string BatteryLevel { get; set; } + + [BsonElement("LastBatteryLevel")] + public DateTime LastBatteryLevel { get; set; } + + // ConnectionLevel wifi strength level + [BsonElement("ConnectionLevel")] + public string ConnectionLevel { get; set; } + + [BsonElement("LastConnectionLevel")] + public DateTime LastConnectionLevel { get; set; } + + + public DeviceDTO ToDTO() + { + return new DeviceDTO() + { + Id = Id, + Name = Name, + IpAddress = IpAddress, + Connected = Connected, + DateCreation = DateCreation + }; + } + } +} \ No newline at end of file diff --git a/ManagerService/Controllers/SectionController.cs b/ManagerService/Controllers/SectionController.cs index 1cc8891..f7be695 100644 --- a/ManagerService/Controllers/SectionController.cs +++ b/ManagerService/Controllers/SectionController.cs @@ -48,21 +48,58 @@ namespace ManagerService.Controllers } } + /// + /// Get a list of all section from a specific configuration + /// + /// configuration id + [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"); + + List
sections = _sectionService.GetAllFromConfiguration(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 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 }; diff --git a/ManagerService/Services/SectionDatabaseService.cs b/ManagerService/Services/SectionDatabaseService.cs index 44c24ba..c76c71c 100644 --- a/ManagerService/Services/SectionDatabaseService.cs +++ b/ManagerService/Services/SectionDatabaseService.cs @@ -18,11 +18,17 @@ namespace Manager.Services var database = client.GetDatabase("TabletDb"); _Sections = database.GetCollection
("Sections"); } + public List
GetAll() { return _Sections.Find(s => !s.IsSubSection).ToList(); } + public List
GetAllFromConfiguration(string configurationId) + { + return _Sections.Find(s => !s.IsSubSection && s.ConfigurationId == configurationId).ToList(); + } + public List
GetAllSubSection(string parentId) { return _Sections.Find(s => s.IsSubSection && s.ParentId == parentId).ToList();