Add configuration label to device DTO

This commit is contained in:
Thomas Fransolet 2021-06-23 22:44:41 +02:00
parent 26253f281e
commit 22c34d90a6
3 changed files with 27 additions and 3 deletions

View File

@ -10,6 +10,7 @@ namespace Manager.Interfaces.DTO
public string Name { get; set; } public string Name { get; set; }
public string IpAddress { get; set; } public string IpAddress { get; set; }
public string ConfigurationId { get; set; } public string ConfigurationId { get; set; }
public string Configuration { get; set; }
public bool Connected{ get; set; } public bool Connected{ get; set; }
public DateTime DateCreation{ get; set; } public DateTime DateCreation{ get; set; }
} }

View File

@ -22,9 +22,12 @@ namespace Manager.Interfaces.Models
[BsonRequired] [BsonRequired]
public string IpAddress { get; set; } public string IpAddress { get; set; }
[BsonElement("Configuration")]
public string Configuration { get; set; }
[BsonElement("ConfigurationId")] [BsonElement("ConfigurationId")]
[BsonRequired] [BsonRequired]
public string ConfigurationId { get; set; } public string ConfigurationId { get; set; }
[BsonElement("Connected")] [BsonElement("Connected")]
[BsonRequired] [BsonRequired]
@ -56,6 +59,7 @@ namespace Manager.Interfaces.Models
Name = Name, Name = Name,
IpAddress = IpAddress, IpAddress = IpAddress,
Connected = Connected, Connected = Connected,
Configuration = Configuration,
ConfigurationId = ConfigurationId, ConfigurationId = ConfigurationId,
DateCreation = DateCreation DateCreation = DateCreation
}; };
@ -69,6 +73,7 @@ namespace Manager.Interfaces.Models
Name = Name, Name = Name,
IpAddress = IpAddress, IpAddress = IpAddress,
Connected = Connected, Connected = Connected,
Configuration = Configuration,
ConfigurationId = ConfigurationId, ConfigurationId = ConfigurationId,
ConnectionLevel = ConnectionLevel, ConnectionLevel = ConnectionLevel,
LastConnectionLevel = LastConnectionLevel, LastConnectionLevel = LastConnectionLevel,

View File

@ -22,12 +22,14 @@ namespace ManagerService.Controllers
public class DeviceController : ControllerBase public class DeviceController : ControllerBase
{ {
private DeviceDatabaseService _deviceService; private DeviceDatabaseService _deviceService;
private ConfigurationDatabaseService _configurationService;
private readonly ILogger<DeviceController> _logger; private readonly ILogger<DeviceController> _logger;
public DeviceController(ILogger<DeviceController> logger, DeviceDatabaseService deviceService) public DeviceController(ILogger<DeviceController> logger, DeviceDatabaseService deviceService, ConfigurationDatabaseService configurationService)
{ {
_logger = logger; _logger = logger;
_deviceService = deviceService; _deviceService = deviceService;
_configurationService = configurationService;
} }
/// <summary> /// <summary>
@ -87,6 +89,7 @@ namespace ManagerService.Controllers
/// <param name="newDevice">New device info</param> /// <param name="newDevice">New device info</param>
[ProducesResponseType(typeof(DeviceDetailDTO), 200)] [ProducesResponseType(typeof(DeviceDetailDTO), 200)]
[ProducesResponseType(typeof(string), 400)] [ProducesResponseType(typeof(string), 400)]
[ProducesResponseType(typeof(string), 404)]
[ProducesResponseType(typeof(string), 409)] [ProducesResponseType(typeof(string), 409)]
[ProducesResponseType(typeof(string), 500)] [ProducesResponseType(typeof(string), 500)]
[HttpPost] [HttpPost]
@ -97,9 +100,14 @@ namespace ManagerService.Controllers
if (newDevice == null) if (newDevice == null)
throw new ArgumentNullException("Device param is 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 device = new Device();
device.Name = newDevice.Name; device.Name = newDevice.Name;
device.Configuration = configuration.Label;
device.ConfigurationId = newDevice.ConfigurationId; device.ConfigurationId = newDevice.ConfigurationId;
device.IpAddress = newDevice.IpAddress; device.IpAddress = newDevice.IpAddress;
device.Connected = newDevice.Connected; device.Connected = newDevice.Connected;
@ -117,6 +125,10 @@ namespace ManagerService.Controllers
{ {
return new BadRequestObjectResult(ex.Message) { }; return new BadRequestObjectResult(ex.Message) { };
} }
catch (KeyNotFoundException ex)
{
return new NotFoundObjectResult(ex.Message) { };
}
catch (InvalidOperationException ex) catch (InvalidOperationException ex)
{ {
return new ConflictObjectResult(ex.Message) { }; return new ConflictObjectResult(ex.Message) { };
@ -197,9 +209,15 @@ namespace ManagerService.Controllers
if (device == null) if (device == null)
throw new KeyNotFoundException("Device does not exist"); 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 ? // Todo add some verification ?
device.Name = deviceIn.Name; device.Name = deviceIn.Name;
device.Connected = deviceIn.Connected; device.Connected = deviceIn.Connected;
device.Configuration = configuration.Label;
device.ConfigurationId = deviceIn.ConfigurationId; device.ConfigurationId = deviceIn.ConfigurationId;
Device deviceModified = _deviceService.Update(device.Id, device); Device deviceModified = _deviceService.Update(device.Id, device);