using System.Collections.Generic; using System.Linq; using FirmTracker_Server.nHibernate.Expenses; using FirmTracker_Server.nHibernate.Transactions; using NHibernate; namespace FirmTracker_Server.nHibernate { public interface IExpenseRepository { List GetAllExpenses(); Expense GetExpense(int expenseId); void AddExpense(Expense expense); void UpdateExpense(Expense expense); void DeleteExpense(int expenseId); } public interface ITransactionRepository { List GetAllTransactions(); Transaction GetTransaction(int transactionId); List GetTransactionsByDateRange(DateTime startDate, DateTime endDate); List GetTransactionProducts(int transactionId); void AddTransaction(Transaction transaction); void UpdateTransaction(Transaction transaction); void DeleteTransaction(int transactionId); List GetTransactionProductsForTransactions(List transactionIds); } public class TransactionRepository : ITransactionRepository { // Retrieve all transactions public List GetAllTransactions() { using (var session = SessionFactory.OpenSession()) { return session.Query().ToList(); } } public List GetTransactionProductsForTransactions(List transactionIds) { using (var session = SessionFactory.OpenSession()) { return session.Query() .Where(tp => transactionIds.Contains(tp.TransactionId)) .ToList(); } } // Retrieve a specific transaction by ID public Transaction GetTransaction(int transactionId) { using (var session = SessionFactory.OpenSession()) { return session.Get(transactionId); } } // Retrieve transactions within a specific date range public List GetTransactionsByDateRange(DateTime startDate, DateTime endDate) { using (var session = SessionFactory.OpenSession()) { return session.Query() .Where(t => t.Date >= startDate && t.Date <= endDate) .ToList(); } } // Retrieve all products for a specific transaction public List GetTransactionProducts(int transactionId) { using (var session = SessionFactory.OpenSession()) { return session.Query() .Where(tp => tp.TransactionId == transactionId) .ToList(); } } // Add a new transaction public void AddTransaction(Transaction transaction) { using (var session = SessionFactory.OpenSession()) using (var transactionScope = session.BeginTransaction()) { try { session.Save(transaction); transactionScope.Commit(); } catch { transactionScope.Rollback(); throw; } } } // Update an existing transaction public void UpdateTransaction(Transaction transaction) { using (var session = SessionFactory.OpenSession()) using (var transactionScope = session.BeginTransaction()) { try { session.Update(transaction); transactionScope.Commit(); } catch { transactionScope.Rollback(); throw; } } } // Delete a transaction by ID public void DeleteTransaction(int transactionId) { using (var session = SessionFactory.OpenSession()) using (var transactionScope = session.BeginTransaction()) { try { var transaction = session.Get(transactionId); if (transaction != null) { session.Delete(transaction); } transactionScope.Commit(); } catch { transactionScope.Rollback(); throw; } } } } public class ExpenseRepository : IExpenseRepository { // Retrieve all expenses public List GetAllExpenses() { using (var session = SessionFactory.OpenSession()) { return session.Query().ToList(); } } // Retrieve a specific expense by ID public Expense GetExpense(int expenseId) { using (var session = SessionFactory.OpenSession()) { return session.Get(expenseId); } } // Add a new expense public void AddExpense(Expense expense) { using (var session = SessionFactory.OpenSession()) using (var transaction = session.BeginTransaction()) { try { session.Save(expense); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } // Update an existing expense public void UpdateExpense(Expense expense) { using (var session = SessionFactory.OpenSession()) using (var transaction = session.BeginTransaction()) { try { session.Update(expense); transaction.Commit(); } catch { transaction.Rollback(); throw; } } } // Delete an expense by ID public void DeleteExpense(int expenseId) { using (var session = SessionFactory.OpenSession()) using (var transaction = session.BeginTransaction()) { try { var expense = session.Get(expenseId); if (expense != null) { session.Delete(expense); } transaction.Commit(); } catch { transaction.Rollback(); throw; } } } } }