using DomainResults.Common;
using DataProviders.Collections;
using Core.Abstractions;
using Core.Enumerations;
using WeatherForecast.Models;
using WeatherForecast.Models.Responses;
namespace WeatherForecast.Services {
///
///
///
public interface IShopItemsService {
///
///
///
///
///
///
///
///
///
///
(ShopItemsResponseModel?, IDomainResult) Get(Guid siteId, Guid? category, int currentPage, int itemsPerPage, string? locale, string? searchText);
///
///
///
///
///
IDomainResult Delete(Guid siteId);
}
///
///
///
public class ShopItemsService : ServiceBase, IShopItemsService {
private readonly IShopCatalogDataProvider _shopCatalogDataProvider;
private readonly ICategoryDataProvider _categoryDataProvider;
///
///
///
///
///
///
public ShopItemsService(
ILogger logger,
IShopCatalogDataProvider shopCatalogDataprovider,
ICategoryDataProvider categoryDataProvider
) : base(logger) {
_shopCatalogDataProvider = shopCatalogDataprovider;
_categoryDataProvider = categoryDataProvider;
}
///
///
///
///
///
///
///
///
///
///
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();
foreach (var item in items) {
var (categories, getCategoryResult) = _categoryDataProvider.GetMany(siteId, item.Categories);
if (!getCategoryResult.IsSuccess || categories == null)
return IDomainResult.Failed();
if (locale != null)
shopItems.Add(new ShopItemResponseModel(item, categories, Enumeration.FromDisplayName(locale) ?? Locales.Us));
else
shopItems.Add(new ShopItemResponseModel(item, categories));
}
return shopItems.Count > 0
? IDomainResult.Success(new ShopItemsResponseModel(currentPage, 0, shopItems))
: IDomainResult.NotFound();
}
catch (Exception ex) {
_logger.LogError(ex, "Unhandled exception");
return IDomainResult.Failed(ex.Message);
}
}
///
///
///
///
///
public IDomainResult Delete(Guid siteId) {
try {
return _shopCatalogDataProvider.DeleteAll(siteId);
}
catch (Exception ex) {
_logger.LogError(ex, "Unhandled exception");
return IDomainResult.Failed(ex.Message);
}
}
}
}