李昊 4 gadi atpakaļ
vecāks
revīzija
9c924dd00e

BIN
gx_api/GxPress/.DS_Store


BIN
gx_api/GxPress/Api/GxPress.Api/.DS_Store


+ 46 - 19
gx_api/GxPress/Api/GxPress.Api/AdminControllers/AdminMediaController.cs

@@ -2,6 +2,8 @@
 using System.Collections.Generic;
 using System.Linq;
 using System.Threading.Tasks;
+using GxPress.Repository.Interface.Media;
+using GxPress.Result.Media;
 using Microsoft.AspNetCore.Mvc;
 
 // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
@@ -11,36 +13,61 @@ namespace GxPress.Api.AdminControllers
     [Route("api/[controller]")]
     public class AdminMediaController : Controller
     {
-        // GET: api/values
-        [HttpGet]
-        public IEnumerable<string> Get()
+        private readonly IMediaRepository mediaRepository;
+
+        public AdminMediaController(IMediaRepository mediaRepository)
         {
-            return new string[] { "value1", "value2" };
+            this.mediaRepository = mediaRepository;
         }
-
-        // GET api/values/5
+        /// <summary>
+        /// 详情
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
         [HttpGet("{id}")]
-        public string Get(int id)
+        public async Task<MediaResult> GetAsync(int id)
         {
-            return "value";
+            return await mediaRepository.GetAsync(id);
         }
-
-        // POST api/values
+        /// <summary>
+        /// 删除
+        /// </summary>
+        /// <param name="id"></param>
+        /// <returns></returns>
+        [HttpDelete("{id}")]
+        public async Task<bool> DeleteAsync(int id)
+        {
+            return await mediaRepository.DeleteAsync(id);
+        }
+        /// <summary>
+        /// 添加媒体
+        /// </summary>
+        /// <param name="result"></param>
+        /// <returns></returns>
         [HttpPost]
-        public void Post([FromBody]string value)
+        public async Task<bool> InsertAsync(MediaResult result)
         {
+            return await mediaRepository.InsertAsync(result);
         }
-
-        // PUT api/values/5
-        [HttpPut("{id}")]
-        public void Put(int id, [FromBody]string value)
+        /// <summary>
+        /// 修改
+        /// </summary>
+        /// <param name="result"></param>
+        /// <returns></returns>
+        [HttpPut]
+        public async Task<bool> UpdateAsync(MediaResult result)
         {
+            return await mediaRepository.UpdateAsync(result);
         }
-
-        // DELETE api/values/5
-        [HttpDelete("{id}")]
-        public void Delete(int id)
+        /// <summary>
+        /// 查询
+        /// </summary>
+        /// <param name="mediaType"></param>
+        /// <returns></returns>
+        [HttpPost("list/{mediaType}")]
+        public async Task<IEnumerable<Entity.tede2.Media.Media>> GetAllAsync(int mediaType)
         {
+            return await mediaRepository.GetAllAsync(mediaType);
         }
     }
 }

+ 26 - 2
gx_api/GxPress/Repository/GxPress.Repository.Implement/Media/MediaLibraryRepository.cs

@@ -1,10 +1,34 @@
 using System;
+using System.Collections.Generic;
+using AutoMapper;
+using Datory;
+using GxPress.Common.AppOptions;
+using GxPress.Common.Tools;
+using GxPress.Repository.Interface.Media;
+using Microsoft.Extensions.Options;
+
 namespace GxPress.Repository.Implement.Media
 {
-    public class MediaLibraryRepository
+    public class MediaLibraryRepository:IMediaLibraryRepository
     {
-        public MediaLibraryRepository()
+        private readonly Repository<Entity.tede2.Media.MediaLibrary> _repository;
+        private readonly IMapper _mapper;
+        private readonly string _connectionString;
+        private readonly string _databaseTypestr;
+        public MediaLibraryRepository(IOptionsMonitor<DatabaseOptions> dbOptionsAccessor, IMapper mapper)
         {
+            _databaseTypestr = dbOptionsAccessor.CurrentValue.DatabaseType;
+            _connectionString = dbOptionsAccessor.CurrentValue.ConnectionString;
+            var databaseType =
+                StringUtils.ToEnum<DatabaseType>(dbOptionsAccessor.CurrentValue.DatabaseType, DatabaseType.MySql);
+            var database = new Database(databaseType, dbOptionsAccessor.CurrentValue.ConnectionString);
+            _repository = new Repository<Entity.tede2.Media.MediaLibrary>(database);
+            _mapper = mapper;
         }
+
+        public IDatabase Database => _repository.Database;
+        public string TableName => _repository.TableName;
+        public List<TableColumn> TableColumns => _repository.TableColumns;
+
     }
 }

+ 36 - 9
gx_api/GxPress/Repository/GxPress.Repository.Implement/Media/MediaRepository.cs

@@ -12,7 +12,7 @@ using System.Transactions;
 
 namespace GxPress.Repository.Implement.Media
 {
-    public class MediaRepository: IMediaRepository
+    public class MediaRepository : IMediaRepository
     {
         private readonly Repository<Entity.tede2.Media.Media> _repository;
         private readonly Repository<Entity.tede2.Media.MediaLibrary> mediaLibraryRepository;
@@ -39,11 +39,11 @@ namespace GxPress.Repository.Implement.Media
         {
             var result = new MediaResult();
             //获取媒体
-            var media= await _repository.GetAsync(id);
+            var media = await _repository.GetAsync(id);
             result = _mapper.Map<MediaResult>(media);
             //获取媒体库资源
-            var mediaLibrarys= await mediaLibraryRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
-            result.MediaLibraryRequest = mediaLibrarys.ToList().Select(n=> _mapper.Map<MediaLibraryResult>(n)).ToList();
+            var mediaLibrarys = await mediaLibraryRepository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), id));
+            result.MediaLibraryRequest = mediaLibrarys.ToList().Select(n => _mapper.Map<MediaLibraryResult>(n)).ToList();
             return result;
         }
 
@@ -57,8 +57,8 @@ namespace GxPress.Repository.Implement.Media
         /// <param name="result"></param>
         /// <returns></returns>
         public async Task<bool> InsertAsync(MediaResult result)
-        { 
-           var model = _mapper.Map<Entity.tede2.Media.Media>(result);
+        {
+            var model = _mapper.Map<Entity.tede2.Media.Media>(result);
             try
             {
                 using (var transactionScope = new TransactionScope())
@@ -69,9 +69,9 @@ namespace GxPress.Repository.Implement.Media
                         foreach (var item in result.MediaLibraryRequest)
                         {
                             var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
-                           await mediaLibraryRepository.InsertAsync(mediaLibrary);
+                            await mediaLibraryRepository.InsertAsync(mediaLibrary);
                         }
-                     
+
                     }
                     //提交事务
                     transactionScope.Complete();
@@ -81,7 +81,7 @@ namespace GxPress.Repository.Implement.Media
             {
                 throw new Common.Exceptions.BusinessException(ex.Message);
             }
-          
+
             return true;
         }
 
@@ -98,5 +98,32 @@ namespace GxPress.Repository.Implement.Media
         {
             return await _repository.GetAllAsync(Q.Where(nameof(Entity.tede2.Media.Media.MediaType), mediaType).OrderByDesc(nameof(Entity.tede2.Media.Media.Sort)));
         }
+
+        public async Task<bool> UpdateAsync(MediaResult result)
+        {
+            var model = _mapper.Map<Entity.tede2.Media.Media>(result);
+            try
+            {
+                using (var transactionScope = new TransactionScope())
+                {
+                    await _repository.UpdateAsync(model);
+                    //删除媒体库
+                    await mediaLibraryRepository.DeleteAsync(Q.Where(nameof(Entity.tede2.Media.MediaLibrary.MediaId), result.Id));
+                    foreach (var item in result.MediaLibraryRequest)
+                    {
+                        var mediaLibrary = _mapper.Map<Entity.tede2.Media.MediaLibrary>(item);
+                        await mediaLibraryRepository.InsertAsync(mediaLibrary);
+                    }
+                    //提交事务
+                    transactionScope.Complete();
+                }
+            }
+            catch (System.Exception ex)
+            {
+                throw new Common.Exceptions.BusinessException(ex.Message);
+            }
+
+            return true;
+        }
     }
 }