Fix de l'API d'invoice pour pouvoir avoir l'info des produits.
This commit is contained in:
@@ -28,11 +28,11 @@ public class InvoiceController : Controller {
|
||||
#endregion
|
||||
|
||||
#region Ctor
|
||||
public InvoiceController(ILogger<InvoiceController> logger,
|
||||
InventoryContext context,
|
||||
public InvoiceController(ILogger<InvoiceController> logger,
|
||||
InventoryContext context,
|
||||
DatabaseCacheService cache,
|
||||
SignInManager<InventoryUser> signInMan,
|
||||
Microsoft.AspNetCore.Identity.UserManager<InventoryUser> userMan) {
|
||||
Microsoft.AspNetCore.Identity.UserManager<InventoryUser> userMan) {
|
||||
_logger = logger;
|
||||
_context = context;
|
||||
_cache = cache;
|
||||
@@ -50,7 +50,8 @@ public class InvoiceController : Controller {
|
||||
try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
|
||||
var user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
roles = await _userMan.GetRolesAsync(user);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
roles = new List<string>();
|
||||
}
|
||||
@@ -58,10 +59,20 @@ public class InvoiceController : Controller {
|
||||
try {
|
||||
id = _signInMan.Context.User.Identity.GetUserId();
|
||||
if (all is not null && all == true && roles.Contains("Administrateur"))
|
||||
return Ok(_context.Invoices.ToList());
|
||||
else return Ok(_context.Invoices.Include("ShippingAddress").Where(x => x.LinkedAccount != null &&
|
||||
x.LinkedAccount.Id == id).ToList());
|
||||
} catch (Exception e) {
|
||||
return Ok(_context.Invoices
|
||||
.Include("ShippingAddress")
|
||||
.Include(x => x.Products)
|
||||
.ThenInclude(y => y.Product)
|
||||
.ToList());
|
||||
|
||||
else
|
||||
return Ok(_context.Invoices
|
||||
.Include("ShippingAddress")
|
||||
.Include(x => x.Products)
|
||||
.ThenInclude(y => y.Product)
|
||||
.Where(x => x.LinkedAccount != null && x.LinkedAccount.Id == id).ToList());
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
@@ -74,14 +85,16 @@ public class InvoiceController : Controller {
|
||||
|
||||
try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
|
||||
roles = await _userMan.GetRolesAsync(await _userMan.GetUserAsync(_signInMan.Context.User));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
roles = new List<string>();
|
||||
}
|
||||
|
||||
try {
|
||||
inv = _context.Invoices.Where(x => x.Id == id).Include("ShippingAddress").First();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
@@ -97,9 +110,9 @@ public class InvoiceController : Controller {
|
||||
public async Task<ActionResult<InvoiceModel>> Post(SendInvoiceModel sinv) {
|
||||
var user = await _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
var prodcom = sinv.ProdQuant;
|
||||
Dictionary<int, uint> badprods = new();
|
||||
Dictionary<int, uint> badprods = new();
|
||||
List<ProductModel> prods;
|
||||
InvoiceModel inv = new() {
|
||||
InvoiceModel inv = new() {
|
||||
FirstName = sinv.FirstName,
|
||||
LastName = sinv.LastName,
|
||||
EmailAddress = sinv.EmailAddress,
|
||||
@@ -112,7 +125,7 @@ public class InvoiceController : Controller {
|
||||
x.City == sinv.City &&
|
||||
x.Province == sinv.Province &&
|
||||
x.Country == sinv.Country) ??
|
||||
new() {
|
||||
new() {
|
||||
CivicNumber = sinv.CivicNumber,
|
||||
Appartment = sinv.Appartment,
|
||||
Street = sinv.Street,
|
||||
@@ -127,7 +140,8 @@ public class InvoiceController : Controller {
|
||||
|
||||
try {
|
||||
prods = _context.Products.Where(x => sinv.ProdQuant.Select(x => x.Key).Contains(x.Id)).ToList();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
@@ -144,20 +158,22 @@ public class InvoiceController : Controller {
|
||||
inventProd.Status = inventProd.Status == ProductModel.States.Clearance ?
|
||||
ProductModel.States.Discontinued :
|
||||
ProductModel.States.BackOrder;
|
||||
} else inventProd.Quantity -= prod.Value;
|
||||
}
|
||||
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.
|
||||
_context.Addresses.Add(ad);
|
||||
_context.Invoices.Add(inv);
|
||||
_context.Products.UpdateRange(prods);
|
||||
_context.SaveChanges();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
return BadRequest(e.InnerException.Message);
|
||||
}
|
||||
@@ -167,27 +183,29 @@ public class InvoiceController : Controller {
|
||||
}
|
||||
|
||||
[HttpPost("Cancel/{id}"), Authorize(Roles = "Client, Administrateur")]
|
||||
public async Task<ActionResult<InvoiceModel>> Cancel(int id) {
|
||||
public async Task<ActionResult<InvoiceModel>> Cancel(int id) {
|
||||
InvoiceModel inv;
|
||||
IList<string> roles;
|
||||
|
||||
try { // Trouver la commande.
|
||||
inv = _context.Invoices.Where(x => x.Id == id)
|
||||
.Include("Product").First();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
|
||||
try { // Trouver les rôles de l'utilisateur, assumer non-admin si impossible à trouver.
|
||||
roles = await _userMan.GetRolesAsync(await _userMan.GetUserAsync(_signInMan.Context.User));
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(10, e.Message);
|
||||
roles = new List<string>();
|
||||
}
|
||||
|
||||
// Pour ne pas pouvoir arbitrairement annuler la commande d'un autre client en tant que client.
|
||||
if (!((inv.LinkedAccount is not null &&
|
||||
if (!((inv.LinkedAccount is not null &&
|
||||
inv.LinkedAccount.Id == _signInMan.Context.User.Identity.GetUserId()) ||
|
||||
roles.Contains("Administrateur")))
|
||||
return Unauthorized();
|
||||
@@ -208,12 +226,13 @@ public class InvoiceController : Controller {
|
||||
else prod.Product.Status = ProductModel.States.Available;
|
||||
prod.Product.Quantity = prod.Product.Quantity + prod.Quantity;
|
||||
prod.Product.Sales -= prod.Quantity;
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
_context.Update(inv);
|
||||
_context.SaveChanges();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
_logger.LogError(8, e.Message);
|
||||
return BadRequest();
|
||||
}
|
||||
|
Reference in New Issue
Block a user