diff --git a/Infrastructure/Repositories/PlantRepository.cs b/Infrastructure/Repositories/PlantRepository.cs index 0727ce1..f1fc8d6 100644 --- a/Infrastructure/Repositories/PlantRepository.cs +++ b/Infrastructure/Repositories/PlantRepository.cs @@ -11,9 +11,16 @@ public class PlantRepository(IDbContextFactory dbContextFa public async Task CreatePlant(Plant plant) { await using var context = await dbContextFactory.CreateDbContextAsync(); + await context.Plants.AddAsync(plant); await context.SaveChangesAsync(); } + + public async Task DoesDeviceIdExist(string deviceId) + { + await using var context = await dbContextFactory.CreateDbContextAsync(); + return await context.Plants.AnyAsync(p => p.DeviceId == deviceId); + } public async Task GetPlantById(Guid id) { diff --git a/api/Core/Services/PlantService.cs b/api/Core/Services/PlantService.cs index 3922459..e395a54 100644 --- a/api/Core/Services/PlantService.cs +++ b/api/Core/Services/PlantService.cs @@ -1,5 +1,7 @@ using api.Core.Options; using api.Core.Services.External.BlobStorage; +using api.Events.Global; +using api.Extensions; using Infrastructure.Repositories; using Microsoft.Extensions.Options; using Shared.Dtos.FromClient.Plant; @@ -13,7 +15,7 @@ public class PlantService( PlantRepository plantRepository, RequirementService requirementService, IBlobStorageService blobStorageService, - IOptions azureBlobStorageOptions) + IOptions azureBlobStorageOptions, WebSocketConnectionService webSocketConnectionService) { public async Task CreatePlant(CreatePlantDto createPlantDto, string loggedInUser) { @@ -28,6 +30,16 @@ public async Task CreatePlant(CreatePlantDto createPlantDto, string logge ímageUrl = await blobStorageService.SaveImageToBlobStorage(createPlantDto.Base64Image, loggedInUser, true); } + + if (!String.IsNullOrEmpty(createPlantDto.DeviceId)) + { + var deviceExists = await plantRepository.DoesDeviceIdExist(createPlantDto.DeviceId); + HandleExistingDeviceId(loggedInUser); + if (deviceExists) + { + createPlantDto.DeviceId = null; + } + } // Insert plant first to get the plantId var plant = new Plant { @@ -40,6 +52,8 @@ public async Task CreatePlant(CreatePlantDto createPlantDto, string logge LatestChange = DateTime.UtcNow }; + + await plantRepository.CreatePlant(plant); // Create requirements for the plant to crete a link between the two @@ -50,6 +64,12 @@ public async Task CreatePlant(CreatePlantDto createPlantDto, string logge return plant; } + private void HandleExistingDeviceId(string loggedInUser) + { + var connection = webSocketConnectionService.GetConnectionByEmail(loggedInUser); + connection?.SendDto(new ServerSendsErrorMessage{Error = "Device ID already in use"}); + } + public async Task GetPlantById(Guid id, string requesterEmail) { var plant = await VerifyPlantExistsAndUserHasAccess(id, requesterEmail); @@ -89,6 +109,16 @@ public async Task UpdatePlant(UpdatePlantDto updatePlantDto, string reque imageUrl = await blobStorageService.SaveImageToBlobStorage(updatePlantDto.Base64Image, requesterEmail, true, plant.ImageUrl); } + if (!String.IsNullOrEmpty(updatePlantDto.DeviceId)) + { + var deviceExists = await plantRepository.DoesDeviceIdExist(updatePlantDto.DeviceId); + HandleExistingDeviceId(requesterEmail); + if (deviceExists) + { + updatePlantDto.DeviceId = null; + } + } + // Update the plant plant = new Plant {