From 0cc291b2a8d99039bce5b73690e94a0b20a056f1 Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Sun, 16 Oct 2022 07:45:18 -0700 Subject: [PATCH] Ca va p-e marcher --- .../Controllers/InventoryController.cs | 33 ++++++++----------- .../Controllers/ProductController.cs | 3 +- 2 files changed, 16 insertions(+), 20 deletions(-) diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs index ef24bc4..98760ad 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/InventoryController.cs @@ -16,8 +16,10 @@ public class InventoryController : Controller { _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 Get(int? lastId, string order, string filter) { + const uint AMOUNT = 5; + var ret = _context.Products.AsQueryable(); switch (filter) { @@ -45,45 +47,38 @@ public class InventoryController : Controller { case "isDiscontinued": ret = ret.Where(x => x.Quantity == 0 && x.isDiscontinued); break; + default: break; } switch (order) { case "Price": - ret = ret.OrderBy(x => x.Price) - .Where(x => x.Id > lastId && x.Id < lastId + 5); + ret = ret.OrderBy(x => x.Price); break; case "PriceDesc": - ret = ret.OrderByDescending(x => x.Price) - .Where(x => x.Id < lastId && x.Id > lastId - 5); + ret = ret.OrderByDescending(x => x.Price); break; case "Title": - ret = ret.OrderBy(x => x.Title) - .Where(x => x.Id > lastId && x.Id < lastId + 5); + ret = ret.OrderBy(x => x.Title); break; case "TitleDesc": - ret = ret.OrderByDescending(x => x.Title) - .Where(x => x.Id < lastId && x.Id > lastId - 5); + ret = ret.OrderByDescending(x => x.Title); break; case "Category": - ret = ret.OrderBy(x => x.Category) - .Where(x => x.Id > lastId && x.Id < lastId + 5); + ret = ret.OrderBy(x => x.Category); break; case "CategoryDesc": - ret = ret.OrderByDescending(x => x.Category) - .Where(x => x.Id < lastId && x.Id > lastId - 5); - break; - default: - + ret = ret.OrderByDescending(x => x.Category); break; + default: break; } - if (order.Contains("Desc")) { + if (order is not null && order.Contains("Desc")) { if (!lastId.HasValue) 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 { if (!lastId.HasValue) 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(); } diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs index 27d6b18..61f4e8e 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/ProductController.cs @@ -3,6 +3,7 @@ using GrossesMitainesAPI.Models; using System.Linq; using GrossesMitainesAPI.Data; using Microsoft.Extensions.Logging; +using Microsoft.AspNetCore.Authorization; namespace GrossesMitainesAPI.Controllers; @@ -16,7 +17,7 @@ public class ProductController : Controller { _context = context; } - [HttpGet(Name = "Product")] + [HttpGet(Name = "Product"), AllowAnonymous] public Product Get(int id) { Product prod; try {