43 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
		
			1.4 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
using Microsoft.AspNetCore.Mvc;
 | 
						|
using GrossesMitainesAPI.Models;
 | 
						|
using System.Linq;
 | 
						|
using GrossesMitainesAPI.Data;
 | 
						|
using Microsoft.Extensions.Logging;
 | 
						|
using Microsoft.AspNetCore.Authorization;
 | 
						|
 | 
						|
namespace GrossesMitainesAPI.Controllers;
 | 
						|
 | 
						|
[ApiController, Route("api/[controller]")]
 | 
						|
public class SearchController : Controller {
 | 
						|
    private readonly ILogger<SearchController> _logger;
 | 
						|
    private readonly InventoryContext _context;
 | 
						|
 | 
						|
    public SearchController(ILogger<SearchController> logger, InventoryContext context) {
 | 
						|
        _logger = logger;
 | 
						|
        _context = context;
 | 
						|
    }
 | 
						|
 | 
						|
    [HttpPost(Name = "Search")]
 | 
						|
    public IEnumerable<Product> Post(string query, bool? preview) {
 | 
						|
        HashSet<Product> products = new();
 | 
						|
 | 
						|
        query = query.Trim();
 | 
						|
 | 
						|
        try { // Pour faire une liste priorisée.
 | 
						|
            if (preview.HasValue && preview == true)
 | 
						|
                products = _context.Products.Where(x => x.Title.Contains(query)).Take(3).ToHashSet();
 | 
						|
            else {
 | 
						|
                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;
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
}
 | 
						|
 |