diff --git a/src/Elektrifikatsiya/Elektrifikatsiya/Services/Implementations/DeviceManagmentService.cs b/src/Elektrifikatsiya/Elektrifikatsiya/Services/Implementations/DeviceManagmentService.cs index 5bbf350..34916e0 100644 --- a/src/Elektrifikatsiya/Elektrifikatsiya/Services/Implementations/DeviceManagmentService.cs +++ b/src/Elektrifikatsiya/Elektrifikatsiya/Services/Implementations/DeviceManagmentService.cs @@ -10,7 +10,6 @@ namespace Elektrifikatsiya.Services.Implementations; public class DeviceManagmentService : IDeviceManagmentService { - private readonly DeviceManagmentDatabaseContext deviceManagmentDatabaseContext; private readonly IServiceScopeFactory serviceScopeFactory; private readonly IDeviceStatusService deviceStatusService; private readonly ILogger logger; @@ -22,7 +21,6 @@ public class DeviceManagmentService : IDeviceManagmentService this.deviceStatusService = deviceStatusService; this.logger = logger; this.httpClient = httpClient; - } public Result GetDevice(string macAdress) @@ -113,9 +111,12 @@ public class DeviceManagmentService : IDeviceManagmentService _ = deviceManagmentDatabaseContext.Add(device); Result saveDatabaseChangesResult = await Result.Try(Task () => deviceManagmentDatabaseContext.SaveChangesAsync()); - Result trackDeviceResult = deviceStatusService.TrackDevice(device); + if (saveDatabaseChangesResult.IsFailed) + { + return saveDatabaseChangesResult; + } - return Result.Merge(saveDatabaseChangesResult, trackDeviceResult).ToResult(device); + return deviceStatusService.TrackDevice(device); } public async Task UnregisterDevice(string macAdress) @@ -144,6 +145,9 @@ public class DeviceManagmentService : IDeviceManagmentService public async Task UpdateDevice(Device device) { + using IServiceScope scope = serviceScopeFactory.CreateScope(); + DeviceManagmentDatabaseContext deviceManagmentDatabaseContext = scope.ServiceProvider.GetRequiredService(); + Result result = deviceStatusService.UpdateDeviceStatus(device); if (result.IsFailed)