PIAPS_CW/WebApp/Pages/Index.cshtml.cs

43 lines
1.5 KiB
C#

using Contracts.SearchModels;
using Contracts.ViewModels;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace WebApp.Pages
{
[AllowAnonymous]
public class IndexModel : PageModel
{
public List<ProductViewModel> ProductsModel { get; set; }
public List<SaleViewModel> SalesModel { get; set; }
public Dictionary<Guid, List<MediaFileViewModel>> MediaByProductsModel { get; set; }
public void OnGet(double? pricefrom, double? priceto, string? search, string? category)
{
string request = "Product/GetList";
if (!string.IsNullOrEmpty(search))
request += $"?search={search}&";
if (!string.IsNullOrEmpty(category))
request += $"?category={category}&";
if (pricefrom != null)
request += $"?pricefrom={pricefrom}&";
if (priceto != null)
request += $"?priceto={priceto}";
ProductsModel = APIClient.GetRequest<List<ProductViewModel>>(request);
MediaByProductsModel = APIClient.GetRequest<Dictionary<Guid, List<MediaFileViewModel>>>($"MediaFile/GetByProducts");
SalesModel = APIClient.GetRequest<List<SaleViewModel>>($"Sale/GetList");
}
public byte[] GetMediaByProduct(ProductViewModel productModel)
{
MediaByProductsModel.TryGetValue(productModel.Id, out List<MediaFileViewModel> models);
return models[0].Image;
}
}
}