network-resurrector/NetworkResurrector.Application/Queries/GetToken.cs

49 lines
1.4 KiB
C#
Raw Normal View History

2020-07-09 02:32:35 +03:00
using AutoMapper;
using MediatR;
using NDB.Application.DataContracts;
2020-07-10 00:29:39 +03:00
using NetworkResurrector.Application.Services;
2020-07-09 02:32:35 +03:00
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
{
2020-07-10 00:29:39 +03:00
public string Token { get; set; }
2020-07-09 02:32:35 +03:00
public DateTime ValidUntil { get; set; }
}
public class QueryHandler : IRequestHandler<Query, Model>
{
2020-07-10 00:29:39 +03:00
private readonly IUserService _userService;
2020-07-09 02:32:35 +03:00
private readonly IMapper _mapper;
2020-07-10 00:29:39 +03:00
public QueryHandler(IUserService userService, IMapper mapper)
2020-07-09 02:32:35 +03:00
{
2020-07-10 00:29:39 +03:00
_userService = userService;
2020-07-09 02:32:35 +03:00
_mapper = mapper;
}
public async Task<Model> Handle(Query request, CancellationToken cancellationToken)
{
2020-07-10 00:29:39 +03:00
var securityToken = await _userService.Login(request.UserName, request.Password);
if (securityToken == null)
return null;
var result = _mapper.Map<Model>(securityToken);
return result;
2020-07-09 02:32:35 +03:00
}
}
}
}