127 lines
3.9 KiB
C#
127 lines
3.9 KiB
C#
using DomainResults.Common;
|
|
|
|
using DataProviders;
|
|
|
|
using Core.Abstractions;
|
|
using Core.DomainObjects;
|
|
using Core.Enumerations;
|
|
|
|
using WeatherForecast.Models;
|
|
using WeatherForecast.Models.Responses;
|
|
using DataProviders.Collections;
|
|
using Core.DomainObjects.Documents;
|
|
|
|
namespace WeatherForecast.Services {
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public interface IShopItemsService {
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="category"></param>
|
|
/// <param name="currentPage"></param>
|
|
/// <param name="itemsPerPage"></param>
|
|
/// <param name="locale"></param>
|
|
/// <param name="searchText"></param>
|
|
/// <returns></returns>
|
|
(ShopItemsResponseModel?, IDomainResult) Get(Guid siteId, Guid? category, int currentPage, int itemsPerPage, string? locale, string? searchText);
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <returns></returns>
|
|
IDomainResult Delete(Guid siteId);
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
public class ShopItemsService : IShopItemsService {
|
|
private readonly ILogger<ShopItemsService> _logger;
|
|
private readonly IShopCatalogDataProvider _shopCatalogDataProvider;
|
|
private readonly ICategoryDataProvider _categoryDataProvider;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="logger"></param>
|
|
/// <param name="shopCatalogDataprovider"></param>
|
|
/// <param name="categoryDataProvider"></param>
|
|
public ShopItemsService(
|
|
ILogger<ShopItemsService> logger,
|
|
IShopCatalogDataProvider shopCatalogDataprovider,
|
|
ICategoryDataProvider categoryDataProvider
|
|
) {
|
|
_logger = logger;
|
|
_shopCatalogDataProvider = shopCatalogDataprovider;
|
|
_categoryDataProvider = categoryDataProvider;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <param name="category"></param>
|
|
/// <param name="currentPage"></param>
|
|
/// <param name="itemsPerPage"></param>
|
|
/// <param name="locale"></param>
|
|
/// <param name="searchText"></param>
|
|
/// <returns></returns>
|
|
public (ShopItemsResponseModel?, IDomainResult) Get(Guid siteId, Guid? category, int currentPage, int itemsPerPage, string? locale, string? searchText) {
|
|
try {
|
|
var (items, result) = _shopCatalogDataProvider.GetAll(siteId, currentPage > 0 ? ((currentPage - 1) * itemsPerPage) : 0, itemsPerPage);
|
|
|
|
if (!result.IsSuccess || items == null)
|
|
return (null, result);
|
|
|
|
|
|
var shopItems = new List<ShopItemResponseModel>();
|
|
foreach (var item in items) {
|
|
|
|
var categories = new List<Category>();
|
|
if (item.Categories != null) {
|
|
foreach (var catId in item.Categories) {
|
|
var (cat, getCategoryResult) = _categoryDataProvider.Get(siteId, catId);
|
|
if (!getCategoryResult.IsSuccess || cat == null)
|
|
return (null, getCategoryResult);
|
|
|
|
categories.Add(cat);
|
|
}
|
|
}
|
|
|
|
if(locale != null)
|
|
shopItems.Add(new ShopItemResponseModel(item, categories, Enumeration.FromDisplayName<Locales>(locale) ?? Locales.Us));
|
|
else
|
|
shopItems.Add(new ShopItemResponseModel(item, categories));
|
|
}
|
|
|
|
return shopItems.Count > 0
|
|
? IDomainResult.Success(new ShopItemsResponseModel(currentPage, 0, shopItems))
|
|
: IDomainResult.NotFound<ShopItemsResponseModel?>();
|
|
}
|
|
catch (Exception ex) {
|
|
return IDomainResult.Failed<ShopItemsResponseModel?>(ex.Message);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="siteId"></param>
|
|
/// <returns></returns>
|
|
public IDomainResult Delete(Guid siteId) {
|
|
try {
|
|
return _shopCatalogDataProvider.DeleteAll(siteId);
|
|
}
|
|
catch (Exception ex) {
|
|
return IDomainResult.Failed(ex.Message);
|
|
}
|
|
}
|
|
}
|
|
}
|