Obsługa sytuacji wydania towaru ponad stan dla metody POST
This commit is contained in:
parent
c4f62ec62b
commit
16c3ed1819
@ -5,6 +5,7 @@ using System.Text.Json.Serialization;
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using System.Transactions;
|
using System.Transactions;
|
||||||
using FirmTracker_Server.nHibernate.Products;
|
using FirmTracker_Server.nHibernate.Products;
|
||||||
|
using FirmTracker_Server.nHibernate;
|
||||||
|
|
||||||
namespace FirmTracker_Server.Controllers
|
namespace FirmTracker_Server.Controllers
|
||||||
{
|
{
|
||||||
@ -40,9 +41,19 @@ namespace FirmTracker_Server.Controllers
|
|||||||
decimal price = _productCRUD.GetProductPrice(product.ProductID);
|
decimal price = _productCRUD.GetProductPrice(product.ProductID);
|
||||||
int type = _productCRUD.GetProductType(product.ProductID);
|
int type = _productCRUD.GetProductType(product.ProductID);
|
||||||
if (type == 1)
|
if (type == 1)
|
||||||
|
{
|
||||||
|
int availability = _productCRUD.GetProductAvailability(product.ProductID);
|
||||||
|
|
||||||
|
if (product.Quantity > availability)
|
||||||
|
{
|
||||||
|
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
|
else
|
||||||
{
|
{
|
||||||
transaction.TotalPrice += (price * ((1 - (transaction.Discount / 100))));
|
transaction.TotalPrice += (price * ((1 - (transaction.Discount / 100))));
|
||||||
|
@ -53,6 +53,19 @@ namespace FirmTracker_Server.nHibernate.Products
|
|||||||
return product;
|
return product;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public int GetProductAvailability(int productId)
|
||||||
|
{
|
||||||
|
using(var session = SessionFactory.OpenSession())
|
||||||
|
{
|
||||||
|
var product = session.Query<Product>()
|
||||||
|
.Where(p => p.Id == productId)
|
||||||
|
.Select(p => p.Availability)
|
||||||
|
.FirstOrDefault();
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Product GetProduct(int productId)
|
public Product GetProduct(int productId)
|
||||||
{
|
{
|
||||||
using (var session = SessionFactory.OpenSession())
|
using (var session = SessionFactory.OpenSession())
|
||||||
|
Loading…
Reference in New Issue
Block a user