mirror of
https://github.com/LBPUnion/ProjectLighthouse.git
synced 2025-08-02 18:18:39 +00:00
Add DeleteUserCommand
This commit is contained in:
parent
f83bd15bc1
commit
0b91fa3367
2 changed files with 73 additions and 0 deletions
|
@ -252,6 +252,37 @@ namespace LBPUnion.ProjectLighthouse
|
|||
|
||||
public async Task<Photo?> PhotoFromSubject(PhotoSubject subject)
|
||||
=> await this.Photos.FirstOrDefaultAsync(p => p.PhotoSubjectIds.Contains(subject.PhotoSubjectId.ToString()));
|
||||
|
||||
public async Task RemoveUser(User user)
|
||||
{
|
||||
this.Locations.Remove(user.Location);
|
||||
LastContact? lastContact = await this.LastContacts.FirstOrDefaultAsync(l => l.UserId == user.UserId);
|
||||
if (lastContact != null) this.LastContacts.Remove(lastContact);
|
||||
|
||||
foreach (Slot slot in this.Slots.Where(s => s.CreatorId == user.UserId)) await this.RemoveSlot(slot, false);
|
||||
|
||||
this.AuthenticationAttempts.RemoveRange(this.AuthenticationAttempts.Include(a => a.GameToken).Where(a => a.GameToken.UserId == user.UserId));
|
||||
this.HeartedProfiles.RemoveRange(this.HeartedProfiles.Where(h => h.UserId == user.UserId));
|
||||
this.PhotoSubjects.RemoveRange(this.PhotoSubjects.Where(s => s.UserId == user.UserId));
|
||||
this.HeartedLevels.RemoveRange(this.HeartedLevels.Where(h => h.UserId == user.UserId));
|
||||
this.VisitedLevels.RemoveRange(this.VisitedLevels.Where(v => v.UserId == user.UserId));
|
||||
this.QueuedLevels.RemoveRange(this.QueuedLevels.Where(q => q.UserId == user.UserId));
|
||||
this.RatedLevels.RemoveRange(this.RatedLevels.Where(r => r.UserId == user.UserId));
|
||||
this.GameTokens.RemoveRange(this.GameTokens.Where(t => t.UserId == user.UserId));
|
||||
this.WebTokens.RemoveRange(this.WebTokens.Where(t => t.UserId == user.UserId));
|
||||
this.Comments.RemoveRange(this.Comments.Where(c => c.PosterUserId == user.UserId));
|
||||
this.Photos.RemoveRange(this.Photos.Where(p => p.CreatorId == user.UserId));
|
||||
|
||||
await this.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task RemoveSlot(Slot slot, bool saveChanges = true)
|
||||
{
|
||||
if (slot.Location != null) this.Locations.Remove(slot.Location);
|
||||
this.Slots.Remove(slot);
|
||||
|
||||
if (saveChanges) await this.SaveChangesAsync();
|
||||
}
|
||||
#nullable disable
|
||||
}
|
||||
}
|
42
ProjectLighthouse/Maintenance/Commands/DeleteUserCommand.cs
Normal file
42
ProjectLighthouse/Maintenance/Commands/DeleteUserCommand.cs
Normal file
|
@ -0,0 +1,42 @@
|
|||
#nullable enable
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using JetBrains.Annotations;
|
||||
using LBPUnion.ProjectLighthouse.Types;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace LBPUnion.ProjectLighthouse.Maintenance.Commands
|
||||
{
|
||||
[UsedImplicitly]
|
||||
public class DeleteUserCommand : ICommand
|
||||
{
|
||||
private readonly Database database = new();
|
||||
public string Name() => "Delete/Ban User";
|
||||
public string[] Aliases()
|
||||
=> new[]
|
||||
{
|
||||
"deleteUser", "wipeUser", "banUser",
|
||||
};
|
||||
public string Arguments() => "<username/userId>";
|
||||
public int RequiredArgs() => 1;
|
||||
public async Task Run(string[] args)
|
||||
{
|
||||
User? user = await this.database.Users.FirstOrDefaultAsync(u => u.Username == args[0]);
|
||||
if (user == null)
|
||||
{
|
||||
try
|
||||
{
|
||||
user = await this.database.Users.FirstOrDefaultAsync(u => u.UserId == Convert.ToInt32(args[0]));
|
||||
if (user == null) throw new Exception();
|
||||
}
|
||||
catch
|
||||
{
|
||||
Console.WriteLine($"Could not find user by parameter '{args[0]}'");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
await this.database.RemoveUser(user);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue