diff --git a/Manager.Interfaces/DTO/DeviceDTO.cs b/Manager.Interfaces/DTO/DeviceDTO.cs index 04f9d7a..bcaae24 100644 --- a/Manager.Interfaces/DTO/DeviceDTO.cs +++ b/Manager.Interfaces/DTO/DeviceDTO.cs @@ -10,6 +10,7 @@ namespace Manager.Interfaces.DTO public string Name { get; set; } public string IpAddress { get; set; } public string ConfigurationId { get; set; } + public string Configuration { 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 index 191262c..1c037cb 100644 --- a/Manager.Interfaces/Models/Device.cs +++ b/Manager.Interfaces/Models/Device.cs @@ -22,9 +22,12 @@ namespace Manager.Interfaces.Models [BsonRequired] public string IpAddress { get; set; } + [BsonElement("Configuration")] + public string Configuration { get; set; } + [BsonElement("ConfigurationId")] [BsonRequired] - public string ConfigurationId { get; set; } + public string ConfigurationId { get; set; } [BsonElement("Connected")] [BsonRequired] @@ -56,6 +59,7 @@ namespace Manager.Interfaces.Models Name = Name, IpAddress = IpAddress, Connected = Connected, + Configuration = Configuration, ConfigurationId = ConfigurationId, DateCreation = DateCreation }; @@ -69,6 +73,7 @@ namespace Manager.Interfaces.Models Name = Name, IpAddress = IpAddress, Connected = Connected, + Configuration = Configuration, ConfigurationId = ConfigurationId, ConnectionLevel = ConnectionLevel, LastConnectionLevel = LastConnectionLevel, diff --git a/ManagerService/Controllers/DeviceController.cs b/ManagerService/Controllers/DeviceController.cs index 6f0d33a..1388cee 100644 --- a/ManagerService/Controllers/DeviceController.cs +++ b/ManagerService/Controllers/DeviceController.cs @@ -22,12 +22,14 @@ namespace ManagerService.Controllers public class DeviceController : ControllerBase { private DeviceDatabaseService _deviceService; + private ConfigurationDatabaseService _configurationService; private readonly ILogger _logger; - public DeviceController(ILogger logger, DeviceDatabaseService deviceService) + public DeviceController(ILogger logger, DeviceDatabaseService deviceService, ConfigurationDatabaseService configurationService) { _logger = logger; _deviceService = deviceService; + _configurationService = configurationService; } /// @@ -87,6 +89,7 @@ namespace ManagerService.Controllers /// New device info [ProducesResponseType(typeof(DeviceDetailDTO), 200)] [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 404)] [ProducesResponseType(typeof(string), 409)] [ProducesResponseType(typeof(string), 500)] [HttpPost] @@ -97,9 +100,14 @@ namespace ManagerService.Controllers if (newDevice == null) throw new ArgumentNullException("Device param is null"); - // Todo add some verification ? + var configuration = _configurationService.GetById(newDevice.ConfigurationId); + + if (configuration == null) + throw new KeyNotFoundException("Configuration does not exist"); + Device device = new Device(); device.Name = newDevice.Name; + device.Configuration = configuration.Label; device.ConfigurationId = newDevice.ConfigurationId; device.IpAddress = newDevice.IpAddress; device.Connected = newDevice.Connected; @@ -117,6 +125,10 @@ namespace ManagerService.Controllers { return new BadRequestObjectResult(ex.Message) { }; } + catch (KeyNotFoundException ex) + { + return new NotFoundObjectResult(ex.Message) { }; + } catch (InvalidOperationException ex) { return new ConflictObjectResult(ex.Message) { }; @@ -197,9 +209,15 @@ namespace ManagerService.Controllers if (device == null) throw new KeyNotFoundException("Device does not exist"); + var configuration = _configurationService.GetById(deviceIn.ConfigurationId); + + if (configuration == null) + throw new KeyNotFoundException("Configuration does not exist"); + // Todo add some verification ? device.Name = deviceIn.Name; device.Connected = deviceIn.Connected; + device.Configuration = configuration.Label; device.ConfigurationId = deviceIn.ConfigurationId; Device deviceModified = _deviceService.Update(device.Id, device);