FirmTracker-Server/nHIbernate/Expenses/ExpenseCRUD.cs

108 lines
3.2 KiB
C#
Raw Normal View History

using FirmTracker_Server.nHibernate;
2024-05-16 21:25:07 +02:00
using FirmTracker_Server.nHibernate.Products;
namespace FirmTracker_Server.nHibernate.Expenses
{
public class ExpenseCRUD
{
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;
}
}
}
public Expense GetExpense(int expenseId) {
using (var session = SessionFactory.OpenSession())
{
/*var query = session.CreateQuery(@"
SELECT e
FROM Expense e
WHERE e.Id = expenseId
");
query.SetParameter("expenseId", expenseId);
var expense = query.UniqueResult<Expense>();*/
return session.Get<Expense>(expenseId);
}
}
2024-05-16 21:25:07 +02:00
public DateTime GetExpenseDate(int expenseId)
{
using(var session = SessionFactory.OpenSession())
{
var expense = session.Query<Expense>()
.Where(e => e.Id == expenseId)
.Select(e => e.Date)
.FirstOrDefault();
return expense;
}
}
public decimal GetExpenseValue(int expenseId)
{
using (var session = SessionFactory.OpenSession())
{
var expense = session.Query<Expense>()
.Where(e => e.Id == expenseId)
.Select(e => e.Value)
.FirstOrDefault();
return 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;
}
}
}
public void DeleteExpense(int expenseId)
{
using (var session = SessionFactory.OpenSession())
using (var transaction = session.BeginTransaction())
try
{
var expense = session.Get<Expense>(expenseId);
if (expense != null)
{
session.Delete(expense);
transaction.Commit();
}
}
catch {
transaction.Rollback();
throw;
}
}
public IList<Expense> GetAllExpenses()
{
using (var session = SessionFactory.OpenSession())
{
return session.Query<Expense>().ToList();
}
}
}
}