using System.Collections.Generic; using System.Linq; using FirmTracker_Server.nHibernate.Expenses; 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 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; } } } } }