PIAPS_CW/RestAPI/Controllers/MediaFileController.cs
2024-06-26 08:04:07 +04:00

64 lines
2.0 KiB
C#

using BusinessLogic.BusinessLogic;
using Contracts.BusinessLogicContracts;
using Contracts.SearchModels;
using Contracts.ViewModels;
using DatabaseImplement.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace RestAPI.Controllers
{
[Route("[controller]/[action]")]
[ApiController]
public class MediaFileController : Controller
{
private readonly IMediaFileLogic _mediaFileLogic;
private readonly IProductLogic _productLogic;
private readonly ILogger _logger;
public MediaFileController(ILogger<MediaFileController> 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<Guid, List<MediaFileViewModel>> GetByProducts()
{
try
{
var dict = new Dictionary<Guid, List<MediaFileViewModel>>();
var products = _productLogic.ReadList(null);
foreach (var product in products)
{
var media = _mediaFileLogic.ReadList(new MediaFileSearchModel()
{
ProductId = product.Id,
});
dict.Add(product.Id, media);
}
return dict;
}
catch (Exception ex)
{
_logger.LogError(ex, "Ошибка получения списка продуктов");
throw;
}
}
}
}