Merge branch 'react-version' of https://github.com/MarcEricMartel/420-5DW-HY-TP into react-version

This commit is contained in:
Victor Turgeon 2022-12-08 18:25:56 -05:00
commit 16f315c8aa
2 changed files with 72 additions and 28 deletions

View File

@ -176,6 +176,68 @@ public class InvoiceController : Controller {
AddressModel ad;
user.Wait();
inv.Products = new();
foreach (var prod in prods) {
inv.Products.Add(new() {
Product = prod,
Quantity = sinv.ProdQuant.First(x => x.Key == prod.Id).Value
});
}
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, inventProd.Quantity);
else 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.Value;
inventProd.LastSale = DateTime.Now;
inventProd.Sales += prod.Value;
}
if (badprods.Count > 0) // Retour des produits non-achetable avec l'inventaire restant.
return BadRequest(badprods.ToArray());
// Charges!
StripeConfiguration.ApiKey = _stripeOptions.Value.SecretKey;
ChargesModel chr;
chr = new() {
Token = sinv.Token,
AmountInCents = sinv.AmountInCents,
Name = sinv.Name,
Phone = sinv.Phone,
Email = sinv.Email,
Description = sinv.Description,
CurrencyCode = sinv.CurrencyCode
};
inv.Payment = chr;
var options = new ChargeCreateOptions {
Amount = sinv.AmountInCents,
Description = sinv.Description,
Source = sinv.Token,
Currency = sinv.CurrencyCode,
};
var service = new ChargeService();
Charge charge = service.Create(options);
if (charge.FailureMessage is not null && charge.FailureMessage != "")
return Json(charge.ToJson());
try {
_context.Invoices.Update(inv);
_context.SaveChanges();
} catch (Exception ex) {
_logger.LogError(20, ex.Message);
return BadRequest(ex.Message);
}
if (user.Result is not null) {
inv.LinkedAccount = user.Result;
ad = _context.Addresses.FirstOrDefault(x => x.CivicNumber == sinv.CivicNumber &&
@ -213,36 +275,9 @@ public class InvoiceController : Controller {
return BadRequest();
}
if (prods.Count == 0)
return BadRequest("Vous devez inclure au moins un produit à votre commande.");
inv.Products = new();
foreach (var prod in prods) {
inv.Products.Add(new() {
Product = prod,
Quantity = sinv.ProdQuant.First(x => x.Key == prod.Id).Value
});
}
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, inventProd.Quantity);
else 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.Value;
inventProd.LastSale = DateTime.Now;
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.
if (ad.Id == 0)
_context.Addresses.Add(ad);
@ -255,7 +290,7 @@ public class InvoiceController : Controller {
}
_cache.askForRefresh();
return Ok(inv);
return Json(charge.ToJson());
}
[HttpPost("Cancel/{id}")]

View File

@ -26,4 +26,13 @@ namespace GrossesMitainesAPI.Models;
public string PostalCode { get; set; }
public Dictionary<int, uint> ProdQuant { get; set; }
// partie pour la charge.
public string Token { get; set; }
public string Description { get; set; }
public long AmountInCents { get; set; }
public string CurrencyCode { get; set; }
public string Name { get; set; }
public string Email { get; set; }
public string Phone { get; set; }
}