2021-08-05 06:22:19 +00:00
|
|
|
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;
|
2021-07-21 00:08:57 +00:00
|
|
|
using Microsoft.AspNetCore.Mvc.RazorPages;
|
2021-08-05 06:22:19 +00:00
|
|
|
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;
|
2021-07-21 00:08:57 +00:00
|
|
|
|
|
|
|
namespace Props.Pages
|
|
|
|
{
|
2021-07-21 06:58:49 +00:00
|
|
|
public class SearchModel : PageModel
|
2021-07-21 00:08:57 +00:00
|
|
|
{
|
2021-08-05 06:22:19 +00:00
|
|
|
[BindProperty(Name = "q", SupportsGet = true)]
|
|
|
|
public string SearchQuery { get; set; }
|
|
|
|
public IEnumerable<ProductListing> 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; }
|
|
|
|
|
2021-08-07 22:20:46 +00:00
|
|
|
public ISearchManager SearchManager { get; private set; }
|
2021-08-05 06:22:19 +00:00
|
|
|
private UserManager<ApplicationUser> userManager;
|
|
|
|
private IMetricsManager analytics;
|
2021-08-07 22:20:46 +00:00
|
|
|
public SearchOutline ActiveSearchOutline { get; private set; }
|
2021-08-05 06:22:19 +00:00
|
|
|
|
|
|
|
public SearchModel(ISearchManager searchManager, UserManager<ApplicationUser> userManager, IMetricsManager analyticsManager)
|
|
|
|
{
|
2021-08-07 22:20:46 +00:00
|
|
|
this.SearchManager = searchManager;
|
2021-08-05 06:22:19 +00:00
|
|
|
this.userManager = userManager;
|
|
|
|
this.analytics = analyticsManager;
|
|
|
|
}
|
|
|
|
|
2021-08-07 22:20:46 +00:00
|
|
|
public async Task OnGetAsync()
|
2021-08-05 06:22:19 +00:00
|
|
|
{
|
2021-08-07 22:20:46 +00:00
|
|
|
ActiveSearchOutline = User.Identity.IsAuthenticated ? (await userManager.GetUserAsync(User)).searchOutlinePreferences.ActiveSearchOutline : new SearchOutline();
|
2021-08-05 06:22:19 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(SearchQuery)) return;
|
2021-08-07 22:20:46 +00:00
|
|
|
Console.WriteLine(SearchQuery);
|
|
|
|
this.SearchResults = await SearchManager.Search(SearchQuery, ActiveSearchOutline);
|
2021-08-05 06:22:19 +00:00
|
|
|
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();
|
|
|
|
}
|
2021-07-21 00:08:57 +00:00
|
|
|
}
|
|
|
|
}
|