李昊 4 年之前
父节点
当前提交
70c1bd93fe

+ 1 - 1
gx_api/GxPress/Api/GxPress.Api/AdminControllers/AdminMediaController.cs

@@ -35,7 +35,7 @@ namespace GxPress.Api.AdminControllers
         [HttpGet("{id}")]
         public async Task<MediaResult> GetAsync(int id)
         {
-            return await mediaRepository.GetAsync(id);
+            return await mediaService.GetAsync(id);
         }
         /// <summary>
         /// 删除

+ 14 - 0
gx_api/GxPress/Model/GxPress.Mappings/CategoryMapping.cs

@@ -0,0 +1,14 @@
+using AutoMapper;
+using GxPress.Entity.tede2.Category;
+using GxPress.Result.Category;
+
+namespace GxPress.Mappings
+{
+    public class CategoryMapping : Profile
+    {
+        public CategoryMapping()
+        {
+            CreateMap<Category, CategoryResult>();
+        }
+    }
+}

+ 48 - 0
gx_api/GxPress/Model/GxPress.Result/Category/ CategoryResult.cs

@@ -0,0 +1,48 @@
+using System.Collections.Generic;
+
+namespace GxPress.Result.Category
+{
+    /// <summary>
+    /// 类别
+    /// </summary>
+    public class CategoryResult
+    {
+        /// <summary>
+        /// id
+        /// </summary>
+        /// <value></value>
+        public int Id{get;set;}
+        /// <summary>
+        /// 名称
+        /// </summary>
+        public string Name { get; set; }
+        /// <summary>
+        /// 是否禁用
+        /// </summary>
+        /// <value></value>
+        public bool IsDisable { get; set; }
+        /// <summary>
+        /// 父级ID
+        /// </summary>
+        /// <value></value>
+
+        public int ParentId { get; set; }
+
+        /// <summary>
+        /// 排序
+        /// </summary>
+        /// <value></value>
+
+        public int Sort { get; set; }
+        /// <summary>
+        /// 是否存在子集
+        /// </summary>
+        /// <value></value>
+        public bool IsChildren { get; set; }
+        /// <summary>
+        /// 子集
+        /// </summary>
+        /// <value></value>
+        public List<CategoryResult> Children { get; set; }
+    }
+}

+ 6 - 0
gx_api/GxPress/Model/GxPress.Result/Media/MediaResult.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Generic;
 using GxPress.EnumConst;
+using GxPress.Result.Category;
 
 namespace GxPress.Result.Media
 {
@@ -204,6 +205,11 @@ namespace GxPress.Result.Media
         /// <value></value>
 
         public int JournalsCategory { get; set; }
+        /// <summary>
+        /// 所有类别
+        /// </summary>
+        /// <value></value>
+        public List<CategoryResult> CategoryResults { get; set; }
 
     }
     /// <summary>

+ 45 - 1
gx_api/GxPress/Service/GxPress.Service.Implement/Media/MediaService.cs

@@ -3,6 +3,9 @@ using GxPress.Repository.Interface.Category;
 using GxPress.Repository.Interface.Media;
 using System.Threading.Tasks;
 using GxPress.Result.Media;
+using AutoMapper;
+using System.Linq;
+using GxPress.Result.Category;
 
 namespace GxPress.Service.Implement.Media
 {
@@ -10,10 +13,12 @@ namespace GxPress.Service.Implement.Media
     {
         private readonly ICategoryRepository categoryRepository;
         private readonly IMediaRepository mediaRepository;
-        public MediaService(ICategoryRepository categoryRepository, IMediaRepository mediaRepository)
+        private readonly IMapper _mapper;
+        public MediaService(ICategoryRepository categoryRepository, IMediaRepository mediaRepository, IMapper _mapper)
         {
             this.categoryRepository = categoryRepository;
             this.mediaRepository = mediaRepository;
+            this._mapper = _mapper;
         }
         public async Task<bool> InsertAsync(MediaResult result)
         {
@@ -24,5 +29,44 @@ namespace GxPress.Service.Implement.Media
             // }
             return await mediaRepository.InsertAsync(result);
         }
+        public async Task<MediaResult> GetAsync(int id)
+        {
+            var result = new MediaResult();
+            //获取媒体
+            result = await mediaRepository.GetAsync(id);
+            //获取类别
+            var categoryAll = await categoryRepository.GetAllAsync();
+            result.CategoryResults = categoryAll.Where(n => n.ParentId == 0).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
+            foreach (var item in result.CategoryResults)
+            {
+                item.IsChildren = categoryAll.Any(n => n.ParentId == item.Id);
+                item.Children = categoryAll.Where(n => n.ParentId == item.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
+                if (item.IsChildren)
+                {
+                    foreach (var chidren in item.Children)
+                    {
+                        chidren.IsChildren = categoryAll.Any(n => n.ParentId == chidren.Id);
+                        chidren.Children = categoryAll.Where(n => n.ParentId == chidren.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
+                    }
+                }
+            }
+            return result;
+        }
+
+        // public List<CategoryResult> GetAllCategoryResult(IEnumerable<Entity.tede2.Category.Category> categorys, int id, List<CategoryResult> categoryResults)
+        // {
+        //     var result = new List<CategoryResult>();
+        //     result = categorys.Where(n => n.ParentId == id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
+        //     foreach (var item in result)
+        //     {
+        //         item.IsChildren = categorys.Any(n => n.ParentId == item.Id);
+        //         item.Children = categorys.Where(n => n.ParentId == item.Id).Select(n => _mapper.Map<CategoryResult>(n)).ToList();
+        //         foreach (var children in item.Children)
+        //         {
+        //             GetAllCategoryResult(categorys,children.Id,categoryResults);
+        //         }
+        //     }
+        //     return result;
+        // }
     }
 }

+ 1 - 0
gx_api/GxPress/Service/GxPress.Service.Interface/Media/IMediaService.cs

@@ -6,5 +6,6 @@ namespace GxPress.Service.Interface.Media
     public interface IMediaService:IService
     {
          Task<bool> InsertAsync(MediaResult result);
+         Task<MediaResult> GetAsync(int id);
     }
 }