diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index bab580b..c802bd7 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -8,14 +8,12 @@ using GrossesMitainesAPI.Services; namespace GrossesMitainesAPI.Controllers; -[EnableCors("_myAllowSpecificOrigins")] -[ApiController, Route("api/[controller]")] +[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")] public class InventoryController : Controller { private readonly ILogger _logger; private readonly InventoryContext _context; private readonly DatabaseCacheService _cache; private static object _lock = new object(); - private const int AMOUNT_SCROLL = 5; public InventoryController(ILogger logger, InventoryContext context, DatabaseCacheService cache) { @@ -24,18 +22,15 @@ public class InventoryController : Controller { _cache = cache; } - [EnableCors("_myAllowSpecificOrigins")] - [HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant) + [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant) public IEnumerable Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) { bool islock = false; IQueryable ret; - if (_cache.isOk()) { ret = _cache.queryCache(); islock = true; } else ret = _context.Products.AsQueryable(); - switch (filterPrice) { case "PriceUnder20": ret = ret.Where(x => x.Price < 20); @@ -51,7 +46,6 @@ public class InventoryController : Controller { break; default: break; } - switch (filterState) { case "isAvailable": ret = ret.Where(x => x.Status == Product.States.Available); @@ -70,7 +64,6 @@ public class InventoryController : Controller { break; default: break; } - switch (order) { case "Price": ret = ret.OrderBy(x => x.Status == Product.States.Promotion || x.Status == Product.States.Clearance? x.PromoPrice: x.Price); @@ -92,13 +85,10 @@ public class InventoryController : Controller { break; default: break; } - List lst = new(); bool yup = false; int add = 0; - try { - if (lastId.HasValue) { // Pour avoir les prochains peu importe l'ordre. List prods; if (islock) @@ -125,7 +115,6 @@ public class InventoryController : Controller { lst = ret.Take(AMOUNT_SCROLL).ToList(); } else lst = ret.Take(AMOUNT_SCROLL).ToList(); - return lst; } catch (Exception e) { if (islock) @@ -134,17 +123,14 @@ public class InventoryController : Controller { return new List(); } } - // Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD. - [EnableCors("_myAllowSpecificOrigins")] - [HttpDelete(Name = "Inventory")] + [EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory")] public ActionResult Delete(int? id) { int rid = 0; if (!id.HasValue) { _logger.LogError(8, "Delete sans Id."); return BadRequest(); } - try { Product prod = _context.Products.First(x => x.Id == id); rid = prod.Id; diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs index 85fe931..57e08d8 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs @@ -16,8 +16,7 @@ namespace GrossesMitainesAPI.Controllers; /// qui sera effectuée dans les 10 secondes après /// l'éxécution d'une modification de la BD. /// -[EnableCors("_myAllowSpecificOrigins")] -[ApiController, Route("api/[controller]")] +[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")] public class ProductController : ControllerBase { private readonly ILogger _logger; private readonly InventoryContext _context; @@ -29,8 +28,7 @@ public class ProductController : ControllerBase { _cache = cache; } - [EnableCors("_myAllowSpecificOrigins")] - [HttpGet(Name = "Product"), AllowAnonymous] + [EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous] public ActionResult Get(int id) { Product prod; try { @@ -43,8 +41,7 @@ public class ProductController : ControllerBase { return prod; } - [EnableCors("_myAllowSpecificOrigins")] - [HttpPost(Name = "Product")] + [EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Product")] public ActionResult Post(Product prod) { if (prod.Price <= prod.PromoPrice) prod.PromoPrice = prod.Price - 0.01M; @@ -60,8 +57,7 @@ public class ProductController : ControllerBase { return prod; } - [EnableCors("_myAllowSpecificOrigins")] - [HttpPatch(Name = "Product")] + [EnableCors("_myAllowSpecificOrigins"), HttpPatch(Name = "Product")] public ActionResult Patch(Product prod) { try { _context.Products.Update(prod); @@ -75,8 +71,7 @@ public class ProductController : ControllerBase { return prod; } - [EnableCors("_myAllowSpecificOrigins")] - [HttpDelete(Name = "Product")] + [EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Product")] public ActionResult DeleteProduct(int id) { try { _context.Products.Remove(_context.Products.Where(x => x.Id == id).First()); diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs index 81ac81d..07f5851 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -9,8 +9,7 @@ using GrossesMitainesAPI.Services; namespace GrossesMitainesAPI.Controllers; -[EnableCors("_myAllowSpecificOrigins")] -[ApiController, Route("api/[controller]")] +[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller]")] public class SearchController : Controller { private readonly ILogger _logger; private readonly InventoryContext _context; @@ -18,7 +17,6 @@ public class SearchController : Controller { private Product[]? _searchCache = null; private const int PREVIEW = 4; - public SearchController(ILogger logger, InventoryContext context, DatabaseCacheService cache) { _logger = logger; _context = context; @@ -28,8 +26,7 @@ public class SearchController : Controller { _searchCache = _cache.GetCacheCopy(); } - [EnableCors("_myAllowSpecificOrigins")] - [HttpPost(Name = "Search")] + [EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Search")] public IEnumerable Post(string query, bool? preview, bool? deep) { if (_searchCache is not null) return SearchCached(query, preview, deep); @@ -38,9 +35,7 @@ public class SearchController : Controller { private List Search(string query, bool? preview, bool? deep) { List products = new(); - query = query.Trim(); - try { // Pour faire une liste priorisée. if (preview.HasValue && preview == true) products = _context.Products.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); @@ -83,14 +78,11 @@ public class SearchController : Controller { private List SearchCached(string query, bool? preview, bool? deep) { List products = new(); - query = query.Trim(); - if (_searchCache is null) { _logger.LogError(8, "Erreur de cache."); - return products; + return Search(query, preview, deep); } - try { // Pour faire une liste priorisée. if (preview.HasValue && preview == true) products = _searchCache.Where(x => x.Title.Contains(query)).Take(PREVIEW).ToList(); @@ -98,7 +90,6 @@ public class SearchController : Controller { if (deep.HasValue && deep == true) { List title = new(), desc = new(), cat = new(); query = query.ToLower(); - foreach (Product prod in _searchCache) { string sTitle = prod.Title.Replace(",", " ").ToLower(), sCat = prod.Category.ToLower(), @@ -127,6 +118,7 @@ public class SearchController : Controller { } } catch (Exception e) { _logger.LogError(8, e.Message); + return Search(query, preview, deep); } return products; }