using System.Security.Claims;
using System.Threading.Tasks;
using GxPress.Api.Tools;
using GxPress.Auth;
using GxPress.Common.Page;
using GxPress.Entity;
using GxPress.EnumConst;
using GxPress.Repository.Interface;
using GxPress.Request.Admin;
using GxPress.Result.Admin;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
namespace GxPress.Api.AdminControllers
{
///
/// 管理员
///
[Route("api/admin/admin")]
[ApiController]
[Authorize(Roles = Roles.Admin)]
public class AdminController : ControllerBase
{
private readonly JwtOptions _jwtOptions;
private readonly ILogger _logger;
private readonly IAdminRepository _adminRepository;
public AdminController(IOptions jwtOptions, ILogger logger, IAdminRepository adminRepository)
{
_jwtOptions = jwtOptions.Value;
_logger = logger;
_adminRepository = adminRepository;
}
///
/// 登录
///
///
///
[HttpPost("signin")]
[AllowAnonymous]
public async Task SignIn(AdminSignInRequest request)
{
var result = await _adminRepository.SignInAsync(request);
var claims = new[]
{
new Claim(ClaimTypes.NameIdentifier,result.AdminId.ToString()),
new Claim(ClaimTypes.Role,AccountTypeConst.Admin.ToString())
};
result.Token = TokenHelper.BuildToken(_jwtOptions, claims);
_logger.LogInformation("admin token:{token}", result.Token);
return result;
}
///
/// 注册
///
///
///
[HttpPost("signup")]
public async Task SignUp([FromBody]AdminSignUpRequest request)
{
return await _adminRepository.SignUpAsync(request);
}
///
/// 删除
///
///
///
[HttpDelete("{id}")]
public async Task Delete(int id)
{
return await _adminRepository.DeleteAsync(id);
}
///
/// 详情
///
///
///
[HttpGet("{id}")]
public async Task Get(int id)
{
return await _adminRepository.GetDetailAsync(id);
}
///
/// 修改
///
///
///
///
[HttpPut("{id}")]
public async Task Update(int id, [FromBody] Admin request)
{
return await _adminRepository.UpdateAsync(id, request);
}
///
/// 修改
///
///
///
[HttpPut("updatemenu")]
public async Task UpdateMenu( [FromBody] Admin request)
{
return await _adminRepository.UpdateQAsync(request);
}
///
/// 分页列表
///
///
///
[HttpPost("page")]
public async Task> GetPagedList([FromBody]AdminPageSearchRequest request)
{
return await _adminRepository.GetPagedList(request);
}
///
/// 修改密码
///
///
///
[HttpPut("updatepassword")]
public async Task UpdatePassWord([FromBody] AdminUpdatePassWordRequest request)
{
return await _adminRepository.UpdatePassWord(request);
}
///
/// 修改密码
///
///
///
[HttpPut("admin-update-password")]
public async Task AdminUpdatePassWord([FromBody] AdminUpdatePassWordRequest request)
{
return await _adminRepository.AdminUpdatePassWord(request);
}
}
}