113 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			113 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using DomainResults.Common;
 | |
| 
 | |
| using Core.Abstractions;
 | |
| using Core.Enumerations;
 | |
| using DataProviders.Collections;
 | |
| 
 | |
| using WeatherForecast.Models.Responses;
 | |
| 
 | |
| namespace WeatherForecast.Services {
 | |
| 
 | |
|   /// <summary>
 | |
|   /// 
 | |
|   /// </summary>
 | |
|   public interface IBlogItemsService {
 | |
| 
 | |
|     /// <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>
 | |
|     (BlogItemsResponseModel?, 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 BlogItemsService : ServiceBase<BlogItemsService>, IBlogItemsService {
 | |
| 
 | |
|     private readonly IBlogCatalogDataProvider _blogCatalogDataProvider;
 | |
|     private readonly ICategoryDataProvider _categoryDataProvider;
 | |
| 
 | |
|     /// <summary>
 | |
|     /// 
 | |
|     /// </summary>
 | |
|     /// <param name="logger"></param>
 | |
|     /// <param name="blogCatalogDataProvider"></param>
 | |
|     /// <param name="categoryDataProvider"></param>
 | |
|     public BlogItemsService(
 | |
|       ILogger<BlogItemsService> logger,
 | |
|       IBlogCatalogDataProvider blogCatalogDataProvider,
 | |
|       ICategoryDataProvider categoryDataProvider
 | |
|     ) : base(logger) {
 | |
|       _blogCatalogDataProvider = blogCatalogDataProvider;
 | |
|       _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 (BlogItemsResponseModel?, IDomainResult) Get(Guid siteId, Guid? category, int currentPage, int itemsPerPage, string? locale, string? searchText) {
 | |
|       try {
 | |
|         var (items, result) = _blogCatalogDataProvider.GetAll(siteId, currentPage > 0 ? ((currentPage - 1) * itemsPerPage) : 0, itemsPerPage);
 | |
|         if (!result.IsSuccess || items == null)
 | |
|           return (null, result);
 | |
| 
 | |
|         var blogItems = new List<BlogItemResponseModel>();
 | |
|         foreach (var item in items) {
 | |
|           var (categories, getCategoryResult) = _categoryDataProvider.GetMany(siteId, item.Categories);
 | |
|           if (!getCategoryResult.IsSuccess || categories == null)
 | |
|             return IDomainResult.Failed<BlogItemsResponseModel?>();
 | |
| 
 | |
|           if (locale != null)
 | |
|             blogItems.Add(new BlogItemResponseModel(item, categories, Enumeration.FromDisplayName<Locales>(locale) ?? Locales.Us));
 | |
|           else
 | |
|             blogItems.Add(new BlogItemResponseModel(item, categories));
 | |
|         }
 | |
| 
 | |
|         return blogItems.Count > 0
 | |
|           ? IDomainResult.Success(new BlogItemsResponseModel(currentPage, 0, blogItems))
 | |
|           : IDomainResult.NotFound<BlogItemsResponseModel?>();
 | |
|       }
 | |
|       catch (Exception ex) {
 | |
|         _logger.LogError(ex, "Unhandled exception");
 | |
|         return IDomainResult.Failed<BlogItemsResponseModel?>(ex.Message);
 | |
|       }
 | |
|     }
 | |
| 
 | |
|     /// <summary>
 | |
|     /// 
 | |
|     /// </summary>
 | |
|     /// <param name="siteId"></param>
 | |
|     /// <returns></returns>
 | |
|     public IDomainResult Delete(Guid siteId) {
 | |
|       try {
 | |
|         return _blogCatalogDataProvider.DeleteAll(siteId);
 | |
|       }
 | |
|       catch (Exception ex) {
 | |
|         _logger.LogError(ex, "Unhandled exception");
 | |
|         return IDomainResult.Failed(ex.Message);
 | |
|       }
 | |
|     }
 | |
|   }
 | |
| }
 |