Create a new AccountDetails page that shows account information and all linked cards. This replaces the standalone Cards page and nests card management under accounts, as cards are always linked to accounts. Features: - View account details (institution, type, owner, etc.) - List all cards linked to the account - Add new cards directly from the account page - Edit and delete cards within the account context - Click-through navigation from Accounts list 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
97 lines
2.9 KiB
C#
97 lines
2.9 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using MoneyMap.Data;
|
|
using MoneyMap.Models;
|
|
|
|
namespace MoneyMap.Pages
|
|
{
|
|
public class AccountDetailsModel : PageModel
|
|
{
|
|
private readonly MoneyMapContext _db;
|
|
|
|
public AccountDetailsModel(MoneyMapContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public Account Account { get; set; } = null!;
|
|
public List<CardWithStats> Cards { get; set; } = new();
|
|
public int TransactionCount { get; set; }
|
|
|
|
[TempData]
|
|
public string? SuccessMessage { get; set; }
|
|
|
|
[TempData]
|
|
public string? ErrorMessage { get; set; }
|
|
|
|
public async Task<IActionResult> OnGetAsync(int id)
|
|
{
|
|
var account = await _db.Accounts.FindAsync(id);
|
|
if (account == null)
|
|
return NotFound();
|
|
|
|
Account = account;
|
|
|
|
// Get cards linked to this account
|
|
var cards = await _db.Cards
|
|
.Where(c => c.AccountId == id)
|
|
.OrderBy(c => c.Owner)
|
|
.ThenBy(c => c.Last4)
|
|
.ToListAsync();
|
|
|
|
var cardStats = new List<CardWithStats>();
|
|
foreach (var card in cards)
|
|
{
|
|
var transactionCount = await _db.Transactions.CountAsync(t => t.CardId == card.Id);
|
|
cardStats.Add(new CardWithStats
|
|
{
|
|
Card = card,
|
|
TransactionCount = transactionCount
|
|
});
|
|
}
|
|
|
|
Cards = cardStats;
|
|
|
|
// Get transaction count for this account
|
|
TransactionCount = await _db.Transactions.CountAsync(t => t.AccountId == id);
|
|
|
|
return Page();
|
|
}
|
|
|
|
public async Task<IActionResult> OnPostDeleteCardAsync(int cardId)
|
|
{
|
|
var card = await _db.Cards.FindAsync(cardId);
|
|
if (card == null)
|
|
{
|
|
ErrorMessage = "Card not found.";
|
|
return RedirectToPage(new { id = card?.AccountId });
|
|
}
|
|
|
|
var accountId = card.AccountId;
|
|
|
|
var transactionCount = await _db.Transactions.CountAsync(t => t.CardId == card.Id);
|
|
if (transactionCount > 0)
|
|
{
|
|
ErrorMessage = $"Cannot delete card. It has {transactionCount} transaction(s) associated with it.";
|
|
return RedirectToPage(new { id = accountId });
|
|
}
|
|
|
|
_db.Cards.Remove(card);
|
|
await _db.SaveChangesAsync();
|
|
|
|
SuccessMessage = "Card deleted successfully.";
|
|
return RedirectToPage(new { id = accountId });
|
|
}
|
|
|
|
public class CardWithStats
|
|
{
|
|
public Card Card { get; set; } = null!;
|
|
public int TransactionCount { get; set; }
|
|
}
|
|
}
|
|
}
|