using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Repository.Interface;
using GxPress.Request.Comment;
using GxPress.Result.Comment;
using Microsoft.AspNetCore.Mvc;
using GxPress.Common.Exceptions;

namespace GxPress.Api.AppControllers
{
    /// <summary>
    /// 评论
    /// </summary>
    [Route("api/app/comment")]
    [ApiController]
    public class CommentController : ControllerBase
    {
        private readonly ICommentRepository _commentRepository;
        private readonly ILoginContext _loginContext;
        public CommentController(ICommentRepository commentRepository, ILoginContext loginContext)
        {
            _commentRepository = commentRepository;
            _loginContext = loginContext;
        }

        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("add")]
        public async Task<bool> Add([FromBody] CommentInRequest request)
        {
            // request.Content = StringUtils.RemoveEmoji(request.Content);
            request.UserId = _loginContext.AccountId;
            if (string.IsNullOrWhiteSpace(request.Content))
                throw new BusinessException("评论内容为空");
            return await _commentRepository.CommentInAsync(request) > 0;
        }
        /// <summary>
        /// 分页显示数据
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("list")]
        public async Task<PagedList<CommentPageResult>> GetPagedList([FromBody] CommentSearchPageRequest request)
        {
            request.UserId = _loginContext.AccountId;
            return await _commentRepository.GetPagedList(request);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpDelete("delete")]
        public async Task<bool> Delete([FromBody] CommentDeleteRequest request)
        {
            return await _commentRepository.DeleteCommentAsync(request);
        }
    }
}