feat: made accounts deleteable, other changes

Signed-off-by: limited_dev <loginakkisativ@gmail.com>
This commit is contained in:
limited_dev 2023-06-13 16:24:38 +02:00
parent c20c5c9343
commit 8f38879294
10 changed files with 116 additions and 35 deletions

View file

@ -9,7 +9,7 @@ public static class CommentsRepository
await using var db = new AppDBContext();
return await db.Comments.ToListAsync();
}
public static async Task<List<CommentData>> 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<CommentData> 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<CommentData> getCommentByGETAsync(string board, int get)
public static async Task<CommentData> 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<PostData> 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<int> 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<bool> updateCommentAsync(CommentData commentToUpdate)
{
await using var db = new AppDBContext();
db.Comments.Update(commentToUpdate);
return await db.SaveChangesAsync() >= 1;
}
public static async Task<bool> deleteCommentAsync(int postId)
{
await using var db = new AppDBContext();

View file

@ -9,7 +9,7 @@ public static class PostsRepository
await using var db = new AppDBContext();
return await db.Posts.ToListAsync();
}
public static async Task<List<PostData>> 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<PostData> 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<PostData> 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<bool> updatePostAsync(PostData postToUpdate)
{
await using var db = new AppDBContext();
db.Posts.Update(postToUpdate);
return await db.SaveChangesAsync() >= 1;
}
public static async Task<bool> deletePostAsync(int postId)
{
await using var db = new AppDBContext();

View file

@ -0,0 +1,57 @@
@page "/sys/delacc"
@using ImageBoardServerApp.Data.Repository
@using ImageBoardServerApp.Auth
@inject IJSRuntime js
@inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navManager
<PageTitle>Delete your account - BulletBoards</PageTitle>
<h3 class="headLogin">Delete your account</h3>
<div class="login">
<form>
<RadzenFormField Text="Email" Variant="Variant.Outlined">
<RadzenTextBox @bind-Value="@Email"/>
</RadzenFormField>
<br/>
<RadzenFormField Text="Password" Variant="Variant.Outlined">
<RadzenPassword @bind-Value="@Password"/>
</RadzenFormField>
<br/>
<br/>
<RadzenButton Click=@del Text="delete account" ButtonStyle="ButtonStyle.Secondary"/>
<br/>
</form>
</div>
@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");
}
}

View file

@ -5,7 +5,7 @@
@inject AuthenticationStateProvider authStateProvider
@inject NavigationManager navManager
<PageTitle>Login - Bulletboards</PageTitle>
<PageTitle>Login - BulletBoards</PageTitle>
<h3 class="headLogin">Login to Bulletboards</h3>
<div class="login">
<form>
@ -25,8 +25,6 @@
<a href="/sys/resetpw">Reset Password</a>
@code {
private Variant vari = Variant.Outlined;
private bool verified;
public string Email { get; set; }

View file

@ -40,6 +40,9 @@
<br/>
<br/>
<a href="/sys/logout">[Logout]</a>
<br/>
<br/>
<a href="/sys/delacc">[Delete Account]</a>
</Authorized>
<NotAuthorized>

View file

@ -39,6 +39,7 @@ else
private List<PostData> posts;
protected override async Task OnParametersSetAsync()
{
try

View file

@ -78,6 +78,12 @@
return;
}
postUsername = foundusr.LastUsedName;
if (!foundusr.ConfirmedEmail)
{
hasErr = true;
postErr = "You cannot post without an verified email.";
return;
}
await base.OnAfterRenderAsync(firstRender);
}

View file

@ -84,6 +84,12 @@
return;
}
postUsername = foundusr.LastUsedName;
if (!foundusr.ConfirmedEmail)
{
hasErr = true;
postErr = "You cannot post without an verified email.";
return;
}
await base.OnAfterRenderAsync(firstRender);
}

View file

@ -6,7 +6,6 @@ namespace ImageBoardServerApp.Util;
public class TheManager
{
private static long getDiff(PostData post)
{
return (DateTimeOffset.Now.ToUnixTimeMilliseconds() - post.CreatedAt);
@ -21,10 +20,12 @@ public class TheManager
{
return num < 0 ? num * -1 : num;
}
public static long getBumpValue(PostData post)
{
return (post.IsSticky ? 999999999999999999 + getDiff(post) : 10 * 60000 - getDiff(post) + ( 60000 * (post.Comments.Count + 1))) ;
return (post.IsSticky
? 999999999999999999 + getDiff(post)
: 10 * 60000 - getDiff(post) + (60000 * (post.Comments.Count + 1)));
}
public static async Task<List<PostData>> getPostList(string boardTag)
@ -46,18 +47,18 @@ public class TheManager
}
}
}
public static async Task deleteThread(PostData post)
{
foreach(var c in post.Comments)
foreach (var c in post.Comments)
{
if (c.Image != null)
{
deleteImage(c.Image);
await deleteImage(c.Image);
}
}
deleteImage(post.Image);
await deleteImage(post.Image);
await PostsRepository.deletePostAsync(post.PostID);
}
@ -66,16 +67,16 @@ public class TheManager
{
if (comment.Image != null)
{
deleteImage(comment.Image);
await deleteImage(comment.Image);
}
await CommentsRepository.deleteCommentAsync(comment.CommentID);
}
public static void deleteImage(ImageData imageData)
public static async Task deleteImage(ImageData imageData)
{
string path = $"./wwwroot{imageData.ImageLocation}";
Console.WriteLine(path);
try
{
File.Delete(path);
@ -102,7 +103,7 @@ public class TheManager
{
rng.GetBytes(bytes);
}
return BitConverter.ToString(bytes).Replace("-", "").ToLower();
}
}