118 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			118 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using DomainResults.Common;
 | |
| 
 | |
| using DataProviders.Collections;
 | |
| 
 | |
| using Core.Abstractions;
 | |
| using Core.Enumerations;
 | |
| 
 | |
| using WeatherForecast.Models;
 | |
| using WeatherForecast.Models.Responses;
 | |
| 
 | |
| 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 : ServiceBase<ShopItemsService>, IShopItemsService {
 | |
| 
 | |
|     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
 | |
|     ) : base(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, getCategoryResult) = _categoryDataProvider.GetMany(siteId, item.Categories);
 | |
|           if (!getCategoryResult.IsSuccess || categories == null)
 | |
|             return IDomainResult.Failed<ShopItemsResponseModel?>();
 | |
| 
 | |
|           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) {
 | |
|         _logger.LogError(ex, "Unhandled exception");
 | |
|         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) {
 | |
|         _logger.LogError(ex, "Unhandled exception");
 | |
|         return IDomainResult.Failed(ex.Message);
 | |
|       }
 | |
|     }
 | |
|   }
 | |
| }
 |