From c57ad9cce7746e3512e265352c888f1573d5dff5 Mon Sep 17 00:00:00 2001 From: Marcel Baumgartner Date: Thu, 9 Nov 2023 14:42:41 +0100 Subject: [PATCH] Fixed ui bugs. Implemented ticket deletion when a user is going to be deleted --- .../App/Services/Users/UserDeleteService.cs | 45 ++++++++++++++++++- .../TicketPopup/TicketPopupView.razor | 2 +- .../Shared/Views/Admin/Tickets/View.razor | 2 +- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/Moonlight/App/Services/Users/UserDeleteService.cs b/Moonlight/App/Services/Users/UserDeleteService.cs index c9ce8daa..025802cf 100644 --- a/Moonlight/App/Services/Users/UserDeleteService.cs +++ b/Moonlight/App/Services/Users/UserDeleteService.cs @@ -2,6 +2,7 @@ using Moonlight.App.Database.Entities; using Moonlight.App.Database.Entities.Community; using Moonlight.App.Database.Entities.Store; +using Moonlight.App.Database.Entities.Tickets; using Moonlight.App.Repositories; using Moonlight.App.Services.Community; using Moonlight.App.Services.ServiceManage; @@ -16,6 +17,8 @@ public class UserDeleteService private readonly Repository TransactionRepository; private readonly Repository CouponUseRepository; private readonly Repository GiftCodeUseRepository; + private readonly Repository TicketRepository; + private readonly Repository TicketMessageRepository; private readonly ServiceService ServiceService; private readonly PostService PostService; @@ -27,7 +30,9 @@ public class UserDeleteService Repository userRepository, Repository giftCodeUseRepository, Repository couponUseRepository, - Repository transactionRepository) + Repository transactionRepository, + Repository ticketRepository, + Repository ticketMessageRepository) { ServiceRepository = serviceRepository; ServiceService = serviceService; @@ -37,6 +42,8 @@ public class UserDeleteService GiftCodeUseRepository = giftCodeUseRepository; CouponUseRepository = couponUseRepository; TransactionRepository = transactionRepository; + TicketRepository = ticketRepository; + TicketMessageRepository = ticketMessageRepository; } public async Task Perform(User user) @@ -103,6 +110,42 @@ public class UserDeleteService foreach (var transaction in transactions) TransactionRepository.Delete(transaction); + // Tickets and ticket messages + + // First we need to fetch every message this user has sent and delete it as admin accounts can have messages + // in tickets they dont own + var messagesFromUser = TicketMessageRepository + .Get() + .Where(x => x.Sender.Id == user.Id) + .ToArray(); + + foreach (var message in messagesFromUser) + { + TicketMessageRepository.Delete(message); + } + + // Now we can only delete the tickets the user actually owns + var tickets = TicketRepository + .Get() + .Include(x => x.Messages) + .Where(x => x.Creator.Id == user.Id) + .ToArray(); + + foreach (var ticket in tickets) + { + var messages = ticket.Messages.ToArray(); // Cache message models + + ticket.Messages.Clear(); + TicketRepository.Update(ticket); + + foreach (var ticketMessage in messages) + { + TicketMessageRepository.Delete(ticketMessage); + } + + TicketRepository.Delete(ticket); + } + // User // We need to use this in order to entity framework not crashing because of the previous deleted data diff --git a/Moonlight/Shared/Components/TicketPopup/TicketPopupView.razor b/Moonlight/Shared/Components/TicketPopup/TicketPopupView.razor index f970dcad..56dc9d99 100644 --- a/Moonlight/Shared/Components/TicketPopup/TicketPopupView.razor +++ b/Moonlight/Shared/Components/TicketPopup/TicketPopupView.razor @@ -57,7 +57,7 @@ } else { -
@(message.Content)
+
@(message.Content)
} } diff --git a/Moonlight/Shared/Views/Admin/Tickets/View.razor b/Moonlight/Shared/Views/Admin/Tickets/View.razor index 2d298803..23ee3f5c 100644 --- a/Moonlight/Shared/Views/Admin/Tickets/View.razor +++ b/Moonlight/Shared/Views/Admin/Tickets/View.razor @@ -100,7 +100,7 @@ - Status + Open