signup fonctionnel
This commit is contained in:
@@ -31,11 +31,13 @@ public class InvoiceController : Controller {
|
||||
public InvoiceController(ILogger<InvoiceController> logger,
|
||||
InventoryContext context,
|
||||
DatabaseCacheService cache,
|
||||
SignInManager<InventoryUser> signInMan,
|
||||
Microsoft.AspNetCore.Identity.UserManager<InventoryUser> userMan) {
|
||||
_logger = logger;
|
||||
_context = context;
|
||||
_cache = cache;
|
||||
_userMan = userMan;
|
||||
_signInMan = signInMan;
|
||||
}
|
||||
|
||||
#endregion
|
||||
@@ -46,18 +48,19 @@ public class InvoiceController : Controller {
|
||||
IList<string> roles;
|
||||
string id;
|
||||
try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
|
||||
roles = await _userMan.GetRolesAsync(await _userMan.GetUserAsync(_signInMan.Context.User));
|
||||
var user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
roles = await _userMan.GetRolesAsync(user);
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
roles = new List<string>();
|
||||
}
|
||||
|
||||
try {
|
||||
try { // TODO: Débugger ça.
|
||||
id = _signInMan.Context.User.Identity.GetUserId();
|
||||
if (all is not null && all == true && roles.Contains("Administrateur"))
|
||||
return _context.Invoices.Include("LinkedAccount, ShippingAddress").ToList();
|
||||
else return _context.Invoices.Include("ShippingAddress").Where(x => x.LinkedAccount != null &&
|
||||
x.LinkedAccount.Id == id).ToList();
|
||||
return Ok(_context.Invoices.Include("LinkedAccount, ShippingAddress").ToList());
|
||||
else return Ok(_context.Invoices.Include("ShippingAddress").Where(x => x.LinkedAccount != null &&
|
||||
x.LinkedAccount.Id == id).ToList());
|
||||
} catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
@@ -93,14 +96,14 @@ public class InvoiceController : Controller {
|
||||
[HttpPost, AllowAnonymous]
|
||||
public async Task<ActionResult<InvoiceModel>> Post(InvoiceModel inv) {
|
||||
var user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
var prodcom = inv.Products.ToList();
|
||||
Dictionary<int, uint> badprods = new();
|
||||
List<ProductModel> prods;
|
||||
|
||||
if (user is not null)
|
||||
inv.LinkedAccount = user;
|
||||
inv.PurchaseDate = DateTime.Now; // Pour forcer la date.
|
||||
|
||||
var prodcom = inv.Products.ToList();
|
||||
|
||||
try {
|
||||
prods = _context.Products.Where(x => inv.Products.Select(x => x.Product).Contains(x)).ToList();
|
||||
} catch (Exception e) {
|
||||
@@ -108,18 +111,26 @@ public class InvoiceController : Controller {
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
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)
|
||||
return BadRequest(); // TODO: retourner le produit qui ne peut pas être vendu.
|
||||
badprods.Add(prod.Id, prod.Quantity);
|
||||
if (inventProd.Quantity == prod.Quantity) {
|
||||
inventProd.Quantity = 0;
|
||||
inventProd.Status = inventProd.Status == ProductModel.States.Clearance ?
|
||||
ProductModel.States.Discontinued :
|
||||
ProductModel.States.BackOrder;
|
||||
} else inventProd.Quantity -= prod.Quantity;
|
||||
inventProd.LastSale = DateTime.Now;
|
||||
inventProd.Sales += prod.Quantity;
|
||||
}
|
||||
|
||||
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.Invoices.Add(inv);
|
||||
_context.Products.UpdateRange(prods);
|
||||
@@ -133,7 +144,7 @@ public class InvoiceController : Controller {
|
||||
return Ok(inv);
|
||||
}
|
||||
|
||||
[HttpPost, Authorize(Roles = "Client, Administrateur")]
|
||||
[HttpPost, Route("CancelInvoice"), Authorize(Roles = "Client, Administrateur")]
|
||||
public async Task<ActionResult<InvoiceModel>> Cancel(int id) {
|
||||
InvoiceModel inv;
|
||||
IList<string> roles;
|
||||
@@ -159,10 +170,23 @@ public class InvoiceController : Controller {
|
||||
roles.Contains("Administrateur")))
|
||||
return Unauthorized();
|
||||
|
||||
if (inv.Status == InvoiceModel.InStates.Cancelled ||
|
||||
inv.Status == InvoiceModel.InStates.Returned)
|
||||
return BadRequest("La commande à déjà été annulée.");
|
||||
|
||||
if (inv.Status == InvoiceModel.InStates.Shipped)
|
||||
return BadRequest("Il est trop tard pour annuler votre commande, veuillez contacter le service à la clientèle pour retourner la commande.");
|
||||
|
||||
inv.Status = InvoiceModel.InStates.Cancelled;
|
||||
|
||||
foreach (var prod in inv.Products) // Revert l'inventaire.
|
||||
foreach (var prod in inv.Products) { // Revert l'inventaire.
|
||||
if (prod.Product.Quantity == 0)
|
||||
if (prod.Product.Status == ProductModel.States.Discontinued)
|
||||
prod.Product.Status = ProductModel.States.Clearance;
|
||||
else prod.Product.Status = ProductModel.States.Available;
|
||||
prod.Product.Quantity = prod.Product.Quantity + prod.Quantity;
|
||||
prod.Product.Sales -= prod.Quantity;
|
||||
}
|
||||
|
||||
try {
|
||||
_context.Update(inv);
|
||||
@@ -173,7 +197,6 @@ public class InvoiceController : Controller {
|
||||
}
|
||||
|
||||
_cache.askForRefresh();
|
||||
|
||||
return Ok(inv);
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user