Merge pull request #332 from Moonlight-Panel/ImproveUserDelete
Improved user delete
This commit is contained in:
commit
b580781618
6 changed files with 117 additions and 9 deletions
|
@ -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);
|
||||
}
|
||||
|
|
112
Moonlight/App/Services/Users/UserDeleteService.cs
Normal file
112
Moonlight/App/Services/Users/UserDeleteService.cs
Normal file
|
@ -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<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
|
||||
|
||||
// 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);
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ public class UserService
|
|||
|
||||
public UserAuthService Auth => ServiceProvider.GetRequiredService<UserAuthService>();
|
||||
public UserDetailsService Details => ServiceProvider.GetRequiredService<UserDetailsService>();
|
||||
public UserDeleteService Delete => ServiceProvider.GetRequiredService<UserDeleteService>();
|
||||
|
||||
public UserService(
|
||||
Repository<User> userRepository,
|
||||
|
@ -41,10 +42,4 @@ public class UserService
|
|||
|
||||
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<UserAuthService>();
|
||||
builder.Services.AddScoped<UserDetailsService>();
|
||||
builder.Services.AddScoped<UserDeleteService>();
|
||||
|
||||
// Services / Background
|
||||
builder.Services.AddSingleton<AutoMailSendService>();
|
||||
|
|
|
@ -130,7 +130,7 @@
|
|||
|
||||
private async Task OnDelete()
|
||||
{
|
||||
await UserService.Delete(IdentityService.CurrentUser);
|
||||
await UserService.Delete.Perform(IdentityService.CurrentUser);
|
||||
await IdentityService.Authenticate();
|
||||
}
|
||||
}
|
|
@ -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");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue