3b01efd8a6
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
80 lines
2.3 KiB
C#
80 lines
2.3 KiB
C#
using Microsoft.EntityFrameworkCore;
|
|
using MoneyMap.Data;
|
|
using MoneyMap.Models;
|
|
|
|
namespace MoneyMap.Services;
|
|
|
|
/// <summary>
|
|
/// Service for core transaction operations including duplicate detection,
|
|
/// retrieval, and deletion.
|
|
/// </summary>
|
|
public interface ITransactionService
|
|
{
|
|
/// <summary>
|
|
/// Checks if a transaction is a duplicate based on date, amount, name, memo,
|
|
/// account, and card.
|
|
/// </summary>
|
|
Task<bool> IsDuplicateAsync(Transaction transaction);
|
|
|
|
/// <summary>
|
|
/// Gets a transaction by ID with optional related data.
|
|
/// </summary>
|
|
Task<Transaction?> GetTransactionByIdAsync(long id, bool includeRelated = false);
|
|
|
|
/// <summary>
|
|
/// Deletes a transaction and all related data (receipts, parse logs, line items).
|
|
/// </summary>
|
|
Task<bool> DeleteTransactionAsync(long id);
|
|
}
|
|
|
|
public class TransactionService : ITransactionService
|
|
{
|
|
private readonly MoneyMapContext _db;
|
|
|
|
public TransactionService(MoneyMapContext db)
|
|
{
|
|
_db = db;
|
|
}
|
|
|
|
public async Task<bool> IsDuplicateAsync(Transaction transaction)
|
|
{
|
|
return await _db.Transactions.AnyAsync(t =>
|
|
t.Date == transaction.Date &&
|
|
t.Amount == transaction.Amount &&
|
|
t.Name == transaction.Name &&
|
|
t.Memo == transaction.Memo &&
|
|
t.AccountId == transaction.AccountId &&
|
|
t.CardId == transaction.CardId);
|
|
}
|
|
|
|
public async Task<Transaction?> GetTransactionByIdAsync(long id, bool includeRelated = false)
|
|
{
|
|
var query = _db.Transactions.AsQueryable();
|
|
|
|
if (includeRelated)
|
|
{
|
|
query = query
|
|
.Include(t => t.Card)
|
|
.ThenInclude(c => c!.Account)
|
|
.Include(t => t.Account)
|
|
.Include(t => t.TransferToAccount)
|
|
.Include(t => t.Merchant)
|
|
.Include(t => t.Receipts)
|
|
.ThenInclude(r => r.LineItems);
|
|
}
|
|
|
|
return await query.FirstOrDefaultAsync(t => t.Id == id);
|
|
}
|
|
|
|
public async Task<bool> DeleteTransactionAsync(long id)
|
|
{
|
|
var transaction = await _db.Transactions.FindAsync(id);
|
|
if (transaction == null)
|
|
return false;
|
|
|
|
_db.Transactions.Remove(transaction);
|
|
await _db.SaveChangesAsync();
|
|
return true;
|
|
}
|
|
}
|