bulletboards/ImageBoardServerApp/Data/Repository/BoardsRepository.cs

55 lines
1.7 KiB
C#
Raw Normal View History

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