using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Entity;
using GxPress.Repository.Interface;
using GxPress.Request.AppVersion;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace GxPress.Api.AdminControllers
{
    /// <summary>
    /// APP版本管理
    /// </summary>
    [Route("api/admin/app-version")]
    [ApiController]
    [Authorize]
    public class AdminAppVersionController : ControllerBase
    {
        private readonly ILogger<AdminAppVersionController> _logger;
        private readonly IAppVersionRepository _appVersionRepository;

        public AdminAppVersionController(ILogger<AdminAppVersionController> logger, IAppVersionRepository appVersionRepository)
        {
            _logger = logger;
            _appVersionRepository = appVersionRepository;
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="request">参数</param>
        /// <returns></returns>
        [HttpPut("add")]
        public async Task<bool> InAppVersion([FromBody] AppVersionInRequest request)
        {
            var appVersion = new AppVersion { VersionType = request.VersionType, VersionNumber = request.VersionNumber, Describe = request.Describe, QrCodeUrl = request.QrCodeUrl };
            return await _appVersionRepository.InsertAsync(appVersion) > 0;
        }

        /// <summary>
        /// 详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<AppVersion> Get(int id)
        {
            return await _appVersionRepository.GetAsync(id);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<bool> Update(int id, [FromBody] AppVersion request)
        {
            request.Id = id;
            return await _appVersionRepository.UpdateAsync(request);
        }
        /// <summary>
        /// 查询
        /// </summary>
        /// <returns></returns>
        [HttpPost("list")]
        public async Task<IEnumerable<AppVersion>> GetAppVersionAll()
        {
            return await _appVersionRepository.GetAppVersionAsync();
        }
      
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        [AllowAnonymous]
        public async Task<bool> Delete(int id)
        {
            return await _appVersionRepository.DeleteAsync(id);
        }
    }
}