create session

master
Tudor Stanciu 2020-06-06 19:21:40 +03:00
parent f06a9fcaea
commit 4157845744
12 changed files with 207 additions and 1 deletions

View File

@ -26,7 +26,21 @@ namespace Chatbot.Api.Controllers
}
[HttpGet("bots")]
public async Task<IActionResult> GetReleaseNotes([FromRoute] GetBots.Query query)
public async Task<IActionResult> GetBots([FromRoute] GetBots.Query query)
{
var result = await _mediator.Send(query);
return Ok(result);
}
[HttpGet("initialize-session")]
public async Task<IActionResult> GetSession([FromRoute] GetSession.Query query)
{
var result = await _mediator.Send(query);
return Ok(result);
}
[HttpGet("initialize-chat")]
public async Task<IActionResult> GetChat([FromRoute] GetChat.Query query)
{
var result = await _mediator.Send(query);
return Ok(result);

View File

@ -9,6 +9,7 @@ namespace Chatbot.Api.Application.Mappings
public MappingProfile()
{
CreateMap<Bot, GetBots.Model>();
CreateMap<BotSession, GetSession.Model>();
}
}
}

View File

@ -0,0 +1,50 @@
using AutoMapper;
using Chatbot.Api.Domain.Repositories;
using MediatR;
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;
}
}
}
}

View File

@ -0,0 +1,25 @@
using Chatbot.Api.Domain.Data.EntityTypeConfiguration;
using Chatbot.Api.Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace Chatbot.Api.Domain.Data.DbContexts
{
public class SessionDbContext : DbContext
{
public DbSet<BotSession> BotSessions { get; set; }
public SessionDbContext(DbContextOptions<SessionDbContext> options)
: base(options)
{
base.ChangeTracker.QueryTrackingBehavior = QueryTrackingBehavior.TrackAll;
base.ChangeTracker.AutoDetectChangesEnabled = true;
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new BotSessionConfiguration());
}
}
}

View File

@ -12,6 +12,7 @@ namespace Chatbot.Api.Domain.Data
public static void AddDataAccess(this IServiceCollection services)
{
services.AddScoped<IBotRepository, BotRepository>();
services.AddScoped<ISessionRepository, SessionRepository>();
services
.AddDbContextPool<BotDbContext>(
@ -21,6 +22,15 @@ namespace Chatbot.Api.Domain.Data
var connectionString = configuration.GetConnectionString("DatabaseConnection");
options.UseSqlServer(connectionString);
});
services
.AddDbContextPool<SessionDbContext>(
(serviceProvider, options) =>
{
var configuration = serviceProvider.GetService<IConfiguration>();
var connectionString = configuration.GetConnectionString("DatabaseConnection");
options.UseSqlServer(connectionString);
});
}
}
}

View File

@ -0,0 +1,14 @@
using Chatbot.Api.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Chatbot.Api.Domain.Data.EntityTypeConfiguration
{
class BotSessionConfiguration : IEntityTypeConfiguration<BotSession>
{
public void Configure(EntityTypeBuilder<BotSession> builder)
{
builder.ToTable("BotSession").HasKey(key => key.SessionId);
}
}
}

View File

@ -2,6 +2,7 @@
using Chatbot.Api.Domain.Entities;
using Chatbot.Api.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks;
namespace Chatbot.Api.Domain.Data.Repositories
@ -19,5 +20,29 @@ namespace Chatbot.Api.Domain.Data.Repositories
{
return await _dbContext.Bots.ToArrayAsync();
}
public async Task<Guid> GetBotId(string botName)
{
var bot = await _dbContext.Bots.FirstOrDefaultAsync(z => z.BotName == botName);
if (bot != null)
return bot.BotId;
else
return await CreateBot(botName);
}
private async Task<Guid> CreateBot(string botName)
{
var id = Guid.NewGuid();
_dbContext.Add(new Bot()
{
BotId = id,
BotCode = botName.ToUpper(),
BotName = botName,
CreationDate = DateTime.Now
});
await _dbContext.SaveChangesAsync();
return id;
}
}
}

View File

@ -0,0 +1,36 @@
using Chatbot.Api.Domain.Data.DbContexts;
using Chatbot.Api.Domain.Entities;
using Chatbot.Api.Domain.Repositories;
using System;
using System.Threading.Tasks;
namespace Chatbot.Api.Domain.Data.Repositories
{
public class SessionRepository : ISessionRepository
{
private readonly SessionDbContext _dbContext;
public SessionRepository(SessionDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<BotSession> CreateSession(Guid botId, string clientApplication, string externalId, string userKey)
{
var session = new BotSession()
{
SessionId = Guid.NewGuid(),
StartDate = DateTime.Now,
BotId = botId,
ClientApplication = clientApplication,
ExternalId = externalId,
UserKey = userKey
};
_dbContext.Add(session);
await _dbContext.SaveChangesAsync();
return session;
}
}
}

View File

@ -0,0 +1,4 @@
if exists(select top 1 1 from sys.columns where name = 'ExternalId' and object_id = object_id('BotSession'))
begin
alter table BotSession alter column ExternalId varchar(100) not null
end

View File

@ -0,0 +1,14 @@
using System;
namespace Chatbot.Api.Domain.Entities
{
public class BotSession
{
public Guid SessionId { get; set; }
public DateTime StartDate { get; set; }
public Guid BotId { get; set; }
public string ExternalId { get; set; }
public string ClientApplication { get; set; }
public string UserKey { get; set; }
}
}

View File

@ -1,4 +1,5 @@
using Chatbot.Api.Domain.Entities;
using System;
using System.Threading.Tasks;
namespace Chatbot.Api.Domain.Repositories
@ -6,5 +7,6 @@ namespace Chatbot.Api.Domain.Repositories
public interface IBotRepository
{
Task<Bot[]> GetBots();
Task<Guid> GetBotId(string botName);
}
}

View File

@ -0,0 +1,11 @@
using Chatbot.Api.Domain.Entities;
using System;
using System.Threading.Tasks;
namespace Chatbot.Api.Domain.Repositories
{
public interface ISessionRepository
{
Task<BotSession> CreateSession(Guid botId, string clientApplication, string externalId, string userKey);
}
}