From f5351340d82aa3ae1234d391077393bd28e23cec Mon Sep 17 00:00:00 2001 From: MarcEricMartel <74071476+MarcEricMartel@users.noreply.github.com> Date: Sun, 9 Oct 2022 11:23:46 -0700 Subject: [PATCH] Continuage d'API --- .../Controllers/SearchController.cs | 38 +++++++++++++++++++ .../GrossesMitainesAPI/Models/Product.cs | 14 +++++-- 2 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs diff --git a/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs new file mode 100644 index 0000000..457e363 --- /dev/null +++ b/GrossesMitaines/GrossesMitainesAPI/Controllers/SearchController.cs @@ -0,0 +1,38 @@ +using Microsoft.AspNetCore.Mvc; +using GrossesMitainesAPI.Models; +using System.Linq; +using GrossesMitainesAPI.Data; +using Microsoft.Extensions.Logging; + +namespace GrossesMitainesAPI.Controllers; + +[ApiController, Route("api/[controller]")] +public class SearchController : Controller { + private readonly ILogger _logger; + private readonly InventoryContext _context; + + public SearchController(ILogger logger, InventoryContext context) { + _logger = logger; + _context = context; + } + + [HttpPost(Name = "Search")] + public IEnumerable Post(string query, bool preview) { + HashSet products = new(); + + query = query.Trim(); + + try { // Pour faire une liste priorisée. + products.Concat(_context.Products.Where(x => x.Title.Contains(query)).ToHashSet()); + products.Concat(_context.Products.Where(x => x.Category.Contains(query)).ToHashSet()); + products.Concat(_context.Products.Where(x => x.Description.Contains(query)).ToHashSet()); + } catch (Exception e) { + _logger.LogError(8, e.Message); + } + + return products; + } + + +} + diff --git a/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs b/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs index e10324b..ecd439a 100644 --- a/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs +++ b/GrossesMitaines/GrossesMitainesAPI/Models/Product.cs @@ -1,15 +1,21 @@ -using System.ComponentModel.DataAnnotations; +using Microsoft.Data.SqlClient.Server; +using System.ComponentModel.DataAnnotations; namespace GrossesMitainesAPI.Models; - +// nom du produit, +// catégories, description, quantité disponible, images, prix normal et +// autres informations pertinentes public class Product { [Key] public int Id { get; set; } [Required, MaxLength(255)] public string Title { get; set; } = "Erreur Aucun Objet"; [Required] + public string Category { get; set; } = "Inconnue"; + [Required] public string Description { get; set; } = "Lorem Ipsum."; - [Range(0.01, (double)decimal.MaxValue)] // Range qui prend pas les decimals! + [Required, Range(0.01, (double)decimal.MaxValue)] // Range qui prend pas les decimals! public decimal Price { get; set; } = 0; - public string? ImageName { get; set; } // Base pour sortir les images ({ImageName}.png , {ImageName}_thumbnail.png, etc...) + public uint Quantity { get; set; } = 0; + public string? ImageName { get; set; } // Base pour sortir les images ({ImageName}.jpg , {ImageName}_thumbnail.jpg, etc...) } \ No newline at end of file