Improved user delete to delete relational data and remove all content from a user
This commit is contained in:
parent
52d39151da
commit
aa501244e1
5 changed files with 113 additions and 8 deletions
109
Moonlight/App/Services/Users/UserDeleteService.cs
Normal file
109
Moonlight/App/Services/Users/UserDeleteService.cs
Normal file
|
@ -0,0 +1,109 @@
|
||||||
|
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<Service> ServiceRepository;
|
||||||
|
private readonly Repository<Post> PostRepository;
|
||||||
|
private readonly Repository<User> UserRepository;
|
||||||
|
private readonly Repository<Transaction> TransactionRepository;
|
||||||
|
private readonly Repository<CouponUse> CouponUseRepository;
|
||||||
|
private readonly Repository<GiftCodeUse> GiftCodeUseRepository;
|
||||||
|
private readonly ServiceService ServiceService;
|
||||||
|
private readonly PostService PostService;
|
||||||
|
|
||||||
|
public UserDeleteService(
|
||||||
|
Repository<Service> serviceRepository,
|
||||||
|
ServiceService serviceService,
|
||||||
|
PostService postService,
|
||||||
|
Repository<Post> postRepository,
|
||||||
|
Repository<User> userRepository,
|
||||||
|
Repository<GiftCodeUse> giftCodeUseRepository,
|
||||||
|
Repository<CouponUse> couponUseRepository,
|
||||||
|
Repository<Transaction> 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
|
||||||
|
UserRepository.Delete(user);
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,6 +11,7 @@ public class UserService
|
||||||
|
|
||||||
public UserAuthService Auth => ServiceProvider.GetRequiredService<UserAuthService>();
|
public UserAuthService Auth => ServiceProvider.GetRequiredService<UserAuthService>();
|
||||||
public UserDetailsService Details => ServiceProvider.GetRequiredService<UserDetailsService>();
|
public UserDetailsService Details => ServiceProvider.GetRequiredService<UserDetailsService>();
|
||||||
|
public UserDeleteService Delete => ServiceProvider.GetRequiredService<UserDeleteService>();
|
||||||
|
|
||||||
public UserService(
|
public UserService(
|
||||||
Repository<User> userRepository,
|
Repository<User> userRepository,
|
||||||
|
@ -41,10 +42,4 @@ public class UserService
|
||||||
|
|
||||||
return Task.CompletedTask;
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task Delete(User user)
|
|
||||||
{
|
|
||||||
UserRepository.Delete(user);
|
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -66,6 +66,7 @@ builder.Services.AddScoped<PostService>();
|
||||||
builder.Services.AddScoped<UserService>();
|
builder.Services.AddScoped<UserService>();
|
||||||
builder.Services.AddScoped<UserAuthService>();
|
builder.Services.AddScoped<UserAuthService>();
|
||||||
builder.Services.AddScoped<UserDetailsService>();
|
builder.Services.AddScoped<UserDetailsService>();
|
||||||
|
builder.Services.AddScoped<UserDeleteService>();
|
||||||
|
|
||||||
// Services / Background
|
// Services / Background
|
||||||
builder.Services.AddSingleton<AutoMailSendService>();
|
builder.Services.AddSingleton<AutoMailSendService>();
|
||||||
|
|
|
@ -130,7 +130,7 @@
|
||||||
|
|
||||||
private async Task OnDelete()
|
private async Task OnDelete()
|
||||||
{
|
{
|
||||||
await UserService.Delete(IdentityService.CurrentUser);
|
await UserService.Delete.Perform(IdentityService.CurrentUser);
|
||||||
await IdentityService.Authenticate();
|
await IdentityService.Authenticate();
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -230,7 +230,7 @@
|
||||||
|
|
||||||
private async Task Delete()
|
private async Task Delete()
|
||||||
{
|
{
|
||||||
await UserService.Delete(User!);
|
await UserService.Delete.Perform(User!);
|
||||||
await ToastService.Success("Successfully deleted user");
|
await ToastService.Success("Successfully deleted user");
|
||||||
Navigation.NavigateTo("/admin/users");
|
Navigation.NavigateTo("/admin/users");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue