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
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