Gossage dans API

This commit is contained in:
MarcEricMartel
2022-10-27 09:37:13 -07:00
parent 04c7d68a44
commit 83331a4a08
10 changed files with 213 additions and 39 deletions

View File

@@ -5,6 +5,7 @@ using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Cors;
using GrossesMitainesAPI.Services;
using Microsoft.AspNetCore.Authorization;
namespace GrossesMitainesAPI.Controllers;
@@ -22,7 +23,7 @@ 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"), AllowAnonymous] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
public IEnumerable<Product> Get(int? lastId, string? order, string? filterPrice, string? filterState, bool? all) {
bool islock = false;
IQueryable<Product> ret;
@@ -127,7 +128,7 @@ public class InventoryController : Controller {
}
}
// Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD.
[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory")]
[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory"), AllowAnonymous]
public ActionResult<int> Delete(int? id) {
int rid = 0;
if (!id.HasValue) {
@@ -139,6 +140,8 @@ public class InventoryController : Controller {
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:

View File

@@ -29,7 +29,7 @@ public class ProductController : ControllerBase {
}
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Product"), AllowAnonymous]
public ActionResult<Product> Get(int id) {
public ActionResult<ProductViewModel> Get(int id) {
Product prod;
try {
prod = _context.Products.Where(x => x.Id == id).First();
@@ -38,7 +38,8 @@ public class ProductController : ControllerBase {
_logger.LogError(8, e.Message);
return NotFound();
}
return prod;
_cache.addHit((uint)id);
return new ProductViewModel(prod);
}
[EnableCors("_myAllowSpecificOrigins"), HttpPost(Name = "Product")]

View File

@@ -26,14 +26,14 @@ public class SearchController : Controller {
_searchCache = _cache.GetCacheCopy();
}
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Search")]
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Search"), AllowAnonymous]
public IEnumerable<Product> Get(string query, bool? preview, bool? deep) {
if (_searchCache is not null)
return SearchCached(query, preview, deep);
else return Search(query, preview, deep);
else return SearchDirect(query, preview, deep);
}
private List<Product> Search(string query, bool? preview, bool? deep) {
private List<Product> SearchDirect(string query, bool? preview, bool? deep) {
List<Product> products = new();
query = query.Trim();
try { // Pour faire une liste priorisée.
@@ -80,7 +80,7 @@ public class SearchController : Controller {
query = query.Trim();
if (_searchCache is null) {
_logger.LogError(8, "Erreur de cache.");
return Search(query, preview, deep); // Fallback vers version non-cached en cas d'erreur.
return SearchDirect(query, preview, deep); // Fallback vers version non-cached en cas d'erreur.
}
try { // Pour faire une liste priorisée.
if (preview.HasValue && preview == true)
@@ -117,7 +117,7 @@ public class SearchController : Controller {
}
} catch (Exception e) {
_logger.LogError(8, e.Message);
return Search(query, preview, deep); // Fallback vers version non-cached en cas d'erreur.
return SearchDirect(query, preview, deep); // Fallback vers version non-cached en cas d'erreur.
}
return products;
}