poprawa mapowania i wyliczeń totalPrice

This commit is contained in:
kamil 2024-06-11 23:54:59 +02:00
parent ca6a02205b
commit ad1fa6e376
4 changed files with 45 additions and 7 deletions

View File

@ -23,6 +23,7 @@ using System.Text.Json;
using System.Transactions; using System.Transactions;
using FirmTracker_Server.nHibernate.Products; using FirmTracker_Server.nHibernate.Products;
using FirmTracker_Server.nHibernate; using FirmTracker_Server.nHibernate;
using Microsoft.AspNetCore.Http.HttpResults;
namespace FirmTracker_Server.Controllers namespace FirmTracker_Server.Controllers
{ {
@ -63,17 +64,18 @@ namespace FirmTracker_Server.Controllers
if (product.Quantity > availability) 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 else
{ {
transaction.TotalPrice += ((product.Quantity * price) * ((1 - (transaction.Discount / 100)))); //transaction.TotalPrice += ((product.Quantity * price) * ((1 - (transaction.Discount / 100))));
} }
} }
else 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); return CreatedAtAction(nameof(GetTransaction), new { id = transaction.Id }, transaction);
} }
catch (InvalidOperationException ioe)
{
return BadRequest(new {
Message = ioe.Message,
ErrorCode = "Availability"
});
}
catch (Exception ex) catch (Exception ex)
{ {
return BadRequest(ex.Message); return BadRequest(ex.Message);
@ -121,7 +130,6 @@ namespace FirmTracker_Server.Controllers
try try
{ {
foreach (var product in transaction.TransactionProducts) foreach (var product in transaction.TransactionProducts)
{ {
product.TransactionId = transaction.Id; product.TransactionId = transaction.Id;

View File

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

View File

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

View File

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