feat: made boards dynamic and save to the db
This commit is contained in:
parent
8ebc78b754
commit
97e4b0026b
14 changed files with 107 additions and 110 deletions
|
@ -9,6 +9,7 @@ internal sealed class AppDBContext : DbContext
|
|||
public DbSet<ImageData> Images { get; set; }
|
||||
public DbSet<CommentData> Comments { get; set; }
|
||||
public DbSet<ReportData> Reports { get; set; }
|
||||
public DbSet<BoardData> Boards { get; set; }
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
|
|
|
@ -1,16 +1,24 @@
|
|||
using System.ComponentModel.DataAnnotations;
|
||||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
|
||||
namespace ImageBoardServerApp.Data;
|
||||
|
||||
public class BoardData
|
||||
{
|
||||
[Required]
|
||||
[DatabaseGenerated((DatabaseGeneratedOption.Identity))]
|
||||
[Key]
|
||||
public int BoardID { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Tag { get; set; }
|
||||
|
||||
[Required]
|
||||
public string Topic { get; set; }
|
||||
|
||||
[Required]
|
||||
public int maxThreads { get; set; }
|
||||
|
||||
|
||||
[Required]
|
||||
public bool isLocked { get; set; }
|
||||
}
|
55
ImageBoardServerApp/Data/Repository/BoardsRepository.cs
Normal file
55
ImageBoardServerApp/Data/Repository/BoardsRepository.cs
Normal file
|
@ -0,0 +1,55 @@
|
|||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace ImageBoardServerApp.Data.Repository;
|
||||
|
||||
public static class BoardsRepository
|
||||
{
|
||||
public static async Task<List<BoardData>> getBoardsAsync()
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Boards
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public static async Task<BoardData> getBoardsByIdAsync(int boardID)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Boards.FirstOrDefaultAsync(board => board.BoardID == boardID);
|
||||
}
|
||||
|
||||
public static async Task<BoardData> getBoardByTagAsync(string tag)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
return await db.Boards
|
||||
.Where(board => board.Tag == tag)
|
||||
.FirstOrDefaultAsync();
|
||||
}
|
||||
|
||||
public static async Task<int> createBoardAsync(BoardData boardToCreate)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
await db.Boards.AddAsync(boardToCreate);
|
||||
if (await db.SaveChangesAsync() >= 1)
|
||||
{
|
||||
Console.WriteLine($"Created user with ID: {boardToCreate.BoardID}");
|
||||
return boardToCreate.BoardID;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
public static async Task<bool> updateBoardAsync(BoardData boardToCreate)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
db.Boards.Update(boardToCreate);
|
||||
return await db.SaveChangesAsync() >= 1;
|
||||
}
|
||||
|
||||
public static async Task<bool> deleteBoardAsync(int boardId)
|
||||
{
|
||||
await using var db = new AppDBContext();
|
||||
BoardData boardToDelete = await getBoardsByIdAsync(boardId);
|
||||
db.Remove(boardToDelete);
|
||||
return await db.SaveChangesAsync() >= 1;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue