using System.Collections.Generic;
using System.Threading.Tasks;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Repository.Interface.Attach;
using GxPress.Request.Attach;
using GxPress.Service.Interface.Attach;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace GxPress.Api.AdminControllers
{
    /// <summary>
    /// 名栏 公众号 期刊
    /// </summary>
    [Route("/api/admin/attach")]
    [ApiController]
    [Authorize]
    public class AdminAttachController : Controller
    {
        private readonly IAttachRepository repository;

        private readonly IAttachService service;
        private readonly ILoginContext _loginContext;
        public AdminAttachController(IAttachRepository repository, ILoginContext _loginContext, IAttachService service)
        {
            this.repository = repository;
            this._loginContext = _loginContext;
            this.service = service;
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<bool> DeleteAsync(int id)
        {
            return await repository.DeleteAsync(id);
        }
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        [HttpPost("list")]
        public async Task<PagedList<Entity.tede2.Attach.Attach>> GetAllAsync(AttachRequest request)
        {
            return await repository.GetAllAsync(request);
        }
        /// <summary>
        /// 获取所有
        /// </summary>
        /// <returns></returns>
        [HttpPost("search")]
        public async Task<IEnumerable<Entity.tede2.Attach.Attach>> GetSearchAllAsync(AttachRequest request)
        {
            return await repository.GetSearchAllAsync(request);
        }
        /// <summary>
        /// 获取详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<Entity.tede2.Attach.Attach> GetAsync(int id)
        {
            return await repository.GetAsync(id);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<int> InsertAsync(Entity.tede2.Attach.Attach note)
        {
            note.AdminId = _loginContext.AccountId;
            note.Creator = _loginContext.Name;
            return await service.InsertAsync(note);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="note"></param>
        /// <returns></returns>
        [HttpPut]
        public async Task<bool> UpdateAsync(Entity.tede2.Attach.Attach note)
        {
            note.AdminId = _loginContext.AccountId;
            note.Creator = _loginContext.Name;
            return await service.UpdateAsync(note);
        }
    }
}