2022-10-30 12:17:10 -04:00
namespace GrossesMitainesAPI.Controllers ;
#region Dependencies
using Microsoft.AspNetCore.Mvc ;
2022-10-08 13:22:12 -04:00
using GrossesMitainesAPI.Models ;
using System.Linq ;
using GrossesMitainesAPI.Data ;
using Microsoft.Extensions.Logging ;
2022-10-18 10:08:23 -04:00
using Microsoft.AspNetCore.Cors ;
2022-10-21 17:52:25 -04:00
using GrossesMitainesAPI.Services ;
2022-10-27 12:37:13 -04:00
using Microsoft.AspNetCore.Authorization ;
2022-10-08 13:22:12 -04:00
2022-10-30 12:17:10 -04:00
#endregion
2022-10-08 13:22:12 -04:00
2022-10-30 16:58:47 -04:00
[EnableCors("_myAllowSpecificOrigins"), ApiController, Route("api/[controller] "),
Authorize ( AuthenticationSchemes = "Identity.Application" ) ]
2022-10-08 13:22:12 -04:00
public class InventoryController : Controller {
2022-10-30 12:17:10 -04:00
#region Constants
2022-10-31 10:12:25 -04:00
private const int AMOUNT_SCROLL = 6 ;
2022-10-30 12:17:10 -04:00
#endregion
#region DI Fields
2022-10-09 15:07:35 -04:00
private readonly ILogger < InventoryController > _logger ;
2022-10-08 13:22:12 -04:00
private readonly InventoryContext _context ;
2022-10-21 17:52:25 -04:00
private readonly DatabaseCacheService _cache ;
2022-10-25 11:57:41 -04:00
2022-10-30 12:17:10 -04:00
#endregion
#region Ctor
2022-10-21 17:52:25 -04:00
public InventoryController ( ILogger < InventoryController > logger , InventoryContext context , DatabaseCacheService cache ) {
2022-10-08 13:22:12 -04:00
_context = context ;
2022-10-09 15:07:35 -04:00
_logger = logger ;
2022-10-21 17:52:25 -04:00
_cache = cache ;
2022-10-08 13:22:12 -04:00
}
2022-10-30 12:17:10 -04:00
#endregion
#region API Methods
2022-10-27 12:37:13 -04:00
[EnableCors("_myAllowSpecificOrigins"), HttpGet(Name = "Inventory"), AllowAnonymous] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
2022-10-27 14:32:06 -04:00
public IEnumerable < ProductViewModel > Get ( int? lastId , string? order , string? filterPrice , string? filterState , bool? all ) {
bool iscache = false ;
2022-10-30 12:17:10 -04:00
IQueryable < ProductViewModel > ret ;
2022-10-25 10:54:11 -04:00
if ( _cache . isOk ( ) ) {
2022-10-30 12:17:10 -04:00
ret = _cache . queryCache ( ) . Select ( x = > new ProductViewModel ( x ) ) ;
2022-10-27 14:32:06 -04:00
iscache = true ;
2022-10-25 10:54:11 -04:00
}
2022-10-30 12:17:10 -04:00
else ret = _context . Products . AsQueryable ( ) . Select ( x = > new ProductViewModel ( x ) ) ;
2022-10-16 11:25:21 -04:00
switch ( filterPrice ) {
2022-10-16 10:33:16 -04:00
case "PriceUnder20" :
ret = ret . Where ( x = > x . Price < 20 ) ;
break ;
case "Price20to49" :
ret = ret . Where ( x = > x . Price > = 20 & & x . Price < 50 ) ;
break ;
case "Price50to99" :
ret = ret . Where ( x = > x . Price > = 50 & & x . Price < 100 ) ;
break ;
case "PriceOver100" :
ret = ret . Where ( x = > x . Price > = 100 ) ;
break ;
2022-10-16 11:25:21 -04:00
default : break ;
}
switch ( filterState ) {
2022-10-16 10:33:16 -04:00
case "isAvailable" :
2022-11-01 13:33:08 -04:00
ret = ret . Where ( x = > x . Status = = ProductModel . States . Available ) ;
2022-10-16 11:25:21 -04:00
break ;
case "isUnavailable" :
2022-11-01 13:33:08 -04:00
ret = ret . Where ( x = > x . Status = = ProductModel . States . Unavailable ) ;
2022-10-16 10:33:16 -04:00
break ;
case "isBackOrder" :
2022-11-01 13:33:08 -04:00
ret = ret . Where ( x = > x . Status = = ProductModel . States . BackOrder ) ;
2022-10-16 10:33:16 -04:00
break ; ;
case "isClearance" :
2022-11-01 13:33:08 -04:00
ret = ret . Where ( x = > x . Status = = ProductModel . States . Clearance ) ;
2022-10-16 10:33:16 -04:00
break ;
case "isDiscontinued" :
2022-11-01 13:33:08 -04:00
ret = ret . Where ( x = > x . Status = = ProductModel . States . Discontinued ) ;
2022-10-16 10:33:16 -04:00
break ;
2022-10-26 14:29:57 -04:00
case "isPromoted" :
2022-11-01 13:33:08 -04:00
ret = ret . Where ( x = > x . Status = = ProductModel . States . Clearance | |
x . Status = = ProductModel . States . Promotion ) ;
2022-10-26 14:29:57 -04:00
break ;
2022-10-16 10:45:18 -04:00
default : break ;
2022-10-16 10:33:16 -04:00
}
switch ( order ) {
case "Price" :
2022-11-01 13:33:08 -04:00
ret = ret . OrderBy ( x = > x . Status = = ProductModel . States . Promotion | |
x . Status = = ProductModel . States . Clearance ?
2022-10-30 12:17:10 -04:00
x . PromoPrice : x . Price ) ;
2022-10-16 10:33:16 -04:00
break ;
case "PriceDesc" :
2022-11-01 13:33:08 -04:00
ret = ret . OrderByDescending ( x = > x . Status = = ProductModel . States . Promotion | |
x . Status = = ProductModel . States . Clearance ?
2022-10-30 12:17:10 -04:00
x . PromoPrice : x . Price ) ;
2022-10-16 10:33:16 -04:00
break ;
case "Title" :
2022-10-16 10:45:18 -04:00
ret = ret . OrderBy ( x = > x . Title ) ;
2022-10-16 10:33:16 -04:00
break ;
case "TitleDesc" :
2022-10-16 10:45:18 -04:00
ret = ret . OrderByDescending ( x = > x . Title ) ;
2022-10-16 10:33:16 -04:00
break ;
case "Category" :
2022-10-16 10:45:18 -04:00
ret = ret . OrderBy ( x = > x . Category ) ;
2022-10-16 10:33:16 -04:00
break ;
case "CategoryDesc" :
2022-10-16 10:45:18 -04:00
ret = ret . OrderByDescending ( x = > x . Category ) ;
2022-10-16 10:33:16 -04:00
break ;
2022-10-16 10:45:18 -04:00
default : break ;
2022-10-16 10:33:16 -04:00
}
2022-10-27 14:32:06 -04:00
List < ProductViewModel > lst = new ( ) ;
2022-10-18 10:43:52 -04:00
bool yup = false ;
int add = 0 ;
2022-10-25 10:54:11 -04:00
try {
2022-10-27 14:32:06 -04:00
if ( ! lastId . HasValue | | lastId = = 0 )
yup = true ;
2022-10-30 12:17:10 -04:00
foreach ( ProductViewModel prod in ret . ToList ( ) ) {
2022-10-27 14:32:06 -04:00
if ( yup & & add < AMOUNT_SCROLL | | ( all . HasValue & & all = = true ) ) {
2022-10-30 12:17:10 -04:00
lst . Add ( prod ) ;
2022-10-27 14:32:06 -04:00
add + + ;
2022-10-18 10:43:52 -04:00
}
2022-10-27 14:32:06 -04:00
if ( ! yup & & prod . Id = = lastId )
yup = true ;
}
2022-10-25 10:54:11 -04:00
return lst ;
} catch ( Exception e ) {
2022-10-27 14:32:06 -04:00
if ( iscache )
2022-10-25 10:54:11 -04:00
_logger . LogError ( e , "Erreur d'appel de cache." ) ;
else _logger . LogError ( e , "Erreur d'appel d'API." ) ;
2022-10-27 14:32:06 -04:00
return new List < ProductViewModel > ( ) ;
2022-10-25 10:54:11 -04:00
}
2022-10-08 13:22:12 -04:00
}
2022-10-21 18:07:11 -04:00
// Inventory/Delete => Décrémenter un produit. Va aller chercher directement dans la BD.
2022-11-01 14:07:49 -04:00
//[EnableCors("_myAllowSpecificOrigins"), HttpDelete(Name = "Inventory"), AllowAnonymous]
//public ActionResult<int> Delete(int? id) {
// int rid = 0;
// if (!id.HasValue) {
// _logger.LogError(8, "Tentative de vente sans Id.");
// return BadRequest();
// }
// try {
// 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 == 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();
// }
// _context.Products.Update(prod);
// _context.SaveChanges();
// } catch (Exception e) {
// _logger.LogError(8, e.Message);
// return BadRequest();
// }
// _cache.askForRefresh();
// return rid;
//}
2022-10-30 12:17:10 -04:00
#endregion
2022-10-08 13:22:12 -04:00
}