Ca va p-e marcher

This commit is contained in:
MarcEricMartel 2022-10-16 07:45:18 -07:00
parent 81f32c630b
commit 0cc291b2a8
2 changed files with 16 additions and 20 deletions

View File

@ -16,8 +16,10 @@ public class InventoryController : Controller {
_logger = logger; _logger = logger;
} }
[HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de 5 (pour du loading en scrollant) [HttpGet(Name = "Inventory")] // Pour faire des calls async par paquet de AMOUNT (5) (pour du loading en scrollant)
public IEnumerable<Product> Get(int? lastId, string order, string filter) { public IEnumerable<Product> Get(int? lastId, string order, string filter) {
const uint AMOUNT = 5;
var ret = _context.Products.AsQueryable(); var ret = _context.Products.AsQueryable();
switch (filter) { switch (filter) {
@ -45,45 +47,38 @@ public class InventoryController : Controller {
case "isDiscontinued": case "isDiscontinued":
ret = ret.Where(x => x.Quantity == 0 && x.isDiscontinued); ret = ret.Where(x => x.Quantity == 0 && x.isDiscontinued);
break; break;
default: break;
} }
switch (order) { switch (order) {
case "Price": case "Price":
ret = ret.OrderBy(x => x.Price) ret = ret.OrderBy(x => x.Price);
.Where(x => x.Id > lastId && x.Id < lastId + 5);
break; break;
case "PriceDesc": case "PriceDesc":
ret = ret.OrderByDescending(x => x.Price) ret = ret.OrderByDescending(x => x.Price);
.Where(x => x.Id < lastId && x.Id > lastId - 5);
break; break;
case "Title": case "Title":
ret = ret.OrderBy(x => x.Title) ret = ret.OrderBy(x => x.Title);
.Where(x => x.Id > lastId && x.Id < lastId + 5);
break; break;
case "TitleDesc": case "TitleDesc":
ret = ret.OrderByDescending(x => x.Title) ret = ret.OrderByDescending(x => x.Title);
.Where(x => x.Id < lastId && x.Id > lastId - 5);
break; break;
case "Category": case "Category":
ret = ret.OrderBy(x => x.Category) ret = ret.OrderBy(x => x.Category);
.Where(x => x.Id > lastId && x.Id < lastId + 5);
break; break;
case "CategoryDesc": case "CategoryDesc":
ret = ret.OrderByDescending(x => x.Category) ret = ret.OrderByDescending(x => x.Category);
.Where(x => x.Id < lastId && x.Id > lastId - 5);
break;
default:
break; break;
default: break;
} }
if (order.Contains("Desc")) { if (order is not null && order.Contains("Desc")) {
if (!lastId.HasValue) if (!lastId.HasValue)
lastId = _context.Products.Max(x => x.Id); lastId = _context.Products.Max(x => x.Id);
ret = ret.Where(x => x.Id < lastId && x.Id > lastId - 5); ret = ret.Where(x => x.Id < lastId && x.Id > lastId - AMOUNT);
} else { } else {
if (!lastId.HasValue) if (!lastId.HasValue)
lastId = 1; lastId = 1;
ret = ret.Where(x => x.Id > lastId && x.Id < lastId + 5); ret = ret.Where(x => x.Id > lastId && x.Id < lastId + AMOUNT);
} }
return ret.ToList(); return ret.ToList();
} }

View File

@ -3,6 +3,7 @@ using GrossesMitainesAPI.Models;
using System.Linq; using System.Linq;
using GrossesMitainesAPI.Data; using GrossesMitainesAPI.Data;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authorization;
namespace GrossesMitainesAPI.Controllers; namespace GrossesMitainesAPI.Controllers;
@ -16,7 +17,7 @@ public class ProductController : Controller {
_context = context; _context = context;
} }
[HttpGet(Name = "Product")] [HttpGet(Name = "Product"), AllowAnonymous]
public Product Get(int id) { public Product Get(int id) {
Product prod; Product prod;
try { try {