using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.EnumConst;
using GxPress.Repository.Interface.Media;
using GxPress.Repository.Interface.TrackLog;
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;
using GxPress.Common.Tools;
// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace GxPress.Api.AdminControllers
{
[Route("api/admin/media")]
[ApiController]
[Authorize]
public class AdminMediaController : Controller
{
private readonly IMediaRepository mediaRepository;
private readonly ILoginContext _loginContext;
private readonly IMediaService mediaService;
private readonly ITrackLogRepository trackLogRepository;
private readonly IHttpContextAccessor httpContextAccessor;
public AdminMediaController(IMediaRepository mediaRepository, ILoginContext _loginContext, IMediaService mediaService, ITrackLogRepository trackLogRepository, IHttpContextAccessor httpContextAccessor)
{
this.mediaRepository = mediaRepository;
this._loginContext = _loginContext;
this.mediaService = mediaService;
this.trackLogRepository = trackLogRepository;
this.httpContextAccessor = httpContextAccessor;
}
///
/// 详情
///
///
///
[HttpGet("{id}")]
public async Task GetAsync(int id)
{
return await mediaService.GetAsync(id);
}
///
/// 删除
///
///
///
[HttpDelete("{id}")]
public async Task DeleteAsync(int id)
{
var result = await mediaRepository.GetAsync(id);
var success = await mediaRepository.DeleteAsync(id);
if (success)
{
var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
var mediaTypeValue = ((ResourceTypeConst)result.MediaType).GetDescriptionOriginal();
var remark = $"删除{mediaTypeValue}{result.Title}";
await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
}
return success;
}
///
/// 添加媒体
///
///
///
[HttpPost]
public async Task InsertAsync(MediaResult result)
{
result.AdminId = _loginContext.AccountId;
result.Creator = _loginContext.Name;
var success = await mediaService.InsertAsync(result);
if (success)
{
var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
var mediaTypeValue = ((ResourceTypeConst)result.MediaType).GetDescriptionOriginal();
var remark = $"添加{mediaTypeValue}{result.Title}";
await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
}
return success;
}
///
/// 修改
///
///
///
[HttpPut]
public async Task UpdateAsync(MediaResult result)
{
var success = await mediaRepository.UpdateAsync(result);
if (success)
{
var ip = httpContextAccessor.HttpContext.Connection.RemoteIpAddress.ToString();
var mediaTypeValue = ((ResourceTypeConst)result.MediaType).GetDescriptionOriginal();
var remark = $"修改{mediaTypeValue}{result.Title}";
await trackLogRepository.InsertAsync(_loginContext.Name, _loginContext.AccountId, ip, remark);
}
return success;
}
///
/// 查询
///
///
///
[HttpPost("list")]
public async Task> GetAllAsync(MediaSearchRequest request)
{
return await mediaRepository.GetAllAsync(request);
}
}
}