feat: added Reports to DB

This commit is contained in:
limited_dev 2023-02-11 15:47:39 +01:00
parent e21ac0d3d7
commit 963711adfb
5 changed files with 67 additions and 0 deletions

View file

@ -8,6 +8,7 @@ internal sealed class AppDBContext : DbContext
public DbSet<PostData> Posts { get; set; } public DbSet<PostData> Posts { get; set; }
public DbSet<ImageData> Images { get; set; } public DbSet<ImageData> Images { get; set; }
public DbSet<CommentData> Comments { get; set; } public DbSet<CommentData> Comments { get; set; }
public DbSet<ReportData> Reports { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlite("Data Source=./Data/Nils.db"); => optionsBuilder.UseSqlite("Data Source=./Data/Nils.db");
@ -31,5 +32,18 @@ internal sealed class AppDBContext : DbContext
mb.Entity<UserData>() mb.Entity<UserData>()
.HasMany(user => user.Comments) .HasMany(user => user.Comments)
.WithOne(comment => comment.User); .WithOne(comment => comment.User);
mb.Entity<ReportData>()
.HasOne(report => report.UserReported)
.WithMany(user => user.RecivedReports);
mb.Entity<ReportData>()
.HasOne(report => report.UserReporter)
.WithMany(user => user.SubmittedReports);
mb.Entity<ReportData>()
.HasOne(report => report.ReportedPost)
.WithOne(post => post.Report);
mb.Entity<ReportData>()
.HasOne(report => report.ReportedComment)
.WithOne(comment => comment.Report);
} }
} }

View file

@ -39,4 +39,6 @@ public class CommentData
[Required] [Required]
public long CreatedAt { get; set; } public long CreatedAt { get; set; }
public ReportData? Report { get; set; }
} }

View file

@ -40,4 +40,6 @@ public class PostData
public string Board { get; set; } public string Board { get; set; }
public List<CommentData> Comments { get; set; } public List<CommentData> Comments { get; set; }
public ReportData? Report { get; set; }
} }

View file

@ -0,0 +1,45 @@
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.ToListAsync();
}
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;
}
}

View file

@ -33,4 +33,8 @@ public class UserData
[Required] [Required]
public string Role { get; set; } public string Role { get; set; }
public List<ReportData> SubmittedReports { get; set; }
public List<ReportData> RecivedReports { get; set; }
} }