using Infrastructure;
using Infrastructure.Cache;
using Infrastructure.CloudSdk.minio;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using OpenAuth.App.ServiceApp;
using OpenAuth.Repository.Domain;
using System.Net.Http;

namespace OpenAuth.WebApi.Controllers.ServiceControllers
{
    /// <summary>
    /// 算法库
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AlgorithmsRepositoryController : ControllerBase
    {
        private readonly AlgorithmsRepositoryApp _app;
        public AlgorithmsRepositoryController(AlgorithmsRepositoryApp app)
        {
            _app = app;
        }
        /// <summary>
        /// 添加算法
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> AddAlgorithmsRepository(LasaAlgorithmsRepository info)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.AddAlgorithmsRepository(info);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 修改算法
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> UpdateAlgorithmsRepository(LasaAlgorithmsRepository info)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.UpdateAlgorithmsRepository(info);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 删除算法
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<Response<bool>> DeleteAlgorithmsRepository(string id)
        {
            var result = new Response<bool>();
            try
            {
                result = await _app.DeleteAlgorithmsRepository(id);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }
            return result;
        }
        /// <summary>
        /// 获取算法列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<Response<PageInfo<List<LasaAlgorithmsRepository>>>> GetAlgorithmsRepositoryList(int page, int limit, string key)
        {
            var result = new Response<PageInfo<List<LasaAlgorithmsRepository>>>();
            try
            {
                result = await _app.GetPageList(page, limit, key);
            }
            catch (Exception ex)
            {
                result.Code = 500;
                result.Message = ex.Message;
            }

            return result;
        }
    }
}
