bulletboards/ImageBoardServerApp/Data/Repository/ReportsRepository.cs

53 lines
1.7 KiB
C#
Raw Normal View History

2023-02-11 14:47:39 +00:00
using Microsoft.EntityFrameworkCore;
namespace ImageBoardServerApp.Data.Repository;
public static class ReportsRepository
{
public static async Task<List<ReportData>> getReportsAsync()
{
await using var db = new AppDBContext();
return await db.Reports
.Include(report => report.ReportedPost)
.Include(report => report.ReportedPost.Image)
.Include(report => report.ReportedPost.Comments)
.Include(report => report.ReportedComment)
.Include(report => report.ReportedComment.Image)
.Include(report => report.UserReported)
.Include(report => report.UserReporter)
.ToListAsync();
2023-02-11 14:47:39 +00:00
}
public static async Task<ReportData> getReportByIdAsync(int reportId)
{
await using var db = new AppDBContext();
return await db.Reports.FirstOrDefaultAsync(report => report.ReportID == reportId);
}
public static async Task<int> createReportAsync(ReportData reportData)
{
await using var db = new AppDBContext();
await db.Reports.AddAsync(reportData);
if (await db.SaveChangesAsync() >= 1)
{
return reportData.ReportID;
}
return -1;
}
public static async Task<bool> updateReportAsync(ReportData reportData)
{
await using var db = new AppDBContext();
db.Reports.Update(reportData);
return await db.SaveChangesAsync() >= 1;
}
public static async Task<bool> deleteReportAsync(int reportId)
{
await using var db = new AppDBContext();
ReportData reportData = await getReportByIdAsync(reportId);
db.Remove(reportData);
return await db.SaveChangesAsync() >= 1;
}
}