MediaController.cs 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. using System.Threading.Tasks;
  2. using GxPress.Auth;
  3. using GxPress.Repository.Interface.Media;
  4. using GxPress.Repository.Interface.Order;
  5. using GxPress.Request.Media;
  6. using GxPress.Result.Media;
  7. using GxPress.Service.Interface.Media;
  8. using Microsoft.AspNetCore.Authorization;
  9. using Microsoft.AspNetCore.Http;
  10. using Microsoft.AspNetCore.Mvc;
  11. namespace GxPress.Api.AppControllers
  12. {
  13. /// <summary>
  14. /// 媒体
  15. /// </summary>
  16. [Route("api/app/media")]
  17. [ApiController]
  18. [Authorize]
  19. public class MediaController : Controller
  20. {
  21. private readonly IMediaRepository mediaRepository;
  22. private readonly ILoginContext _loginContext;
  23. private readonly IMediaService mediaService;
  24. private IHttpContextAccessor _contextAccessor;
  25. private readonly IMediaLibraryRepository mediaLibraryRepository;
  26. private HttpContext _context { get { return _contextAccessor.HttpContext; } }
  27. private readonly IOrderRepository orderRepository;
  28. public MediaController(IMediaRepository mediaRepository, ILoginContext _loginContext, IMediaService mediaService, IHttpContextAccessor contextAccessor, IMediaLibraryRepository mediaLibraryRepository, IOrderRepository orderRepository)
  29. {
  30. this.mediaRepository = mediaRepository;
  31. this._loginContext = _loginContext;
  32. this.mediaService = mediaService;
  33. _contextAccessor = contextAccessor;
  34. this.mediaLibraryRepository = mediaLibraryRepository;
  35. this.orderRepository = orderRepository;
  36. }
  37. /// <summary>
  38. /// 书籍详情
  39. /// /// </summary>
  40. /// <param name="id"></param>
  41. /// <returns></returns>
  42. [HttpGet("book/{id}")]
  43. [AllowAnonymous]
  44. public async Task<BookMediaResult> GetBookMediaResult(int id)
  45. {
  46. var result = await mediaService.GetBookMediaResultAsync(id, _loginContext.AccountId);
  47. if (_loginContext.AccountId > 0 && result.FreeProportion == 0)
  48. result.IsBuy = true;
  49. else if (_loginContext.AccountId > 0)
  50. result.IsBuy = await orderRepository.GetExistsAsync(_loginContext.AccountId, id);
  51. return result;
  52. }
  53. /// <summary>
  54. /// 获取视频详情
  55. /// </summary>
  56. /// <param name="id"></param>
  57. /// <returns></returns>
  58. [HttpGet("video/{id}")]
  59. [AllowAnonymous]
  60. public async Task<VideoMediaResult> GetVideoMediaResultAsync(int id)
  61. {
  62. var result = await mediaService.GetVideoMediaResultAsync(id);
  63. if (_loginContext.AccountId > 0 && result.FreeProportion == 0)
  64. result.IsBuy = true;
  65. else if (_loginContext.AccountId > 0)
  66. result.IsBuy = await orderRepository.GetExistsAsync(_loginContext.AccountId, id);
  67. return result;
  68. }
  69. /// <summary>
  70. /// 获取章节内容
  71. /// </summary>
  72. /// <param name="request"></param>
  73. /// <returns></returns>
  74. [HttpPost("book/content")]
  75. [AllowAnonymous]
  76. public async Task<string> GetBookMediaContentResultAsync(BookCatalogRequest request)
  77. {
  78. request.UserId = _loginContext.AccountId;
  79. return await mediaService.GetBookMediaContentResultAsync(request);
  80. }
  81. }
  82. }