bulletboards/ImageBoardServerApp/Data/Repository/AccountsRepository.cs
2023-01-19 13:00:30 +01:00

53 lines
No EOL
1.7 KiB
C#

using Microsoft.EntityFrameworkCore;
namespace ImageBoardServerApp.Data.Repository;
public static class AccountsRepository
{
public static async Task<List<AccountData>> getAccountsAsync()
{
await using var db = new AppDBContext();
return await db.Accounts.ToListAsync();
}
public static async Task<List<AccountData>> getAccountsByMailAsync(string email)
{
await using var db = new AppDBContext();
return await db.Accounts
.Where(acc => acc.Email.Equals(email))
.ToListAsync();
}
public static async Task<AccountData> getAccountByIdAsync(int accountId)
{
await using var db = new AppDBContext();
return await db.Accounts.FirstOrDefaultAsync(post => post.AccountID == accountId);
}
public static async Task<int> createAccountAsync(AccountData accountToCreate)
{
await using var db = new AppDBContext();
await db.Accounts.AddAsync(accountToCreate);
if (await db.SaveChangesAsync() >= 1)
{
Console.WriteLine($"Created post with ID: {accountToCreate.AccountID}");
return accountToCreate.AccountID;
}
return -1;
}
public static async Task<bool> updateAccountAsync(AccountData accountToUpdate)
{
await using var db = new AppDBContext();
db.Accounts.Update(accountToUpdate);
return await db.SaveChangesAsync() >= 1;
}
public static async Task<bool> deleteAccountAsync(int postId)
{
await using var db = new AppDBContext();
AccountData accountToDelete = await getAccountByIdAsync(postId);
db.Remove(accountToDelete);
return await db.SaveChangesAsync() >= 1;
}
}