diff --git a/ProjectLighthouse/Database.cs b/ProjectLighthouse/Database.cs index 3a5366cc..0330497e 100644 --- a/ProjectLighthouse/Database.cs +++ b/ProjectLighthouse/Database.cs @@ -252,6 +252,37 @@ namespace LBPUnion.ProjectLighthouse public async Task 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 } } \ No newline at end of file diff --git a/ProjectLighthouse/Maintenance/Commands/DeleteUserCommand.cs b/ProjectLighthouse/Maintenance/Commands/DeleteUserCommand.cs new file mode 100644 index 00000000..a0e0f8d1 --- /dev/null +++ b/ProjectLighthouse/Maintenance/Commands/DeleteUserCommand.cs @@ -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() => ""; + 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); + } + } +} \ No newline at end of file