Skip to content

Commit

Permalink
added check for existing plant with device id to create and update plant
Browse files Browse the repository at this point in the history
  • Loading branch information
mariaruth1 committed May 28, 2024
1 parent 874146a commit 91c04d0
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Infrastructure/Repositories/PlantRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,16 @@ public class PlantRepository(IDbContextFactory<ApplicationDbContext> 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<bool> DoesDeviceIdExist(string deviceId)
{
await using var context = await dbContextFactory.CreateDbContextAsync();
return await context.Plants.AnyAsync(p => p.DeviceId == deviceId);
}

public async Task<Plant?> GetPlantById(Guid id)
{
Expand Down
32 changes: 31 additions & 1 deletion api/Core/Services/PlantService.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -13,7 +15,7 @@ public class PlantService(
PlantRepository plantRepository,
RequirementService requirementService,
IBlobStorageService blobStorageService,
IOptions<AzureBlobStorageOptions> azureBlobStorageOptions)
IOptions<AzureBlobStorageOptions> azureBlobStorageOptions, WebSocketConnectionService webSocketConnectionService)
{
public async Task<Plant> CreatePlant(CreatePlantDto createPlantDto, string loggedInUser)
{
Expand All @@ -28,6 +30,16 @@ public async Task<Plant> 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
{
Expand All @@ -40,6 +52,8 @@ public async Task<Plant> CreatePlant(CreatePlantDto createPlantDto, string logge
LatestChange = DateTime.UtcNow
};



await plantRepository.CreatePlant(plant);

// Create requirements for the plant to crete a link between the two
Expand All @@ -50,6 +64,12 @@ public async Task<Plant> 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<Plant> GetPlantById(Guid id, string requesterEmail)
{
var plant = await VerifyPlantExistsAndUserHasAccess(id, requesterEmail);
Expand Down Expand Up @@ -89,6 +109,16 @@ public async Task<Plant> 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
{
Expand Down

0 comments on commit 91c04d0

Please sign in to comment.