Skip to content

Commit

Permalink
Fixes a some critical bugs in the edit screens for devices / traits w…
Browse files Browse the repository at this point in the history
…hich could result in lost configuration
  • Loading branch information
i8beef committed Dec 31, 2022
1 parent 2b5d34d commit 25a744f
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ public IActionResult Edit([Required] string deviceId)
[ExportModelState]
public IActionResult Edit([Required] string deviceId, DeviceViewModel viewModel)
{
if (!_deviceRepository.Contains(deviceId))
var existingDevice = _deviceRepository.FindById(deviceId);
if (existingDevice is null)
{
return NotFound();
}
Expand Down Expand Up @@ -276,7 +277,7 @@ public IActionResult Edit([Required] string deviceId, DeviceViewModel viewModel)
RoomHint = viewModel.RoomHint,
Name = nameInfo,
DeviceInfo = deviceInfo,
Traits = new List<DeviceTrait>()
Traits = existingDevice.Traits
};

// Final validation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,6 @@ public IActionResult Edit([Required] string deviceId, [Required] string traitId,
}

// Set new values
var existingtrait = device.Traits.FirstOrDefault(x => x.Trait == traitEnumId);
var commands = !string.IsNullOrEmpty(viewModel.Commands) ? JsonConvert.DeserializeObject<Dictionary<string, IDictionary<string, string>>>(viewModel.Commands) : null;
var trait = new DeviceTrait
{
Expand All @@ -224,6 +223,10 @@ public IActionResult Edit([Required] string deviceId, [Required] string traitId,
}
};

// Replace trait
device.Traits.Remove(device.Traits.First(x => x.Trait == traitEnumId));
device.Traits.Add(trait);

// Final validation
foreach (var error in DeviceTraitValidator.Validate(trait))
{
Expand Down

0 comments on commit 25a744f

Please sign in to comment.