49 lines
1.4 KiB
C#
49 lines
1.4 KiB
C#
using AutoMapper;
|
|
using MediatR;
|
|
using NDB.Application.DataContracts;
|
|
using NetworkResurrector.Application.Services;
|
|
using System;
|
|
using System.Threading;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace NetworkResurrector.Application.Queries
|
|
{
|
|
public class GetToken
|
|
{
|
|
public class Query : Query<Model>
|
|
{
|
|
public string UserName { get; set; }
|
|
public string Password { get; set; }
|
|
public Query() { }
|
|
}
|
|
|
|
public class Model
|
|
{
|
|
public string Token { get; set; }
|
|
public DateTime ValidUntil { get; set; }
|
|
}
|
|
|
|
public class QueryHandler : IRequestHandler<Query, Model>
|
|
{
|
|
private readonly IUserService _userService;
|
|
private readonly IMapper _mapper;
|
|
|
|
public QueryHandler(IUserService userService, IMapper mapper)
|
|
{
|
|
_userService = userService;
|
|
_mapper = mapper;
|
|
}
|
|
|
|
public async Task<Model> Handle(Query request, CancellationToken cancellationToken)
|
|
{
|
|
var securityToken = await _userService.Login(request.UserName, request.Password);
|
|
if (securityToken == null)
|
|
return null;
|
|
|
|
var result = _mapper.Map<Model>(securityToken);
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
}
|