diff --git a/Controllers/ExpenseController.cs b/Controllers/ExpenseController.cs
index 3e2e1b0..cb6b8e6 100644
--- a/Controllers/ExpenseController.cs
+++ b/Controllers/ExpenseController.cs
@@ -40,5 +40,59 @@ namespace FirmTracker_Server.Controllers
}
return Ok(expense);
}
+
+ //PUT: api/Expenses
+ [HttpPut("{id}")]
+ [ProducesResponseType(200)]
+ [ProducesResponseType(200)]
+ public IActionResult UpdateExpense(int id, [FromBody] Expense expense)
+ {
+ if (id != expense.Id)
+ {
+ return BadRequest("Expense ID mismatch");
+ }
+ try
+ {
+ _expenseCrud.UpdateExpense(expense);
+ return NoContent();
+ }
+ catch (System.Exception ex)
+ {
+ return BadRequest(ex.Message);
+ }
+ }
+
+ [HttpDelete("{id}")]
+ [ProducesResponseType(200)]
+ [ProducesResponseType(400)]
+ public IActionResult DeleteExpense(int id)
+ {
+ try
+ {
+ _expenseCrud.DeleteExpense(id);
+ return NoContent();
+ }
+ catch (System.Exception ex)
+ {
+ return NotFound(ex.Message);
+ }
+ }
+
+ [HttpGet]
+ [ProducesResponseType(200)]
+ [ProducesResponseType(400)]
+ public IActionResult GetAllExpenses()
+ {
+ try
+ {
+ var expenses = _expenseCrud.GetAllExpenses();
+ return Ok(expenses);
+ }
+ catch (System.Exception ex)
+ {
+ return BadRequest(ex.Message);
+ }
+ }
}
+
}
diff --git a/FirmTracker-Server.csproj b/FirmTracker-Server.csproj
index e6f7bb1..c4a467b 100644
--- a/FirmTracker-Server.csproj
+++ b/FirmTracker-Server.csproj
@@ -8,6 +8,10 @@
08986e21-848b-485a-a219-03e2dc6041e4
+
+
+
+
diff --git a/TestClass.cs b/TestClass.cs
index 7188bf4..a79333f 100644
--- a/TestClass.cs
+++ b/TestClass.cs
@@ -29,6 +29,14 @@ namespace FirmTracker_Server
Type = 0,
Availability = 0
};
+ var product3 = new nHibernate.Products.Product
+ {
+ Name = "Produkt 2",
+ Description = "produkt",
+ Price = 16.50m,
+ Type = 1,
+ Availability = 20
+ };
var transaction1 = new Transaction
{
Date = DateTime.Now,
@@ -37,35 +45,57 @@ namespace FirmTracker_Server
EmployeeId = 1,
PaymentType = "Karta kredytowa",
};
+ var transaction2 = new Transaction
+ {
+ Date = DateTime.Now,
+ Description = "testowa transakcja",
+ Discount = 30,
+ EmployeeId = 2,
+ PaymentType = "Gotówka",
+ };
var expense1 = new Expense
{
Date = DateTime.Now,
- TotalPrice = 10.5m,
+ Value = 1003.9m,
Description = "testowy rozchód"
};
try
{
- FirmTracker_Server.nHibernate.Products.ProductCRUD crud = new ProductCRUD();
+ FirmTracker_Server.nHibernate.Products.ProductCRUD productCrud = new ProductCRUD();
FirmTracker_Server.nHibernate.Transactions.TransactionCRUD transactionCrud = new nHibernate.Transactions.TransactionCRUD();
ExpenseCRUD expenseCrud = new ExpenseCRUD();
- crud.AddProduct(product);
- crud.AddProduct(product2);
+ productCrud.AddProduct(product);
+ productCrud.AddProduct(product2);
+ productCrud.AddProduct(product3);
transactionCrud.AddTransaction(transaction1);
+ transactionCrud.AddTransaction(transaction2);
expenseCrud.AddExpense(expense1);
List testTransactionProducts = new List {
new TransactionProduct { ProductID = 1, Quantity = 2 },
- new TransactionProduct { ProductID = 2, Quantity = 1 }
+ new TransactionProduct { ProductID = 2, Quantity = 1 },
+ new TransactionProduct { ProductID = 3, Quantity = 10 }
};
foreach (var transactionProduct in testTransactionProducts)
{
transactionCrud.AddTransactionProductToTransaction(transaction1.Id, transactionProduct);
}
-
+
+ List testTransactionProducts2 = new List
+ {
+ new TransactionProduct { ProductID = 3, Quantity=4},
+ new TransactionProduct { ProductID = 1, Quantity=6}
+ };
+ foreach (var transactionProduct in testTransactionProducts2)
+ {
+ transactionCrud.AddTransactionProductToTransaction(transaction2.Id, transactionProduct);
+
+ }
+
}
catch(Exception ex)
diff --git a/nHIbernate/Expenses/Expense.cs b/nHIbernate/Expenses/Expense.cs
index 106ae9f..f99cc0a 100644
--- a/nHIbernate/Expenses/Expense.cs
+++ b/nHIbernate/Expenses/Expense.cs
@@ -4,7 +4,7 @@
{
public virtual int Id { get; set; }
public virtual DateTime Date { get; set; }
- public virtual decimal TotalPrice { get; set; }
+ public virtual decimal Value { get; set; }
public virtual string Description { get; set; }
}
}
diff --git a/nHIbernate/Expenses/ExpenseCRUD.cs b/nHIbernate/Expenses/ExpenseCRUD.cs
index 94c3d5c..a7ceeab 100644
--- a/nHIbernate/Expenses/ExpenseCRUD.cs
+++ b/nHIbernate/Expenses/ExpenseCRUD.cs
@@ -1,4 +1,5 @@
using FirmTracker_Server.nHibernate;
+using FirmTracker_Server.nHibernate.Products;
namespace FirmTracker_Server.nHibernate.Expenses
{
@@ -34,5 +35,73 @@ namespace FirmTracker_Server.nHibernate.Expenses
return session.Get(expenseId);
}
}
+ public DateTime GetExpenseDate(int expenseId)
+ {
+ using(var session = SessionFactory.OpenSession())
+ {
+ var expense = session.Query()
+ .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()
+ .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(expenseId);
+ if (expense != null)
+ {
+ session.Delete(expense);
+ transaction.Commit();
+ }
+
+ }
+ catch {
+ transaction.Rollback();
+ throw;
+ }
+ }
+
+ public IList GetAllExpenses()
+ {
+ using (var session = SessionFactory.OpenSession())
+ {
+ return session.Query().ToList();
+ }
+ }
}
}
diff --git a/nHIbernate/Expenses/ExpenseMapping.cs b/nHIbernate/Expenses/ExpenseMapping.cs
index 2eee2c8..ad2468d 100644
--- a/nHIbernate/Expenses/ExpenseMapping.cs
+++ b/nHIbernate/Expenses/ExpenseMapping.cs
@@ -9,7 +9,7 @@ namespace FirmTracker_Server.nHibernate.Expenses
Table("Expenses");
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.Date);
- Map(x => x.TotalPrice);
+ Map(x => x.Value);
Map(x => x.Description);
}