Ça va prendre un Update-Database
This commit is contained in:
@@ -58,7 +58,7 @@ public class InvoiceController : Controller {
|
||||
try { // TODO: Débugger ça.
|
||||
id = _signInMan.Context.User.Identity.GetUserId();
|
||||
if (all is not null && all == true && roles.Contains("Administrateur"))
|
||||
return Ok(_context.Invoices.Include("LinkedAccount, ShippingAddress").ToList());
|
||||
return Ok(_context.Invoices/*.Include("LinkedAccount").Include("ShippingAddress"/*"LinkedAccount, ShippingAddress")*/.ToList());
|
||||
else return Ok(_context.Invoices.Include("ShippingAddress").Where(x => x.LinkedAccount != null &&
|
||||
x.LinkedAccount.Id == id).ToList());
|
||||
} catch (Exception e) {
|
||||
@@ -94,18 +94,39 @@ public class InvoiceController : Controller {
|
||||
}
|
||||
|
||||
[HttpPost, AllowAnonymous]
|
||||
public async Task<ActionResult<InvoiceModel>> Post(InvoiceModel inv) {
|
||||
public async Task<ActionResult<InvoiceModel>> Post(SendInvoiceModel sinv) {
|
||||
var user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
var prodcom = inv.Products.ToList();
|
||||
var prodcom = sinv.ProdQuant;
|
||||
Dictionary<int, uint> badprods = new();
|
||||
List<ProductModel> prods;
|
||||
|
||||
InvoiceModel inv = new() {
|
||||
FirstName = sinv.FirstName,
|
||||
LastName = sinv.LastName,
|
||||
EmailAddress = sinv.EmailAddress,
|
||||
PhoneNumber = sinv.PhoneNumber,
|
||||
PurchaseDate = DateTime.Now
|
||||
};
|
||||
AddressModel ad = _context.Addresses.FirstOrDefault(x => x.CivicNumber == sinv.CivicNumber &&
|
||||
x.Appartment == sinv.Appartment &&
|
||||
x.Street == sinv.Street &&
|
||||
x.City == sinv.City &&
|
||||
x.Province == sinv.Province &&
|
||||
x.Country == sinv.Country) ??
|
||||
new() {
|
||||
CivicNumber = sinv.CivicNumber,
|
||||
Appartment = sinv.Appartment,
|
||||
Street = sinv.Street,
|
||||
City = sinv.City,
|
||||
Province = sinv.Province,
|
||||
Country = sinv.Country,
|
||||
PostalCode = sinv.PostalCode
|
||||
};
|
||||
inv.ShippingAddress = ad;
|
||||
if (user is not null)
|
||||
inv.LinkedAccount = user;
|
||||
inv.PurchaseDate = DateTime.Now; // Pour forcer la date.
|
||||
|
||||
try {
|
||||
prods = _context.Products.Where(x => inv.Products.Select(x => x.Product).Contains(x)).ToList();
|
||||
prods = _context.Products.Where(x => sinv.ProdQuant.Select(x => x.Key).Contains(x.Id)).ToList();
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
return BadRequest();
|
||||
@@ -114,30 +135,31 @@ public class InvoiceController : Controller {
|
||||
if (prods.Count == 0)
|
||||
return BadRequest("Vous devez inclure au moins un produit à votre commande.");
|
||||
|
||||
foreach (var prod in prodcom) { // Update de quantités dans l'inventaire.
|
||||
ProductModel inventProd = prods.Where(x => x.Id == prod.Product.Id).First();
|
||||
if (inventProd.Quantity > prod.Quantity)
|
||||
badprods.Add(prod.Id, prod.Quantity);
|
||||
if (inventProd.Quantity == prod.Quantity) {
|
||||
foreach (var prod in sinv.ProdQuant) { // Update de quantités dans l'inventaire.
|
||||
ProductModel inventProd = prods.Where(x => x.Id == prod.Key).First();
|
||||
if (inventProd.Quantity > prod.Value)
|
||||
badprods.Add(prod.Key, prod.Value);
|
||||
if (inventProd.Quantity == prod.Value) {
|
||||
inventProd.Quantity = 0;
|
||||
inventProd.Status = inventProd.Status == ProductModel.States.Clearance ?
|
||||
ProductModel.States.Discontinued :
|
||||
ProductModel.States.BackOrder;
|
||||
} else inventProd.Quantity -= prod.Quantity;
|
||||
} else inventProd.Quantity -= prod.Value;
|
||||
inventProd.LastSale = DateTime.Now;
|
||||
inventProd.Sales += prod.Quantity;
|
||||
inventProd.Sales += prod.Value;
|
||||
}
|
||||
|
||||
if (badprods.Count > 0) // Retour des produits non-achetable avec l'inventaire restant.
|
||||
return BadRequest(badprods.ToArray());
|
||||
|
||||
|
||||
try { // Faire les updates dans la BD.
|
||||
_context.Addresses.Add(ad);
|
||||
_context.Invoices.Add(inv);
|
||||
_context.Products.UpdateRange(prods);
|
||||
_context.SaveChanges();
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
return BadRequest();
|
||||
return BadRequest(e.InnerException.Message);
|
||||
}
|
||||
|
||||
_cache.askForRefresh();
|
||||
|
Reference in New Issue
Block a user