2023-01-18 13:56:24 +01:00
|
|
|
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();
|
|
|
|
}
|
2023-06-13 16:24:38 +02:00
|
|
|
|
2023-01-18 13:56:24 +01:00
|
|
|
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();
|
|
|
|
}
|
2023-06-13 16:24:38 +02:00
|
|
|
|
2023-01-18 13:56:24 +01:00
|
|
|
public static async Task<CommentData> getCommentByIdAsync(int postId)
|
|
|
|
{
|
|
|
|
await using var db = new AppDBContext();
|
2023-02-03 11:33:56 +01:00
|
|
|
return await db.Comments
|
|
|
|
.Where(comment => comment.CommentID == postId)
|
|
|
|
.Include(comment => comment.Image)
|
|
|
|
.Include(comment => comment.Post)
|
2023-02-12 14:02:01 +01:00
|
|
|
.Include(comment => comment.User)
|
|
|
|
.Include(comment => comment.Report)
|
2023-02-03 11:33:56 +01:00
|
|
|
.FirstOrDefaultAsync();
|
2023-01-18 13:56:24 +01:00
|
|
|
}
|
2023-06-13 16:24:38 +02:00
|
|
|
|
|
|
|
public static async Task<CommentData> getCommentByGETAsync(string board, int get)
|
2023-03-18 00:49:50 +01:00
|
|
|
{
|
|
|
|
await using var db = new AppDBContext();
|
|
|
|
return await db.Comments
|
|
|
|
.Where(comment => comment.GET == get)
|
|
|
|
.Where(comment => comment.Board == board)
|
|
|
|
.Include(comment => comment.Image)
|
|
|
|
.Include(comment => comment.Post)
|
|
|
|
.Include(comment => comment.User)
|
|
|
|
.Include(comment => comment.Report)
|
|
|
|
.FirstOrDefaultAsync();
|
|
|
|
}
|
2023-06-13 16:24:38 +02:00
|
|
|
|
|
|
|
public static async Task deleteCommentFromUser(UserData u)
|
2023-02-03 11:33:56 +01:00
|
|
|
{
|
|
|
|
await using var db = new AppDBContext();
|
2023-06-13 16:24:38 +02:00
|
|
|
var l = db.Users
|
|
|
|
.Where(x => x.UserID == u.UserID);
|
|
|
|
foreach (var e in l)
|
|
|
|
db.Remove(e);
|
|
|
|
}
|
2023-01-18 13:56:24 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2023-06-13 16:24:38 +02:00
|
|
|
|
2023-01-18 13:56:24 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2023-06-13 16:24:38 +02:00
|
|
|
|
2023-01-18 13:56:24 +01:00
|
|
|
public static async Task<bool> updateCommentAsync(CommentData commentToUpdate)
|
|
|
|
{
|
|
|
|
await using var db = new AppDBContext();
|
|
|
|
db.Comments.Update(commentToUpdate);
|
|
|
|
return await db.SaveChangesAsync() >= 1;
|
|
|
|
}
|
2023-06-13 16:24:38 +02:00
|
|
|
|
2023-01-18 13:56:24 +01:00
|
|
|
public static async Task<bool> deleteCommentAsync(int postId)
|
|
|
|
{
|
|
|
|
await using var db = new AppDBContext();
|
|
|
|
CommentData commentToDelete = await getCommentByIdAsync(postId);
|
2023-02-12 14:02:01 +01:00
|
|
|
if (commentToDelete.Report != null)
|
|
|
|
db.Remove(commentToDelete.Report);
|
2023-01-18 13:56:24 +01:00
|
|
|
db.Remove(commentToDelete);
|
|
|
|
return await db.SaveChangesAsync() >= 1;
|
|
|
|
}
|
|
|
|
}
|