Add project files.
This commit is contained in:
1 parent
fd9f318d6a
commit
0ce077ec11
98 files changed
+65707
No files matched your search
@@ -0,0 +1,75 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using BudgetApp.Data;
|
||||
|
||||
namespace BudgetApp.Services;
|
||||
|
||||
public class AccountService
|
||||
{
|
||||
private readonly ApplicationDbContext _db;
|
||||
|
||||
public AccountService(ApplicationDbContext db)
|
||||
{
|
||||
_db = db;
|
||||
}
|
||||
|
||||
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