bulletboards/ImageBoardServerApp/Data/Repository/PostsRepository.cs
limited_dev 8f38879294 feat: made accounts deleteable, other changes
Signed-off-by: limited_dev <loginakkisativ@gmail.com>
2023-06-13 16:24:38 +02:00

85 lines
No EOL
2.7 KiB
C#

using Microsoft.EntityFrameworkCore;
namespace ImageBoardServerApp.Data.Repository;
public static class PostsRepository
{
public static async Task<List<PostData>> getPostsAsync()
{
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();
return await db.Posts
.Where(post => post.Board.Equals(board))
.Include(post => post.Image)
.Include(post => post.Comments)
.Include(post => post.User)
.ToListAsync();
}
public static async Task<PostData> getPostByIdAsync(int postId)
{
await using var db = new AppDBContext();
return await db.Posts
.Where(post => post.PostID == postId)
.Include(post => post.Image)
.Include(post => post.Comments)
.Include(post => post.User)
.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();
return await db.Posts
.Where(post => post.GET == get)
.Where(post => post.Board == board)
.Include(post => post.Image)
.Include(post => post.Comments)
.Include(post => post.User)
.FirstOrDefaultAsync();
//return await db.Posts.FirstOrDefaultAsync(post => post.PostID == postId);
}
public static async Task<int> createPostAsync(PostData postToCreate)
{
await using var db = new AppDBContext();
await db.Posts.AddAsync(postToCreate);
if (await db.SaveChangesAsync() >= 1)
{
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();
PostData postToDelete = await getPostByIdAsync(postId);
db.Remove(postToDelete);
return await db.SaveChangesAsync() >= 1;
}
}