public Device UpdateDeviceData(int userId, UpdateDeviceDataDto updateDeviceDataDto) { var existingDevice = GetById(updateDeviceDataDto.DeviceId); if (existingDevice == null) { var createdDevice = new Device { Id = updateDeviceDataDto.DeviceId, Name = updateDeviceDataDto.Name, UserId = userId }; foreach (var sensor in updateDeviceDataDto.SensorData) { var createdSensor = new Sensor { SensorKey = sensor.SensorKey, SensorType = sensor.SensorType }; var data = new SensorData(sensor.Value); createdSensor.Data.Add(data); createdDevice.Sensors.Add(createdSensor); } _context.Add(createdDevice); _context.SaveChanges(); return(createdDevice); } foreach (var sensor in updateDeviceDataDto.SensorData) { var dataToAdd = new SensorData(sensor.Value); if (existingDevice.Sensors.All(s => s.SensorKey != sensor.SensorKey)) { var sensorToAdd = new Sensor { SensorKey = sensor.SensorKey, SensorType = sensor.SensorType }; sensorToAdd.Data.Add(dataToAdd); existingDevice.Sensors.Add(sensorToAdd); } else { existingDevice.Sensors.SingleOrDefault(s => s.SensorKey == sensor.SensorKey)?.Data .Add(dataToAdd); } } _context.Update(existingDevice); _context.SaveChanges(); return(existingDevice); }
public IActionResult SendSensorData([FromBody] UpdateDeviceDataDto updateDeviceDataDto) => _sendSensorDataOperation.Execute(updateDeviceDataDto);