diff --git a/ImageBoardServerApp/Data/Repository/CommentsRepository.cs b/ImageBoardServerApp/Data/Repository/CommentsRepository.cs index 9ce47ee..a8133ad 100644 --- a/ImageBoardServerApp/Data/Repository/CommentsRepository.cs +++ b/ImageBoardServerApp/Data/Repository/CommentsRepository.cs @@ -9,7 +9,7 @@ public static class CommentsRepository await using var db = new AppDBContext(); return await db.Comments.ToListAsync(); } - + public static async Task> getCommentsByBoardAsync(string board) { await using var db = new AppDBContext(); @@ -18,7 +18,7 @@ public static class CommentsRepository .Include(comment => comment.Image) .ToListAsync(); } - + public static async Task getCommentByIdAsync(int postId) { await using var db = new AppDBContext(); @@ -30,8 +30,8 @@ public static class CommentsRepository .Include(comment => comment.Report) .FirstOrDefaultAsync(); } - - public static async Task getCommentByGETAsync(string board, int get) + + public static async Task getCommentByGETAsync(string board, int get) { await using var db = new AppDBContext(); return await db.Comments @@ -43,17 +43,15 @@ public static class CommentsRepository .Include(comment => comment.Report) .FirstOrDefaultAsync(); } - - /*public static async Task getPostByIdAsync(int postId) + + public static async Task deleteCommentFromUser(UserData u) { await using var db = new AppDBContext(); - return await db.Posts - .Where(post => post.PostID == postId) - .Include(post => post.Image) - .Include(post => post.Comments) - .FirstOrDefaultAsync(); - //return await db.Posts.FirstOrDefaultAsync(post => post.PostID == postId); - }*/ + var l = db.Users + .Where(x => x.UserID == u.UserID); + foreach (var e in l) + db.Remove(e); + } public static async Task createCommentAsync(CommentData commentData) { @@ -64,16 +62,17 @@ public static class CommentsRepository Console.WriteLine($"Created comment with ID: {commentData.PostID}"); return commentData.PostID; } + return -1; } - + public static async Task updateCommentAsync(CommentData commentToUpdate) { await using var db = new AppDBContext(); db.Comments.Update(commentToUpdate); return await db.SaveChangesAsync() >= 1; } - + public static async Task deleteCommentAsync(int postId) { await using var db = new AppDBContext(); diff --git a/ImageBoardServerApp/Data/Repository/PostsRepository.cs b/ImageBoardServerApp/Data/Repository/PostsRepository.cs index 5d97101..a6d4fc8 100644 --- a/ImageBoardServerApp/Data/Repository/PostsRepository.cs +++ b/ImageBoardServerApp/Data/Repository/PostsRepository.cs @@ -9,7 +9,7 @@ public static class PostsRepository await using var db = new AppDBContext(); return await db.Posts.ToListAsync(); } - + public static async Task> getPostsByBoardAsync(string board) { await using var db = new AppDBContext(); @@ -20,7 +20,7 @@ public static class PostsRepository .Include(post => post.User) .ToListAsync(); } - + public static async Task getPostByIdAsync(int postId) { await using var db = new AppDBContext(); @@ -32,7 +32,7 @@ public static class PostsRepository .FirstOrDefaultAsync(); //return await db.Posts.FirstOrDefaultAsync(post => post.PostID == postId); } - + public static async Task getPostByGETAsync(string board, int get) { await using var db = new AppDBContext(); @@ -55,16 +55,26 @@ public static class PostsRepository Console.WriteLine($"Created post with ID: {postToCreate.PostID}"); return postToCreate.PostID; } + return -1; } - + + public static async Task deletePostsFromUser(UserData u) + { + await using var db = new AppDBContext(); + var l = db.Posts + .Where(x => x.UserID == u.UserID); + foreach (var e in l) + db.Remove(e); + } + public static async Task updatePostAsync(PostData postToUpdate) { await using var db = new AppDBContext(); db.Posts.Update(postToUpdate); return await db.SaveChangesAsync() >= 1; } - + public static async Task deletePostAsync(int postId) { await using var db = new AppDBContext(); diff --git a/ImageBoardServerApp/Pages/Accounts/DeleteAccount.razor b/ImageBoardServerApp/Pages/Accounts/DeleteAccount.razor new file mode 100644 index 0000000..cb4b5c9 --- /dev/null +++ b/ImageBoardServerApp/Pages/Accounts/DeleteAccount.razor @@ -0,0 +1,57 @@ +@page "/sys/delacc" +@using ImageBoardServerApp.Data.Repository +@using ImageBoardServerApp.Auth +@inject IJSRuntime js +@inject AuthenticationStateProvider authStateProvider +@inject NavigationManager navManager + +Delete your account - BulletBoards +

Delete your account

+ + +@code { + private bool verified; + + public string Email { get; set; } + public string Password { get; set; } + + private async void del() + { + var user = await UsersRepository.getUserByEmailAsync(Email); + if (user == null) + { + await js.InvokeVoidAsync("alert", "User does not exist"); + verified = false; + return; + } + Console.WriteLine("loggin you in..."); + verified = BCrypt.Net.BCrypt.Verify(Password, user.Password); + if (verified) + { + await CommentsRepository.deleteCommentFromUser(user); + await PostsRepository.deletePostsFromUser(user); + + await UsersRepository.deleteUserAsync(user.UserID); + var customAuthStateProvider = (CustomAuthenticationStateProvider)authStateProvider; + await customAuthStateProvider.UpdateAuthenticationStateAsync(null); + navManager.NavigateTo("/", true); + return; + } + await js.InvokeVoidAsync("alert", $"Wrong Password"); + } + +} \ No newline at end of file diff --git a/ImageBoardServerApp/Pages/Accounts/DeleteAccount.razor.css b/ImageBoardServerApp/Pages/Accounts/DeleteAccount.razor.css new file mode 100644 index 0000000..e69de29 diff --git a/ImageBoardServerApp/Pages/Accounts/Login.razor b/ImageBoardServerApp/Pages/Accounts/Login.razor index e94d6fe..7d042ea 100644 --- a/ImageBoardServerApp/Pages/Accounts/Login.razor +++ b/ImageBoardServerApp/Pages/Accounts/Login.razor @@ -5,7 +5,7 @@ @inject AuthenticationStateProvider authStateProvider @inject NavigationManager navManager -Login - Bulletboards +Login - BulletBoards

Login to Bulletboards