From 8f388792949d4165e9de5f681a4ef7072f29531e Mon Sep 17 00:00:00 2001 From: limited_dev Date: Tue, 13 Jun 2023 16:24:38 +0200 Subject: [PATCH] feat: made accounts deleteable, other changes Signed-off-by: limited_dev --- .../Data/Repository/CommentsRepository.cs | 29 +++++----- .../Data/Repository/PostsRepository.cs | 20 +++++-- .../Pages/Accounts/DeleteAccount.razor | 57 +++++++++++++++++++ .../Pages/Accounts/DeleteAccount.razor.css | 0 .../Pages/Accounts/Login.razor | 4 +- .../Pages/Accounts/UserPage.razor | 3 + .../Shared/Components/Board.razor | 1 + .../Shared/Components/Forms/CommentForm.razor | 6 ++ .../Shared/Components/Forms/PostForm.razor | 6 ++ ImageBoardServerApp/Util/TheManager.cs | 25 ++++---- 10 files changed, 116 insertions(+), 35 deletions(-) create mode 100644 ImageBoardServerApp/Pages/Accounts/DeleteAccount.razor create mode 100644 ImageBoardServerApp/Pages/Accounts/DeleteAccount.razor.css 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