From 26253f281e52129635d6911cf0a082b1bf01f233 Mon Sep 17 00:00:00 2001 From: Thomas Fransolet Date: Sat, 19 Jun 2021 17:29:59 +0200 Subject: [PATCH] Add update device main infos --- .../Controllers/DeviceController.cs | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/ManagerService/Controllers/DeviceController.cs b/ManagerService/Controllers/DeviceController.cs index 9de9ca6..6f0d33a 100644 --- a/ManagerService/Controllers/DeviceController.cs +++ b/ManagerService/Controllers/DeviceController.cs @@ -100,6 +100,7 @@ namespace ManagerService.Controllers // Todo add some verification ? Device device = new Device(); device.Name = newDevice.Name; + device.ConfigurationId = newDevice.ConfigurationId; device.IpAddress = newDevice.IpAddress; device.Connected = newDevice.Connected; device.ConnectionLevel = newDevice.ConnectionLevel; @@ -175,6 +176,50 @@ namespace ManagerService.Controllers } } + /// + /// Update device main info + /// + /// Device to update + [ProducesResponseType(typeof(DeviceDTO), 200)] + [ProducesResponseType(typeof(string), 400)] + [ProducesResponseType(typeof(string), 404)] + [ProducesResponseType(typeof(string), 500)] + [HttpPut("mainInfos")] + public ObjectResult UpdateMainInfos([FromBody] DeviceDTO deviceIn) + { + try + { + if (deviceIn == null) + throw new ArgumentNullException("Device param is null"); + + Device device = _deviceService.GetById(deviceIn.Id); + + if (device == null) + throw new KeyNotFoundException("Device does not exist"); + + // Todo add some verification ? + device.Name = deviceIn.Name; + device.Connected = deviceIn.Connected; + device.ConfigurationId = deviceIn.ConfigurationId; + + Device deviceModified = _deviceService.Update(device.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