bulletboards/ImageBoardServerApp/Data/Repository/PostsRepository.cs
limited_dev 396edeefde feat: started working on replys in comments, changes IDs to GETs
Signed-off-by: limited_dev <loginakkisativ@gmail.com>
2023-03-18 00:49:50 +01:00

75 lines
No EOL
2.5 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<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;
}
}