Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collections events #18

Merged
merged 5 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Core/Services/CollectionsService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Infrastructure.Repositories;
using Serilog;
using Shared.Dtos;
using Shared.Dtos.FromClient.Collections;
using Shared.Exceptions;
Expand Down
11 changes: 11 additions & 0 deletions Infrastructure/Repositories/CollectionsRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ public async Task<Collection> 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.Plants.UpdateRange(plantsToUpdate);
applicationDbContext.Collections.Remove(collection);
await applicationDbContext.SaveChangesAsync();
}
Expand Down
18 changes: 15 additions & 3 deletions api/Events/Collections/Client/ClientWantsToDeleteCollection.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
mariaruth1 marked this conversation as resolved.
Show resolved Hide resolved
{
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."
});
}
}
}
Loading