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

@ -31,7 +31,7 @@ public static class CommentsRepository
.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
@ -44,16 +44,14 @@ public static class CommentsRepository
.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,6 +62,7 @@ public static class CommentsRepository
Console.WriteLine($"Created comment with ID: {commentData.PostID}");
return commentData.PostID;
}
return -1;
}

View file

@ -55,9 +55,19 @@ 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();

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);
@ -24,7 +23,9 @@ public class TheManager
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)
@ -49,15 +50,15 @@ 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);