Просмотр каталожечка

This commit is contained in:
devil_1nc 2024-06-25 15:50:56 +04:00
parent eeec81593b
commit 28437690c0
4 changed files with 69 additions and 85 deletions

View File

@ -1,83 +0,0 @@
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace RestAPI.Controllers
{
public class CartController : Controller
{
// GET: CartController
public ActionResult Index()
{
return View();
}
// GET: CartController/Details/5
public ActionResult Details(int id)
{
return View();
}
// GET: CartController/Create
public ActionResult Create()
{
return View();
}
// POST: CartController/Create
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(IFormCollection collection)
{
try
{
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
// GET: CartController/Edit/5
public ActionResult Edit(int id)
{
return View();
}
// POST: CartController/Edit/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(int id, IFormCollection collection)
{
try
{
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
// GET: CartController/Delete/5
public ActionResult Delete(int id)
{
return View();
}
// POST: CartController/Delete/5
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Delete(int id, IFormCollection collection)
{
try
{
return RedirectToAction(nameof(Index));
}
catch
{
return View();
}
}
}
}

View File

@ -0,0 +1,60 @@
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace RestAPI.Controllers
{
public class MediaFileController : Controller
{
private readonly IMediaFileLogic _mediaFileLogic;
private readonly IProductLogic _productLogic;
private readonly ILogger _logger;
public MediaFileController(ILogger logger, IMediaFileLogic mediaFileLogic, IProductLogic productLogic)
{
_logger = logger;
_mediaFileLogic = mediaFileLogic;
_productLogic = productLogic;
}
[HttpGet]
public List<MediaFileViewModel> GetFullList()
{
try
{
return _mediaFileLogic.ReadList(null);
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка продуктов");
throw;
}
}
[HttpGet]
public Dictionary<ProductViewModel, List<MediaFileViewModel>> GetByProducts()
{
try
{
var dict = new Dictionary<ProductViewModel, List<MediaFileViewModel>>();
var products = _productLogic.ReadList(null);
foreach (var product in products)
{
var media = _mediaFileLogic.ReadList(new MediaFileSearchModel()
{
ProductId = product.Id,
});
dict.Add(product, media);
}
return dict;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка продуктов");
throw;
}
}
}
}

View File

@ -48,7 +48,7 @@
@foreach (var weapon in @Model.ProductsModel)
{
<div class="card">
<img src="#" class="card-img-top" alt="@weapon.Name">
<img src="@Model.GetMediaByProduct(weapon)[0].Location" class="card-img-top" alt="@weapon.Name">
<div class="card-body">
<h5 class="card-title">@weapon.Name</h5>
<p class="card-text">Цена: >@weapon.Price руб.</p>

View File

@ -9,10 +9,17 @@ namespace WebApp.Pages
public class IndexModel : PageModel
{
public List<ProductViewModel> ProductsModel { get; set; }
public Dictionary<ProductViewModel, List<MediaFileViewModel>> MediaByProductsModel { get; set; }
public void OnGet()
{
ProductsModel = APIClient.GetRequest<List<ProductViewModel>>($"Product/GetFullList");
ProductsModel = APIClient.GetRequest<List<ProductViewModel>>($"Product/GetFullList");
MediaByProductsModel = APIClient.GetRequest<Dictionary<ProductViewModel, List<MediaFileViewModel>>>($"MediaFile/GetByProducts?");
}
public List<MediaFileViewModel> GetMediaByProduct(ProductViewModel productModel)
{
MediaByProductsModel.TryGetValue(productModel, out List<MediaFileViewModel> models);
return models;
}
}
}