PI2024-9 #1

Merged
s464958 merged 8 commits from PI2024-9 into master 2024-06-03 15:17:12 +02:00
6 changed files with 165 additions and 8 deletions
Showing only changes of commit f09289c101 - Show all commits

View File

@ -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);
}
}
}
}

View File

@ -8,6 +8,10 @@
<UserSecretsId>08986e21-848b-485a-a219-03e2dc6041e4</UserSecretsId>
</PropertyGroup>
<ItemGroup>
<Compile Remove="nHIbernate\Transactions\TransactionCRUD2.cs" />
</ItemGroup>
<ItemGroup>
<Content Include="Properties\launchSettings.json" />
</ItemGroup>

View File

@ -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<TransactionProduct> testTransactionProducts = new List<TransactionProduct> {
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<TransactionProduct> testTransactionProducts2 = new List<TransactionProduct>
{
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)

View File

@ -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; }
}
}

View File

@ -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<Expense>(expenseId);
}
}
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();
}
}
}
}

View File

@ -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);
}