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,114 @@
|
||||
using BudgetApp.Data.Entities;
|
||||
using BudgetApp.Data.Interfaces;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace BudgetApp.Data.Repositories;
|
||||
|
||||
public class CategoryRepository(ApplicationDbContext db) : ICategoryRepository
|
||||
{
|
||||
private static readonly (string Name, string Icon)[] DefaultCategories =
|
||||
[
|
||||
("Groceries", "🛒"),
|
||||
("Rent / Mortgage", "🏠"),
|
||||
("Utilities", "💡"),
|
||||
("Transport", "🚗"),
|
||||
("Eating Out", "🍽️"),
|
||||
("Entertainment", "🎬"),
|
||||
("Health", "🏥"),
|
||||
("Insurance", "🛡️"),
|
||||
("Salary", "💰"),
|
||||
("Savings", "🏦"),
|
||||
("Subscriptions", "📱"),
|
||||
("Shopping", "🛍️"),
|
||||
("Education", "📚"),
|
||||
("Travel", "✈️"),
|
||||
("Gifts", "🎁"),
|
||||
("Loan", "🏧"),
|
||||
("Credit Card", "💳"),
|
||||
("Childcare", "👶")
|
||||
];
|
||||
|
||||
public async Task<List<Category>> GetCategoriesAsync(string userId)
|
||||
{
|
||||
return await db.Categories
|
||||
.Where(c => c.UserId == userId)
|
||||
.OrderBy(c => c.Name)
|
||||
.ToListAsync();
|
||||
}
|
||||
|
||||
public async Task EnsureDefaultCategoriesAsync(string userId)
|
||||
{
|
||||
var existing = await db.Categories
|
||||
.Where(c => c.UserId == userId)
|
||||
.ToListAsync();
|
||||
|
||||
if (existing.Count == 0)
|
||||
{
|
||||
var defaults = DefaultCategories.Select(d => new Category
|
||||
{
|
||||
Name = d.Name,
|
||||
Icon = d.Icon,
|
||||
IsDefault = true,
|
||||
UserId = userId
|
||||
});
|
||||
|
||||
db.Categories.AddRange(defaults);
|
||||
await db.SaveChangesAsync();
|
||||
return;
|
||||
}
|
||||
|
||||
// Backfill icons and IsDefault on existing default categories
|
||||
var defaultLookup = DefaultCategories.ToDictionary(d => d.Name, d => d.Icon);
|
||||
bool updated = false;
|
||||
|
||||
foreach (var category in existing)
|
||||
{
|
||||
if (string.IsNullOrEmpty(category.Icon) && defaultLookup.TryGetValue(category.Name, out var icon))
|
||||
{
|
||||
category.Icon = icon;
|
||||
updated = true;
|
||||
}
|
||||
if (!category.IsDefault && defaultLookup.ContainsKey(category.Name))
|
||||
{
|
||||
category.IsDefault = true;
|
||||
updated = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (updated)
|
||||
{
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CreateCategoryAsync(Category category)
|
||||
{
|
||||
db.Categories.Add(category);
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task UpdateCategoryAsync(int id, string userId, string name, string icon)
|
||||
{
|
||||
var category = await db.Categories
|
||||
.FirstOrDefaultAsync(c => c.Id == id && c.UserId == userId);
|
||||
if (category is not null)
|
||||
{
|
||||
if (category.IsDefault) return;
|
||||
category.Name = name;
|
||||
category.Icon = icon;
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteCategoryAsync(int id, string userId)
|
||||
{
|
||||
var category = await db.Categories
|
||||
.FirstOrDefaultAsync(c => c.Id == id && c.UserId == userId);
|
||||
if (category is not null)
|
||||
{
|
||||
if (category.IsDefault) return;
|
||||
db.Categories.Remove(category);
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user