using Kean.Application.Command.Interfaces;
using Kean.Application.Command.ViewModels;
using Microsoft.AspNetCore.Mvc;
using System.Threading.Tasks;
namespace Kean.Presentation.Rest.Controllers
{
///
/// 会话服务
///
[ApiController, Route("api/sessions")]
public class SessionsController : ControllerBase
{
private readonly IIdentityService _identityCommandService; // 身份命令服务
///
/// 依赖注入
///
public SessionsController(
IIdentityService identityCommandService)
{
_identityCommandService = identityCommandService;
}
///
/// 创建资源(登录)
///
/// 成功
/// 账号或密码错误
/// 账号被冻结
[HttpPost, Anonymous]
[ProducesResponseType(201)]
[ProducesResponseType(401)]
[ProducesResponseType(423)]
public async Task Login(User user, [FromMiddleware] string ip, [FromMiddleware] string ua)
{
var token = await _identityCommandService.Login(user, ip, ua);
return token switch
{
null => StatusCode(401),
"" => StatusCode(423),
_ => StatusCode(201, new { token })
};
}
///
/// 删除资源(注销)
///
/// 成功
[HttpDelete, Anonymous]
[ProducesResponseType(204)]
public async Task Logout(string token, string reason)
{
await _identityCommandService.Logout(token, reason);
return StatusCode(204);
}
}
}