From 0365b9b954ce9296aeec99f86a89064747a84ea0 Mon Sep 17 00:00:00 2001 From: mariaruth1 <113031776+mariaruth1@users.noreply.github.com> Date: Mon, 20 May 2024 18:38:33 +0200 Subject: [PATCH 1/4] deleted reference of collection from plant upon deleting collection, where is generic error coming from?? --- Core/Services/CollectionsService.cs | 13 +++++++++++-- .../Repositories/CollectionsRepository.cs | 10 ++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/Core/Services/CollectionsService.cs b/Core/Services/CollectionsService.cs index d0f4700..73c62c8 100644 --- a/Core/Services/CollectionsService.cs +++ b/Core/Services/CollectionsService.cs @@ -1,4 +1,5 @@ using Infrastructure.Repositories; +using Serilog; using Shared.Dtos; using Shared.Dtos.FromClient.Collections; using Shared.Exceptions; @@ -49,8 +50,16 @@ public async Task UpdateCollection(UpdateCollectionDto updateCollect public async Task DeleteCollection(Guid collectionId, string loggedInUser) { - var collection = await VerifyCollectionExistsAndUserHasAccess(collectionId, loggedInUser); - await collectionsRepository.DeleteCollection(collection); + try + { + var collection = await VerifyCollectionExistsAndUserHasAccess(collectionId, loggedInUser); + await collectionsRepository.DeleteCollection(collection); + } + catch (Exception e) + { + Log.Error(e.Message, e.InnerException); + throw new AppException("Failed to delete collection."); + } } public async Task AddPlantToCollection(Guid collectionId, Guid plantId, string loggedInUser) diff --git a/Infrastructure/Repositories/CollectionsRepository.cs b/Infrastructure/Repositories/CollectionsRepository.cs index 0462bd0..79d5312 100644 --- a/Infrastructure/Repositories/CollectionsRepository.cs +++ b/Infrastructure/Repositories/CollectionsRepository.cs @@ -39,6 +39,16 @@ public async Task UpdateCollection(Collection collection) public async Task DeleteCollection(Collection collection) { await using var applicationDbContext = await dbContextFactory.CreateDbContextAsync(); + + // Remove the collection reference from plants + var plantsToUpdate = await applicationDbContext.Plants + .Where(p => p.CollectionId == collection.CollectionId) + .ToListAsync(); + + foreach (var plant in plantsToUpdate) + { + plant.CollectionId = null; + } applicationDbContext.Collections.Remove(collection); await applicationDbContext.SaveChangesAsync(); } From c9bfd36acc0c5ecf69b0512beb8f648997645af9 Mon Sep 17 00:00:00 2001 From: mariaruth1 <113031776+mariaruth1@users.noreply.github.com> Date: Thu, 23 May 2024 09:54:28 +0200 Subject: [PATCH 2/4] changed error handling flow delete collection --- Core/Services/CollectionsService.cs | 12 ++---------- .../Client/ClientWantsToDeleteCollection.cs | 18 +++++++++++++++--- 2 files changed, 17 insertions(+), 13 deletions(-) diff --git a/Core/Services/CollectionsService.cs b/Core/Services/CollectionsService.cs index 73c62c8..0146847 100644 --- a/Core/Services/CollectionsService.cs +++ b/Core/Services/CollectionsService.cs @@ -50,16 +50,8 @@ public async Task UpdateCollection(UpdateCollectionDto updateCollect public async Task DeleteCollection(Guid collectionId, string loggedInUser) { - try - { - var collection = await VerifyCollectionExistsAndUserHasAccess(collectionId, loggedInUser); - await collectionsRepository.DeleteCollection(collection); - } - catch (Exception e) - { - Log.Error(e.Message, e.InnerException); - throw new AppException("Failed to delete collection."); - } + var collection = await VerifyCollectionExistsAndUserHasAccess(collectionId, loggedInUser); + await collectionsRepository.DeleteCollection(collection); } public async Task AddPlantToCollection(Guid collectionId, Guid plantId, string loggedInUser) diff --git a/api/Events/Collections/Client/ClientWantsToDeleteCollection.cs b/api/Events/Collections/Client/ClientWantsToDeleteCollection.cs index ee5a207..f9ebb66 100644 --- a/api/Events/Collections/Client/ClientWantsToDeleteCollection.cs +++ b/api/Events/Collections/Client/ClientWantsToDeleteCollection.cs @@ -1,8 +1,10 @@ using api.Events.Collections.Server; +using api.Events.User.ServerResponses; using api.Extensions; using Core.Services; using Fleck; using lib; +using Shared.Exceptions; using Shared.Models; namespace api.Events.Collections.Client; @@ -16,8 +18,18 @@ public class ClientWantsToDeleteCollection(CollectionsService collectionsService { public override async Task Handle(ClientWantsToDeleteCollectionDto dto, IWebSocketConnection socket) { - var email = jwtService.GetEmailFromJwt(dto.Jwt!); - await collectionsService.DeleteCollection(dto.CollectionId, email); - socket.SendDto(new ServerDeletesCollection()); + try + { + var email = jwtService.GetEmailFromJwt(dto.Jwt!); + await collectionsService.DeleteCollection(dto.CollectionId, email); + socket.SendDto(new ServerDeletesCollection()); + } + catch (Exception e) when (e is not NotFoundException) + { + socket.SendDto(new ServerRejectsUpdate + { + Error = "Could not delete collection. Please try again later." + }); + } } } \ No newline at end of file From d4ba13c332782fda2b0024aeda02b7ba07abf00a Mon Sep 17 00:00:00 2001 From: mariaruth1 <113031776+mariaruth1@users.noreply.github.com> Date: Thu, 23 May 2024 13:24:13 +0200 Subject: [PATCH 3/4] actually update plants when deleting a collection --- Infrastructure/Repositories/CollectionsRepository.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/Infrastructure/Repositories/CollectionsRepository.cs b/Infrastructure/Repositories/CollectionsRepository.cs index 79d5312..f434f85 100644 --- a/Infrastructure/Repositories/CollectionsRepository.cs +++ b/Infrastructure/Repositories/CollectionsRepository.cs @@ -49,6 +49,7 @@ public async Task DeleteCollection(Collection collection) { plant.CollectionId = null; } + applicationDbContext.Plants.UpdateRange(plantsToUpdate); applicationDbContext.Collections.Remove(collection); await applicationDbContext.SaveChangesAsync(); } From b7206f8b80cd657db21bdb396fc1bfc9d85cb7dc Mon Sep 17 00:00:00 2001 From: mariaruth1 <113031776+mariaruth1@users.noreply.github.com> Date: Thu, 23 May 2024 16:09:08 +0200 Subject: [PATCH 4/4] implemented suggested changes to avoid duplicate error handling --- .../Client/ClientWantsToDeleteCollection.cs | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/api/Events/Collections/Client/ClientWantsToDeleteCollection.cs b/api/Events/Collections/Client/ClientWantsToDeleteCollection.cs index f9ebb66..f924791 100644 --- a/api/Events/Collections/Client/ClientWantsToDeleteCollection.cs +++ b/api/Events/Collections/Client/ClientWantsToDeleteCollection.cs @@ -18,18 +18,9 @@ public class ClientWantsToDeleteCollection(CollectionsService collectionsService { public override async Task Handle(ClientWantsToDeleteCollectionDto dto, IWebSocketConnection socket) { - try - { - var email = jwtService.GetEmailFromJwt(dto.Jwt!); - await collectionsService.DeleteCollection(dto.CollectionId, email); - socket.SendDto(new ServerDeletesCollection()); - } - catch (Exception e) when (e is not NotFoundException) - { - socket.SendDto(new ServerRejectsUpdate - { - Error = "Could not delete collection. Please try again later." - }); - } + var email = jwtService.GetEmailFromJwt(dto.Jwt!); + await collectionsService.DeleteCollection(dto.CollectionId, email); + socket.SendDto(new ServerDeletesCollection()); + } } \ No newline at end of file