using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Common.Tools;
using GxPress.Request.App.ElasticSearch;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AppControllers
{
    [Route("api/app/elastics")]
    [ApiController]
    [Authorize]
    public class ElasticsSearchController : ControllerBase
    {
        private readonly ILoginContext _loginContext;
        public ElasticsSearchController(ILoginContext loginContext)
        {
            _loginContext = loginContext;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<Nest.Result> Delete(string id)
        {
            var elasticsSearchHelper = new ElasticSearchHelper();
            return await elasticsSearchHelper.Delete(id);
        }
        /// <summary>
        /// 搜索结果
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost("search")]
        public async Task<PagedList<TedeData>> Search(ElasticSearchRequest request)
        {
            var elasticsSearchHelper = new ElasticSearchHelper();
            var searchResult = await elasticsSearchHelper.SearchAsync(request.Key,request.SearchType,_loginContext.AccountId,request.Page,request.PerPage);
            return searchResult;
        }
        /// <summary>
        /// 根据ID查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<TedeData> Get(string id)
        {
            var elasticsSearchHelper = new ElasticSearchHelper();
            return await elasticsSearchHelper.Get(id);
        }
    }
}