PI2024-23 #2

Merged
s464958 merged 21 commits from PI2024-23 into master 2024-06-14 14:47:25 +02:00
4 changed files with 45 additions and 7 deletions
Showing only changes of commit ad1fa6e376 - Show all commits

View File

@ -23,6 +23,7 @@ using System.Text.Json;
using System.Transactions;
using FirmTracker_Server.nHibernate.Products;
using FirmTracker_Server.nHibernate;
using Microsoft.AspNetCore.Http.HttpResults;
namespace FirmTracker_Server.Controllers
{
@ -63,17 +64,18 @@ namespace FirmTracker_Server.Controllers
if (product.Quantity > availability)
{
return BadRequest($"Can't add product {product.ProductID} to transaction. Available: {availability}, Desired: {product.Quantity}");
throw new InvalidOperationException($"Can't add product {product.ProductID} to transaction. Available: {availability}, Desired: {product.Quantity}");
//return BadRequest($"Can't add product {product.ProductID} to transaction. Available: {availability}, Desired: {product.Quantity}");
}
else
{
transaction.TotalPrice += ((product.Quantity * price) * ((1 - (transaction.Discount / 100))));
//transaction.TotalPrice += ((product.Quantity * price) * ((1 - (transaction.Discount / 100))));
}
}
else
{
transaction.TotalPrice += (price * ((1 - (transaction.Discount / 100))));
//transaction.TotalPrice += (price * ((1 - (transaction.Discount / 100))));
}
}
@ -91,6 +93,13 @@ namespace FirmTracker_Server.Controllers
return CreatedAtAction(nameof(GetTransaction), new { id = transaction.Id }, transaction);
}
catch (InvalidOperationException ioe)
{
return BadRequest(new {
Message = ioe.Message,
ErrorCode = "Availability"
});
}
catch (Exception ex)
{
return BadRequest(ex.Message);
@ -121,7 +130,6 @@ namespace FirmTracker_Server.Controllers
try
{
foreach (var product in transaction.TransactionProducts)
{
product.TransactionId = transaction.Id;

View File

@ -13,6 +13,7 @@ namespace FirmTracker_Server.nHibernate.Transactions
Map(x => x.PaymentType);
Map(x => x.Discount);
Map(x => x.Description);
Map(x => x.TotalPrice);
HasMany(x => x.TransactionProducts)
.KeyColumn("TransactionId")

View File

@ -19,7 +19,15 @@ namespace FirmTracker_Server.nHibernate.Transactions
{
foreach (var transactionProduct in transaction.TransactionProducts)
{
var product = session.Get<Product>(transactionProduct.ProductID);
if (product.Type != 0)
{
transaction.TotalPrice += ((transactionProduct.Quantity * product.Price) * ((1 - (transaction.Discount / 100))));
}
else
{
transaction.TotalPrice += (product.Price) * ((1 - (transaction.Discount / 100)));
}
transactionProduct.TransactionId = transaction.Id;
session.Save(transactionProduct);
}
@ -78,6 +86,15 @@ namespace FirmTracker_Server.nHibernate.Transactions
{
foreach (var transactionProduct in transaction.TransactionProducts)
{
/*var product = session.Get<Product>(transactionProduct.ProductID);
if (product.Type != 0)
{
transaction.TotalPrice += ((transactionProduct.Quantity * product.Price) * ((1 - (transaction.Discount / 100))));
}
else
{
transaction.TotalPrice += (product.Price) * ((1 - (transaction.Discount / 100)));
}*/
transactionProduct.TransactionId = transaction.Id;
session.Update(transactionProduct);
@ -154,11 +171,22 @@ namespace FirmTracker_Server.nHibernate.Transactions
var transactionToUpdate = session.Get<Transaction>(transactionId);
if (transactionToUpdate != null)
{
var product = session.Get<Product>(transactionProduct.ProductID);
if (product.Type != 0)
{
transactionToUpdate.TotalPrice += ((transactionProduct.Quantity * product.Price) * ((1 - (transactionToUpdate.Discount / 100))));
}
else
{
transactionToUpdate.TotalPrice += (product.Price) * ((1 - (transactionToUpdate.Discount / 100)));
}
transactionProduct.TransactionId= transactionToUpdate.Id;
session.Save(transactionProduct);
transaction.Commit();
var product = session.Get<Product>(transactionProduct.ProductID);
session.Update(transactionToUpdate);
//var product = session.Get<Product>(transactionProduct.ProductID);
if (product.Type != 0)
{
product.Availability -= transactionProduct.Quantity;

View File

@ -13,6 +13,7 @@ namespace FirmTracker_Server.nHibernate.Transactions
Map(x => x.PaymentType);
Map(x => x.Discount);
Map(x => x.Description);
Map(x => x.TotalPrice);
HasMany(x => x.TransactionProducts)
.KeyColumn("TransactionId")