Refactor. Add data layer
This commit is contained in:
1 parent
3b7e4e6ec5
commit
b6b794f0b4
82 files changed
+2930
-2811
No files matched your search
@@ -0,0 +1,69 @@
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user