69 lines
No EOL
2.3 KiB
C#
69 lines
No EOL
2.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace ImageBoardServerApp.Data.Repository;
|
|
|
|
public static class CommentsRepository
|
|
{
|
|
public static async Task<List<CommentData>> getCommentsAsync()
|
|
{
|
|
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();
|
|
return await db.Comments
|
|
.Where(comment => comment.Board.Equals(board))
|
|
.Include(comment => comment.Image)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public static async Task<CommentData> getCommentByIdAsync(int postId)
|
|
{
|
|
await using var db = new AppDBContext();
|
|
return await db.Comments
|
|
.Where(comment => comment.CommentID == postId)
|
|
.Include(comment => comment.Image)
|
|
.Include(comment => comment.Post)
|
|
.FirstOrDefaultAsync();
|
|
}
|
|
|
|
/*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)
|
|
.FirstOrDefaultAsync();
|
|
//return await db.Posts.FirstOrDefaultAsync(post => post.PostID == postId);
|
|
}*/
|
|
|
|
public static async Task<int> createCommentAsync(CommentData commentData)
|
|
{
|
|
await using var db = new AppDBContext();
|
|
await db.Comments.AddAsync(commentData);
|
|
if (await db.SaveChangesAsync() >= 1)
|
|
{
|
|
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();
|
|
CommentData commentToDelete = await getCommentByIdAsync(postId);
|
|
db.Remove(commentToDelete);
|
|
return await db.SaveChangesAsync() >= 1;
|
|
}
|
|
} |