diff --git a/Chatbot.Api/Controllers/SystemController.cs b/Chatbot.Api/Controllers/SystemController.cs index 2dc2e4b..55bf1a2 100644 --- a/Chatbot.Api/Controllers/SystemController.cs +++ b/Chatbot.Api/Controllers/SystemController.cs @@ -26,7 +26,21 @@ namespace Chatbot.Api.Controllers } [HttpGet("bots")] - public async Task GetReleaseNotes([FromRoute] GetBots.Query query) + public async Task GetBots([FromRoute] GetBots.Query query) + { + var result = await _mediator.Send(query); + return Ok(result); + } + + [HttpGet("initialize-session")] + public async Task GetSession([FromRoute] GetSession.Query query) + { + var result = await _mediator.Send(query); + return Ok(result); + } + + [HttpGet("initialize-chat")] + public async Task GetChat([FromRoute] GetChat.Query query) { var result = await _mediator.Send(query); return Ok(result); diff --git a/Chatbot.Application/Mappings/MappingProfile.cs b/Chatbot.Application/Mappings/MappingProfile.cs index 8de78c9..fa9ef3b 100644 --- a/Chatbot.Application/Mappings/MappingProfile.cs +++ b/Chatbot.Application/Mappings/MappingProfile.cs @@ -9,6 +9,7 @@ namespace Chatbot.Api.Application.Mappings public MappingProfile() { CreateMap(); + CreateMap(); } } } \ No newline at end of file diff --git a/Chatbot.Application/Queries/GetSession.cs b/Chatbot.Application/Queries/GetSession.cs new file mode 100644 index 0000000..976aada --- /dev/null +++ b/Chatbot.Application/Queries/GetSession.cs @@ -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 + { + 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 + { + 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 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(session); + + return result; + } + } + } +} diff --git a/Chatbot.Domain.Data/DbContexts/SessionDbContext.cs b/Chatbot.Domain.Data/DbContexts/SessionDbContext.cs new file mode 100644 index 0000000..7f9c68f --- /dev/null +++ b/Chatbot.Domain.Data/DbContexts/SessionDbContext.cs @@ -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 BotSessions { get; set; } + + public SessionDbContext(DbContextOptions 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()); + } + } +} diff --git a/Chatbot.Domain.Data/DependencyInjectionExtensions.cs b/Chatbot.Domain.Data/DependencyInjectionExtensions.cs index 3d8f14e..a93b8e3 100644 --- a/Chatbot.Domain.Data/DependencyInjectionExtensions.cs +++ b/Chatbot.Domain.Data/DependencyInjectionExtensions.cs @@ -12,6 +12,7 @@ namespace Chatbot.Api.Domain.Data public static void AddDataAccess(this IServiceCollection services) { services.AddScoped(); + services.AddScoped(); services .AddDbContextPool( @@ -21,6 +22,15 @@ namespace Chatbot.Api.Domain.Data var connectionString = configuration.GetConnectionString("DatabaseConnection"); options.UseSqlServer(connectionString); }); + + services + .AddDbContextPool( + (serviceProvider, options) => + { + var configuration = serviceProvider.GetService(); + var connectionString = configuration.GetConnectionString("DatabaseConnection"); + options.UseSqlServer(connectionString); + }); } } } diff --git a/Chatbot.Domain.Data/EntityTypeConfiguration/BotSessionConfiguration.cs b/Chatbot.Domain.Data/EntityTypeConfiguration/BotSessionConfiguration.cs new file mode 100644 index 0000000..f1bfe7a --- /dev/null +++ b/Chatbot.Domain.Data/EntityTypeConfiguration/BotSessionConfiguration.cs @@ -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 + { + public void Configure(EntityTypeBuilder builder) + { + builder.ToTable("BotSession").HasKey(key => key.SessionId); + } + } +} diff --git a/Chatbot.Domain.Data/Repositories/BotRepository.cs b/Chatbot.Domain.Data/Repositories/BotRepository.cs index 6fe9333..79b4c4b 100644 --- a/Chatbot.Domain.Data/Repositories/BotRepository.cs +++ b/Chatbot.Domain.Data/Repositories/BotRepository.cs @@ -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 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 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; + } } } diff --git a/Chatbot.Domain.Data/Repositories/SessionRepository.cs b/Chatbot.Domain.Data/Repositories/SessionRepository.cs new file mode 100644 index 0000000..ac80382 --- /dev/null +++ b/Chatbot.Domain.Data/Repositories/SessionRepository.cs @@ -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 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; + } + } +} diff --git a/Chatbot.Domain.Data/Scripts/03.BotSession ExternalId update.sql b/Chatbot.Domain.Data/Scripts/03.BotSession ExternalId update.sql new file mode 100644 index 0000000..b136c35 --- /dev/null +++ b/Chatbot.Domain.Data/Scripts/03.BotSession ExternalId update.sql @@ -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 \ No newline at end of file diff --git a/Chatbot.Domain/Entities/BotSession.cs b/Chatbot.Domain/Entities/BotSession.cs new file mode 100644 index 0000000..7bb2a3b --- /dev/null +++ b/Chatbot.Domain/Entities/BotSession.cs @@ -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; } + } +} diff --git a/Chatbot.Domain/Repositories/IBotRepository.cs b/Chatbot.Domain/Repositories/IBotRepository.cs index 8b22867..0a4d565 100644 --- a/Chatbot.Domain/Repositories/IBotRepository.cs +++ b/Chatbot.Domain/Repositories/IBotRepository.cs @@ -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 GetBots(); + Task GetBotId(string botName); } } diff --git a/Chatbot.Domain/Repositories/ISessionRepository.cs b/Chatbot.Domain/Repositories/ISessionRepository.cs new file mode 100644 index 0000000..4de0bdf --- /dev/null +++ b/Chatbot.Domain/Repositories/ISessionRepository.cs @@ -0,0 +1,11 @@ +using Chatbot.Api.Domain.Entities; +using System; +using System.Threading.Tasks; + +namespace Chatbot.Api.Domain.Repositories +{ + public interface ISessionRepository + { + Task CreateSession(Guid botId, string clientApplication, string externalId, string userKey); + } +}