poprawa mapowania i wyliczeń totalPrice
This commit is contained in:
parent
ca6a02205b
commit
ad1fa6e376
@ -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;
|
||||
|
@ -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")
|
||||
|
@ -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;
|
||||
|
@ -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")
|
||||
|
Loading…
Reference in New Issue
Block a user