Renvoit du rôle au frontend avec Get Login (UPDATE-DATABASE)
This commit is contained in:
@@ -63,35 +63,35 @@ public class InventoryController : Controller {
|
||||
}
|
||||
switch (filterState) {
|
||||
case "isAvailable":
|
||||
ret = ret.Where(x => x.Status == Product.States.Available);
|
||||
ret = ret.Where(x => x.Status == ProductModel.States.Available);
|
||||
break;
|
||||
case "isUnavailable":
|
||||
ret = ret.Where(x => x.Status == Product.States.Unavailable);
|
||||
ret = ret.Where(x => x.Status == ProductModel.States.Unavailable);
|
||||
break;
|
||||
case "isBackOrder":
|
||||
ret = ret.Where(x => x.Status == Product.States.BackOrder);
|
||||
ret = ret.Where(x => x.Status == ProductModel.States.BackOrder);
|
||||
break; ;
|
||||
case "isClearance":
|
||||
ret = ret.Where(x => x.Status == Product.States.Clearance);
|
||||
ret = ret.Where(x => x.Status == ProductModel.States.Clearance);
|
||||
break;
|
||||
case "isDiscontinued":
|
||||
ret = ret.Where(x => x.Status == Product.States.Discontinued);
|
||||
ret = ret.Where(x => x.Status == ProductModel.States.Discontinued);
|
||||
break;
|
||||
case "isPromoted":
|
||||
ret = ret.Where(x => x.Status == Product.States.Clearance ||
|
||||
x.Status == Product.States.Promotion);
|
||||
ret = ret.Where(x => x.Status == ProductModel.States.Clearance ||
|
||||
x.Status == ProductModel.States.Promotion);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
switch (order) {
|
||||
case "Price":
|
||||
ret = ret.OrderBy(x => x.Status == Product.States.Promotion ||
|
||||
x.Status == Product.States.Clearance ?
|
||||
ret = ret.OrderBy(x => x.Status == ProductModel.States.Promotion ||
|
||||
x.Status == ProductModel.States.Clearance ?
|
||||
x.PromoPrice : x.Price);
|
||||
break;
|
||||
case "PriceDesc":
|
||||
ret = ret.OrderByDescending(x => x.Status == Product.States.Promotion ||
|
||||
x.Status == Product.States.Clearance ?
|
||||
ret = ret.OrderByDescending(x => x.Status == ProductModel.States.Promotion ||
|
||||
x.Status == ProductModel.States.Clearance ?
|
||||
x.PromoPrice : x.Price);
|
||||
break;
|
||||
case "Title":
|
||||
@@ -140,16 +140,16 @@ public class InventoryController : Controller {
|
||||
return BadRequest();
|
||||
}
|
||||
try {
|
||||
Product prod = _context.Products.First(x => x.Id == id);
|
||||
ProductModel prod = _context.Products.First(x => x.Id == id);
|
||||
rid = prod.Id;
|
||||
if (prod.Quantity > 0) {
|
||||
prod.Quantity = prod.Quantity - 1;
|
||||
prod.Sales = prod.Sales + 1;
|
||||
prod.LastSale = DateTime.Now;
|
||||
if (prod.Quantity == 0)
|
||||
prod.Status = prod.Status == Product.States.Clearance ?
|
||||
Product.States.Discontinued :
|
||||
Product.States.BackOrder;
|
||||
prod.Status = prod.Status == ProductModel.States.Clearance ?
|
||||
ProductModel.States.Discontinued :
|
||||
ProductModel.States.BackOrder;
|
||||
} else {
|
||||
_logger.LogError(8, $"Vente de produit pas en stock. Id Produit: {prod.Id}");
|
||||
return BadRequest();
|
||||
|
@@ -14,11 +14,13 @@ using GrossesMitainesAPI.Models;
|
||||
Authorize(AuthenticationSchemes = "Identity.Application")]
|
||||
public class LoginController : Controller {
|
||||
private readonly UserManager<InventoryUser> _userMan;
|
||||
private readonly RoleManager<IdentityRole> _roleMan;
|
||||
private readonly SignInManager<InventoryUser> _signInMan;
|
||||
|
||||
public LoginController(SignInManager<InventoryUser> signin, UserManager<InventoryUser> userman) {
|
||||
public LoginController(SignInManager<InventoryUser> signin, UserManager<InventoryUser> userman, RoleManager<IdentityRole> roleMan) {
|
||||
this._signInMan = signin;
|
||||
this._userMan = userman;
|
||||
this._roleMan = roleMan;
|
||||
}
|
||||
|
||||
public class LoginUser {
|
||||
@@ -28,9 +30,16 @@ public class LoginController : Controller {
|
||||
|
||||
[HttpGet, Route("Login")]
|
||||
public ReturnUserViewModel WhoAmI() {
|
||||
var user = _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
var user = _userMan.GetUserAsync(_signInMan.Context.User);
|
||||
user.Wait();
|
||||
return new ReturnUserViewModel(user.Result);
|
||||
var roles = _userMan.GetRolesAsync(user.Result);
|
||||
roles.Wait();
|
||||
|
||||
string role = "";
|
||||
if (roles.Result.Contains("Administrateur"))
|
||||
role = "Administrateur";
|
||||
else role = "Client";
|
||||
return new ReturnUserViewModel(user.Result, role);
|
||||
}
|
||||
|
||||
[HttpPost, Route("Login"), AllowAnonymous]
|
||||
|
@@ -44,7 +44,7 @@ public class ProductController : ControllerBase {
|
||||
#region API Methods
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous]
|
||||
public ActionResult<ProductViewModel> Get(int id) {
|
||||
Product prod;
|
||||
ProductModel prod;
|
||||
try {
|
||||
prod = _context.Products.Where(x => x.Id == id).First();
|
||||
}
|
||||
@@ -57,7 +57,7 @@ public class ProductController : ControllerBase {
|
||||
}
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Product")]
|
||||
public async Task<ActionResult<Product>> Post(Product prod) {
|
||||
public async Task<ActionResult<ProductModel>> Post(ProductModel prod) {
|
||||
if (prod.Price <= prod.PromoPrice)
|
||||
prod.PromoPrice = prod.Price - 0.01M;
|
||||
try {
|
||||
@@ -76,7 +76,7 @@ public class ProductController : ControllerBase {
|
||||
}
|
||||
|
||||
[EnableCors("_myAllowSpecificOrigins"), HttpPatch(Name = "Product")]
|
||||
public ActionResult<Product> Patch(Product prod) {
|
||||
public ActionResult<ProductModel> Patch(ProductModel prod) {
|
||||
try {
|
||||
_context.Products.Update(prod);
|
||||
_context.SaveChanges();
|
||||
|
@@ -25,7 +25,7 @@ public class SearchController : Controller {
|
||||
private readonly ILogger<SearchController> _logger;
|
||||
private readonly InventoryContext _context;
|
||||
private readonly DatabaseCacheService _cache;
|
||||
private IQueryable<Product>? _searchCache = null;
|
||||
private IQueryable<ProductModel>? _searchCache = null;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -81,22 +81,22 @@ public class SearchController : Controller {
|
||||
}
|
||||
switch (filterState) {
|
||||
case "isAvailable":
|
||||
ret = ret.Where(x => x.Status == Product.States.Available);
|
||||
ret = ret.Where(x => x.Status == ProductModel.States.Available);
|
||||
break;
|
||||
case "isUnavailable":
|
||||
ret = ret.Where(x => x.Status == Product.States.Unavailable);
|
||||
ret = ret.Where(x => x.Status == ProductModel.States.Unavailable);
|
||||
break;
|
||||
case "isBackOrder":
|
||||
ret = ret.Where(x => x.Status == Product.States.BackOrder);
|
||||
ret = ret.Where(x => x.Status == ProductModel.States.BackOrder);
|
||||
break; ;
|
||||
case "isClearance":
|
||||
ret = ret.Where(x => x.Status == Product.States.Clearance);
|
||||
ret = ret.Where(x => x.Status == ProductModel.States.Clearance);
|
||||
break;
|
||||
case "isDiscontinued":
|
||||
ret = ret.Where(x => x.Status == Product.States.Discontinued);
|
||||
ret = ret.Where(x => x.Status == ProductModel.States.Discontinued);
|
||||
break;
|
||||
case "isPromoted":
|
||||
ret = ret.Where(x => x.Status == Product.States.Clearance || x.Status == Product.States.Promotion);
|
||||
ret = ret.Where(x => x.Status == ProductModel.States.Clearance || x.Status == ProductModel.States.Promotion);
|
||||
break;
|
||||
default: break;
|
||||
}
|
||||
@@ -122,10 +122,10 @@ public class SearchController : Controller {
|
||||
products.AddRange(cat);
|
||||
break;
|
||||
case "Price":
|
||||
ret = ret.OrderBy(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price);
|
||||
ret = ret.OrderBy(x => x.Status == ProductModel.States.Promotion || x.Status == ProductModel.States.Clearance ? x.PromoPrice : x.Price);
|
||||
goto default;
|
||||
case "PriceDesc":
|
||||
ret = ret.OrderByDescending(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance ? x.PromoPrice : x.Price);
|
||||
ret = ret.OrderByDescending(x => x.Status == ProductModel.States.Promotion || x.Status == ProductModel.States.Clearance ? x.PromoPrice : x.Price);
|
||||
goto default;
|
||||
case "Title":
|
||||
ret = ret.OrderBy(x => x.Title);
|
||||
|
Reference in New Issue
Block a user