AdminMediaController.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. using System.Threading.Tasks;
  2. using GxPress.Auth;
  3. using GxPress.Common.Page;
  4. using GxPress.EnumConst;
  5. using GxPress.Repository.Interface.Media;
  6. using GxPress.Repository.Interface.TrackLog;
  7. using GxPress.Request.Media;
  8. using GxPress.Result.Media;
  9. using GxPress.Service.Interface.Media;
  10. using Microsoft.AspNetCore.Authorization;
  11. using Microsoft.AspNetCore.Http;
  12. using Microsoft.AspNetCore.Mvc;
  13. using GxPress.Common.Tools;
  14. // For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
  15. namespace GxPress.Api.AdminControllers
  16. {
  17. [Route("api/admin/media")]
  18. [ApiController]
  19. [Authorize]
  20. public class AdminMediaController : Controller
  21. {
  22. private readonly IMediaRepository mediaRepository;
  23. private readonly ILoginContext _loginContext;
  24. private readonly IMediaService mediaService;
  25. private readonly ITrackLogRepository trackLogRepository;
  26. private readonly IHttpContextAccessor httpContextAccessor;
  27. public AdminMediaController(IMediaRepository mediaRepository, ILoginContext _loginContext, IMediaService mediaService, ITrackLogRepository trackLogRepository, IHttpContextAccessor httpContextAccessor)
  28. {
  29. this.mediaRepository = mediaRepository;
  30. this._loginContext = _loginContext;
  31. this.mediaService = mediaService;
  32. this.trackLogRepository = trackLogRepository;
  33. this.httpContextAccessor = httpContextAccessor;
  34. }
  35. /// <summary>
  36. /// 详情
  37. /// </summary>
  38. /// <param name="id"></param>
  39. /// <returns></returns>
  40. [HttpGet("{id}")]
  41. public async Task<MediaResult> GetAsync(int id)
  42. {
  43. return await mediaService.GetAsync(id);
  44. }
  45. /// <summary>
  46. /// 删除
  47. /// </summary>
  48. /// <param name="id"></param>
  49. /// <returns></returns>
  50. [HttpDelete("{id}")]
  51. public async Task<bool> DeleteAsync(int id)
  52. {
  53. var result = await mediaRepository.GetAsync(id);
  54. var success = await mediaRepository.DeleteAsync(id);
  55. if (success)
  56. {
  57. var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
  58. var mediaTypeValue = ((ResourceTypeConst)result.MediaType).GetDescriptionOriginal();
  59. var remark = $"删除{mediaTypeValue}{result.Title}";
  60. await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
  61. }
  62. return success;
  63. }
  64. /// <summary>
  65. /// 添加媒体
  66. /// </summary>
  67. /// <param name="result"></param>
  68. /// <returns></returns>
  69. [HttpPost]
  70. public async Task<bool> InsertAsync(MediaResult result)
  71. {
  72. result.AdminId = _loginContext.AccountId;
  73. result.Creator = _loginContext.Name;
  74. var success = await mediaService.InsertAsync(result);
  75. if (success)
  76. {
  77. var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
  78. var mediaTypeValue = ((ResourceTypeConst)result.MediaType).GetDescriptionOriginal();
  79. var remark = $"添加{mediaTypeValue}{result.Title}";
  80. await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
  81. }
  82. return success;
  83. }
  84. /// <summary>
  85. /// 修改
  86. /// </summary>
  87. /// <param name="result"></param>
  88. /// <returns></returns>
  89. [HttpPut]
  90. public async Task<bool> UpdateAsync(MediaResult result)
  91. {
  92. var success = await mediaRepository.UpdateAsync(result);
  93. if (success)
  94. {
  95. var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
  96. var mediaTypeValue = ((ResourceTypeConst)result.MediaType).GetDescriptionOriginal();
  97. var remark = $"修改{mediaTypeValue}{result.Title}";
  98. await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
  99. }
  100. return success;
  101. }
  102. /// <summary>
  103. /// 查询
  104. /// </summary>
  105. /// <param name="request"></param>
  106. /// <returns></returns>
  107. [HttpPost("list")]
  108. public async Task<PagedList<Entity.tede2.Media.Media>> GetAllAsync(MediaSearchRequest request)
  109. {
  110. return await mediaRepository.GetAllAsync(request);
  111. }
  112. }
  113. }