using AutoMapper;
using Kean.Application.Command.Interfaces;
using Kean.Application.Command.ViewModels;
using Kean.Domain;
using Kean.Domain.Identity.Commands;
using System.Linq;
namespace Kean.Application.Command.Implements
{
///
/// 身份命令服务实现
///
public class IdentityService : IIdentityService
{
private readonly ICommandBus _bus; // 命令总线
private readonly IMapper _mapper; // 模型映射
private readonly INotification _notifications; // 总线通知
///
/// 依赖注入
///
public IdentityService(
ICommandBus bus,
IMapper mapper,
INotification notifications)
{
_bus = bus;
_mapper = mapper;
_notifications = notifications;
}
/*
* 实现 Kean.Application.Command.Interfaces.IIdentityService.Login(User user) 方法
*/
public async System.Threading.Tasks.Task Login(User user, string remoteIp, string userAgent)
{
var command = _mapper.Map(user);
command.RemoteIp = remoteIp;
command.UserAgent = userAgent;
await _bus.Execute(command);
return command.Token;
}
/*
* 实现 Kean.Application.Command.Interfaces.IIdentityService.Connect(string token, string id) 方法
*/
public async System.Threading.Tasks.Task Connect(string token, string id)
{
await _bus.Execute(new ConnectCommand
{
Token = token,
Id = id
});
}
/*
* 实现 Kean.Application.Command.Interfaces.IIdentityService.Disconnect(string token, string id) 方法
*/
public async System.Threading.Tasks.Task Disconnect(string token, string id)
{
await _bus.Execute(new DisconnectCommand
{
Token = token,
Id = id
});
}
/*
* 实现 Kean.Application.Command.Interfaces.IIdentityService.Logout(string session, string reason) 方法
*/
public async System.Threading.Tasks.Task Logout(string token, string reason)
{
await _bus.Execute(new LogoutCommand
{
Token = token,
Reason = reason
});
}
/*
* 实现 Kean.Application.Command.Interfaces.IIdentityService.Authenticate(string token) 方法
*/
public async System.Threading.Tasks.Task Authenticate(string token)
{
var command = new AuthenticateCommand { Token = token };
await _bus.Execute(command);
return command.Identity;
}
/*
* 实现 Kean.Application.Command.Interfaces.IIdentityService.Navigate(string token, string url) 方法
*/
public async System.Threading.Tasks.Task<(bool Success, Failure Failure)> Navigate(string token, string url, params string[] ignore)
{
await _bus.Execute(new NavigateCommand
{
Token = token,
Url = url,
Ignore = ignore
});
var failure = _notifications.FirstOrDefault();
return (failure == null, failure);
}
/*
* 实现 Kean.Application.Command.Interfaces.IIdentityService.InitializePassword(Password password) 方法
*/
public async System.Threading.Tasks.Task<(bool Success, Failure Failure)> InitializePassword(Password password)
{
await _bus.Execute(_mapper.Map(password));
var failure = _notifications.FirstOrDefault();
return (failure == null, failure);
}
/*
* 实现 Kean.Application.Command.Interfaces.IIdentityService.ModifyPassword(Password password) 方法
*/
public async System.Threading.Tasks.Task<(bool Success, Failure Failure)> ModifyPassword(Password password)
{
await _bus.Execute(_mapper.Map(password));
var failure = _notifications.FirstOrDefault();
return (failure == null, failure);
}
/*
* 实现 Kean.Application.Command.Interfaces.IIdentityService.ModifyAvatar(User user) 方法
*/
public async System.Threading.Tasks.Task<(bool Success, Failure Failure)> ModifyAvatar(User user)
{
await _bus.Execute(_mapper.Map(user));
var failure = _notifications.FirstOrDefault();
return (failure == null, failure);
}
/*
* 实现 Kean.Application.Command.Interfaces.IIdentityService.Finalize() 方法
*/
public async System.Threading.Tasks.Task Finalize()
{
await _bus.Execute(new FinalizeCommand());
}
}
}