using System; using System.Collections; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Castle.Core.Internal; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.EntityFrameworkCore; using Props.Data; using Props.Extensions; using Props.Models.Search; using Props.Models.User; using Props.Services.Modules; using Props.Shop.Framework; namespace Props.Pages { public class SearchModel : PageModel { [BindProperty(Name = "q", SupportsGet = true)] public string SearchQuery { get; set; } public IEnumerable SearchResults { get; private set; } public ProductListing BestRatingPriceRatio { get; private set; } public ProductListing TopRated { get; private set; } public ProductListing MostPurchases { get; private set; } public ProductListing MostReviews { get; private set; } public ProductListing BestPrice { get; private set; } private ISearchManager searchManager; private UserManager userManager; private IMetricsManager analytics; public SearchModel(ISearchManager searchManager, UserManager userManager, IMetricsManager analyticsManager) { this.searchManager = searchManager; this.userManager = userManager; this.analytics = analyticsManager; } public async Task OnGet() { if (string.IsNullOrWhiteSpace(SearchQuery)) return; SearchOutline activeSearchOutline = User.Identity.IsAuthenticated ? (await userManager.GetUserAsync(User)).searchOutlinePreferences.ActiveSearchOutline : new SearchOutline(); this.SearchResults = searchManager.Search(SearchQuery, activeSearchOutline); BestRatingPriceRatio = (from result in SearchResults orderby result.GetRatingToPriceRatio() descending select result).FirstOrDefault((listing) => listing.GetRatingToPriceRatio() >= 0.5f); TopRated = (from result in SearchResults orderby result.Rating descending select result).FirstOrDefault(); MostPurchases = (from result in SearchResults orderby result.PurchaseCount descending select result).FirstOrDefault(); MostReviews = (from result in SearchResults orderby result.ReviewCount descending select result).FirstOrDefault(); BestPrice = (from result in SearchResults orderby result.UpperPrice descending select result).FirstOrDefault(); } } }