using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Repository.Interface.Media;
using GxPress.Repository.Interface.Order;
using GxPress.Request.Media;
using GxPress.Result.Media;
using GxPress.Service.Interface.Media;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace GxPress.Api.AppControllers
{
///
/// 媒体
///
[Route("api/app/media")]
[ApiController]
[Authorize]
public class MediaController : Controller
{
private readonly IMediaRepository mediaRepository;
private readonly ILoginContext _loginContext;
private readonly IMediaService mediaService;
private IHttpContextAccessor _contextAccessor;
private readonly IMediaLibraryRepository mediaLibraryRepository;
private HttpContext _context { get { return _contextAccessor.HttpContext; } }
private readonly IOrderRepository orderRepository;
public MediaController(IMediaRepository mediaRepository, ILoginContext _loginContext, IMediaService mediaService, IHttpContextAccessor contextAccessor, IMediaLibraryRepository mediaLibraryRepository, IOrderRepository orderRepository)
{
this.mediaRepository = mediaRepository;
this._loginContext = _loginContext;
this.mediaService = mediaService;
_contextAccessor = contextAccessor;
this.mediaLibraryRepository = mediaLibraryRepository;
this.orderRepository = orderRepository;
}
///
/// 详情
///
///
///
[HttpGet("{id}")]
[AllowAnonymous]
public async Task GetAsync(int id)
{
var result = await mediaService.GetAsync(id);
if (_loginContext.AccountId > 0 && result.FreeProportion == 0)
result.IsBuy = true;
else if (_loginContext.AccountId > 0)
result.IsBuy = await orderRepository.GetExistsAsync(_loginContext.AccountId, id);
return result;
}
///
/// 书籍详情
/// ///
///
///
[HttpGet("book/{id}")]
[AllowAnonymous]
public async Task GetBookMediaResult(int id)
{
var result = await mediaService.GetBookMediaResultAsync(id, _loginContext.AccountId);
if (_loginContext.AccountId > 0 && result.FreeProportion == 0)
result.IsBuy = true;
else if (_loginContext.AccountId > 0)
result.IsBuy = await orderRepository.GetExistsAsync(_loginContext.AccountId, id);
return result;
}
///
/// 获取视频详情
///
///
///
[HttpGet("video/{id}")]
[AllowAnonymous]
public async Task GetVideoMediaResultAsync(int id)
{
var result = await mediaService.GetVideoMediaResultAsync(id);
if (_loginContext.AccountId > 0 && result.FreeProportion == 0)
result.IsBuy = true;
else if (_loginContext.AccountId > 0)
result.IsBuy = await orderRepository.GetExistsAsync(_loginContext.AccountId, id);
return result;
}
///
/// 获取章节内容
///
///
///
[HttpPost("book/content")]
[AllowAnonymous]
public async Task GetBookMediaContentResultAsync(BookCatalogRequest request)
{
request.UserId = _loginContext.AccountId;
return await mediaService.GetBookMediaContentResultAsync(request);
}
}
}