chatbot/Chatbot.Api.Application/Queries/GetSession.cs

52 lines
1.6 KiB
C#
Raw Normal View History

2020-06-06 19:21:40 +03:00
using AutoMapper;
using Chatbot.Api.Domain.Repositories;
using MediatR;
using NDB.Application.DataContracts;
2020-06-06 19:21:40 +03:00
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Chatbot.Api.Application.Queries
{
public class GetSession
{
public class Query : Query<Model>
{
public string BotName { get; set; }
public string ExternalId { get; set; }
public string ClientApplication { get; set; }
public string UserKey { get; set; }
public Query() { }
}
public class Model
{
public Guid SessionId { get; set; }
public DateTime StartDate { get; set; }
}
public class QueryHandler : IRequestHandler<Query, Model>
{
private readonly IBotRepository _botRepository;
private readonly ISessionRepository _sessionRepository;
private readonly IMapper _mapper;
public QueryHandler(IBotRepository botRepository, ISessionRepository sessionRepository, IMapper mapper)
{
_botRepository = botRepository;
_sessionRepository = sessionRepository;
_mapper = mapper;
}
public async Task<Model> Handle(Query request, CancellationToken cancellationToken)
{
var botId = await _botRepository.GetBotId(request.BotName);
var session = await _sessionRepository.CreateSession(botId, request.ClientApplication, request.ExternalId, request.UserKey);
var result = _mapper.Map<Model>(session);
return result;
}
}
}
}