diff --git a/Moonlight/App/Services/ServiceManage/ServiceAdminService.cs b/Moonlight/App/Services/ServiceManage/ServiceAdminService.cs index d189f24..2fe7b46 100644 --- a/Moonlight/App/Services/ServiceManage/ServiceAdminService.cs +++ b/Moonlight/App/Services/ServiceManage/ServiceAdminService.cs @@ -75,7 +75,7 @@ public class ServiceAdminService await Actions[service.Product.Type].Delete(scope.ServiceProvider, service); - foreach (var share in service.Shares) + foreach (var share in service.Shares.ToArray()) { serviceShareRepo.Delete(share); } diff --git a/Moonlight/App/Services/Users/UserDeleteService.cs b/Moonlight/App/Services/Users/UserDeleteService.cs new file mode 100644 index 0000000..c9ce8da --- /dev/null +++ b/Moonlight/App/Services/Users/UserDeleteService.cs @@ -0,0 +1,112 @@ +using Microsoft.EntityFrameworkCore; +using Moonlight.App.Database.Entities; +using Moonlight.App.Database.Entities.Community; +using Moonlight.App.Database.Entities.Store; +using Moonlight.App.Repositories; +using Moonlight.App.Services.Community; +using Moonlight.App.Services.ServiceManage; + +namespace Moonlight.App.Services.Users; + +public class UserDeleteService +{ + private readonly Repository ServiceRepository; + private readonly Repository PostRepository; + private readonly Repository UserRepository; + private readonly Repository TransactionRepository; + private readonly Repository CouponUseRepository; + private readonly Repository GiftCodeUseRepository; + private readonly ServiceService ServiceService; + private readonly PostService PostService; + + public UserDeleteService( + Repository serviceRepository, + ServiceService serviceService, + PostService postService, + Repository postRepository, + Repository userRepository, + Repository giftCodeUseRepository, + Repository couponUseRepository, + Repository transactionRepository) + { + ServiceRepository = serviceRepository; + ServiceService = serviceService; + PostService = postService; + PostRepository = postRepository; + UserRepository = userRepository; + GiftCodeUseRepository = giftCodeUseRepository; + CouponUseRepository = couponUseRepository; + TransactionRepository = transactionRepository; + } + + public async Task Perform(User user) + { + // Community + + // - Posts + foreach (var post in PostRepository.Get().ToArray()) + { + await PostService.Delete(post); + } + + // - Comments + var posts = PostRepository + .Get() + .Where(x => x.Comments.Any(y => y.Author.Id == user.Id)) + .ToArray(); + + foreach (var post in posts) + { + var comments = PostRepository + .Get() + .Include(x => x.Comments) + .ThenInclude(x => x.Author) + .First(x => x.Id == post.Id) + .Comments + .Where(x => x.Author.Id == user.Id) + .ToArray(); + + foreach (var comment in comments) + await PostService.DeleteComment(post, comment); + } + + // Services + foreach (var service in ServiceRepository.Get().Where(x => x.Owner.Id == user.Id).ToArray()) + { + await ServiceService.Admin.Delete(service); + } + + // Transactions - Coupons - Gift codes + var userWithDetails = UserRepository + .Get() + .Include(x => x.Transactions) + .Include(x => x.CouponUses) + .Include(x => x.GiftCodeUses) + .First(x => x.Id == user.Id); + + var giftCodeUses = userWithDetails.GiftCodeUses.ToArray(); + var couponUses = userWithDetails.CouponUses.ToArray(); + var transactions = userWithDetails.Transactions.ToArray(); + + userWithDetails.GiftCodeUses.Clear(); + userWithDetails.CouponUses.Clear(); + userWithDetails.Transactions.Clear(); + + UserRepository.Update(userWithDetails); + + foreach (var giftCodeUse in giftCodeUses) + GiftCodeUseRepository.Delete(giftCodeUse); + + foreach (var couponUse in couponUses) + CouponUseRepository.Delete(couponUse); + + foreach (var transaction in transactions) + TransactionRepository.Delete(transaction); + + // User + + // We need to use this in order to entity framework not crashing because of the previous deleted data + var userToDelete = UserRepository.Get().First(x => x.Id == user.Id); + UserRepository.Delete(userToDelete); + } +} \ No newline at end of file diff --git a/Moonlight/App/Services/Users/UserService.cs b/Moonlight/App/Services/Users/UserService.cs index 21c907f..4770545 100644 --- a/Moonlight/App/Services/Users/UserService.cs +++ b/Moonlight/App/Services/Users/UserService.cs @@ -11,6 +11,7 @@ public class UserService public UserAuthService Auth => ServiceProvider.GetRequiredService(); public UserDetailsService Details => ServiceProvider.GetRequiredService(); + public UserDeleteService Delete => ServiceProvider.GetRequiredService(); public UserService( Repository userRepository, @@ -41,10 +42,4 @@ public class UserService return Task.CompletedTask; } - - public Task Delete(User user) - { - UserRepository.Delete(user); - return Task.CompletedTask; - } } \ No newline at end of file diff --git a/Moonlight/Program.cs b/Moonlight/Program.cs index dca2ceb..2300633 100644 --- a/Moonlight/Program.cs +++ b/Moonlight/Program.cs @@ -66,6 +66,7 @@ builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); builder.Services.AddScoped(); +builder.Services.AddScoped(); // Services / Background builder.Services.AddSingleton(); diff --git a/Moonlight/Shared/Views/Account/Index.razor b/Moonlight/Shared/Views/Account/Index.razor index 4b50822..8ec07b5 100644 --- a/Moonlight/Shared/Views/Account/Index.razor +++ b/Moonlight/Shared/Views/Account/Index.razor @@ -130,7 +130,7 @@ private async Task OnDelete() { - await UserService.Delete(IdentityService.CurrentUser); + await UserService.Delete.Perform(IdentityService.CurrentUser); await IdentityService.Authenticate(); } } \ No newline at end of file diff --git a/Moonlight/Shared/Views/Admin/Users/View.razor b/Moonlight/Shared/Views/Admin/Users/View.razor index 548adb5..b343cba 100644 --- a/Moonlight/Shared/Views/Admin/Users/View.razor +++ b/Moonlight/Shared/Views/Admin/Users/View.razor @@ -230,7 +230,7 @@ private async Task Delete() { - await UserService.Delete(User!); + await UserService.Delete.Perform(User!); await ToastService.Success("Successfully deleted user"); Navigation.NavigateTo("/admin/users"); }