70 lines
2.0 KiB
C#
70 lines
2.0 KiB
C#
using BudgetApp.Data.Entities;
|
|
using BudgetApp.Data.Interfaces;
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
namespace BudgetApp.Data.Repositories;
|
|
|
|
public class AccountRepository(ApplicationDbContext db) : IAccountRepository
|
|
{
|
|
public async Task<List<Account>> GetAccountsAsync(string userId)
|
|
{
|
|
return await db.Accounts
|
|
.Where(a => a.UserId == userId)
|
|
.OrderBy(a => a.Name)
|
|
.ToListAsync();
|
|
}
|
|
|
|
public async Task<Account?> GetAccountAsync(int id, string userId)
|
|
{
|
|
return await db.Accounts
|
|
.FirstOrDefaultAsync(a => a.Id == id && a.UserId == userId);
|
|
}
|
|
|
|
public async Task CreateAccountAsync(Account account)
|
|
{
|
|
db.Accounts.Add(account);
|
|
await db.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task UpdateAccountAsync(Account account)
|
|
{
|
|
db.Accounts.Update(account);
|
|
await db.SaveChangesAsync();
|
|
}
|
|
|
|
public async Task UpdateBalanceAsync(int id, string userId, decimal newBalance)
|
|
{
|
|
var account = await db.Accounts
|
|
.FirstOrDefaultAsync(a => a.Id == id && a.UserId == userId);
|
|
if (account is not null)
|
|
{
|
|
account.CurrentBalance = newBalance;
|
|
await db.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task UpdateAccountDetailsAsync(int id, string userId, string name, string color, string currencyCode)
|
|
{
|
|
var account = await db.Accounts
|
|
.FirstOrDefaultAsync(a => a.Id == id && a.UserId == userId);
|
|
if (account is not null)
|
|
{
|
|
account.Name = name;
|
|
account.Color = color;
|
|
account.CurrencyCode = currencyCode;
|
|
await db.SaveChangesAsync();
|
|
}
|
|
}
|
|
|
|
public async Task DeleteAccountAsync(int id, string userId)
|
|
{
|
|
var account = await db.Accounts
|
|
.FirstOrDefaultAsync(a => a.Id == id && a.UserId == userId);
|
|
if (account is not null)
|
|
{
|
|
db.Accounts.Remove(account);
|
|
await db.SaveChangesAsync();
|
|
}
|
|
}
|
|
}
|