create chat

master
Tudor Stanciu 2020-06-06 23:18:41 +03:00
parent 4157845744
commit 50e9c3b311
8 changed files with 134 additions and 1 deletions

View File

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

View File

@ -0,0 +1,43 @@
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 GetChat
{
public class Query : Query<Model>
{
public Guid SessionId { get; set; }
public Query() { }
}
public class Model
{
public Guid ChatId { get; set; }
}
public class QueryHandler : IRequestHandler<Query, Model>
{
private readonly IChatRepository _chatRepository;
private readonly IMapper _mapper;
public QueryHandler(IChatRepository chatRepository, IMapper mapper)
{
_chatRepository = chatRepository;
_mapper = mapper;
}
public async Task<Model> Handle(Query request, CancellationToken cancellationToken)
{
var chat = await _chatRepository.CreateChat(request.SessionId);
var result = _mapper.Map<Model>(chat);
return result;
}
}
}
}

View File

@ -1,9 +1,13 @@
using Microsoft.EntityFrameworkCore; using Chatbot.Api.Domain.Data.EntityTypeConfiguration;
using Chatbot.Api.Domain.Entities;
using Microsoft.EntityFrameworkCore;
namespace Chatbot.Api.Domain.Data.DbContexts namespace Chatbot.Api.Domain.Data.DbContexts
{ {
public class ChatDbContext : DbContext public class ChatDbContext : DbContext
{ {
public DbSet<Chat> Chats { get; set; }
public ChatDbContext(DbContextOptions<ChatDbContext> options) public ChatDbContext(DbContextOptions<ChatDbContext> options)
: base(options) : base(options)
{ {
@ -11,6 +15,11 @@ namespace Chatbot.Api.Domain.Data.DbContexts
base.ChangeTracker.AutoDetectChangesEnabled = true; base.ChangeTracker.AutoDetectChangesEnabled = true;
} }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new ChatConfiguration());
}
} }
} }

View File

@ -13,6 +13,7 @@ namespace Chatbot.Api.Domain.Data
{ {
services.AddScoped<IBotRepository, BotRepository>(); services.AddScoped<IBotRepository, BotRepository>();
services.AddScoped<ISessionRepository, SessionRepository>(); services.AddScoped<ISessionRepository, SessionRepository>();
services.AddScoped<IChatRepository, ChatRepository>();
services services
.AddDbContextPool<BotDbContext>( .AddDbContextPool<BotDbContext>(
@ -31,6 +32,15 @@ namespace Chatbot.Api.Domain.Data
var connectionString = configuration.GetConnectionString("DatabaseConnection"); var connectionString = configuration.GetConnectionString("DatabaseConnection");
options.UseSqlServer(connectionString); options.UseSqlServer(connectionString);
}); });
services
.AddDbContextPool<ChatDbContext>(
(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 ChatConfiguration : IEntityTypeConfiguration<Chat>
{
public void Configure(EntityTypeBuilder<Chat> builder)
{
builder.ToTable("Chat").HasKey(key => key.ChatId);
}
}
}

View File

@ -0,0 +1,33 @@
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
{
class ChatRepository : IChatRepository
{
private readonly ChatDbContext _dbContext;
public ChatRepository(ChatDbContext dbContext)
{
_dbContext = dbContext;
}
public async Task<Chat> CreateChat(Guid sessionId)
{
var chat = new Chat()
{
ChatId = Guid.NewGuid(),
SessionId = sessionId,
StartDate = DateTime.Now
};
_dbContext.Add(chat);
await _dbContext.SaveChangesAsync();
return chat;
}
}
}

View File

@ -0,0 +1,12 @@
using System;
namespace Chatbot.Api.Domain.Entities
{
public class Chat
{
public Guid ChatId { get; set; }
public Guid SessionId { get; set; }
public DateTime StartDate { get; set; }
public DateTime? StopDate { get; set; }
}
}

View File

@ -0,0 +1,11 @@
using Chatbot.Api.Domain.Entities;
using System;
using System.Threading.Tasks;
namespace Chatbot.Api.Domain.Repositories
{
public interface IChatRepository
{
Task<Chat> CreateChat(Guid sessionId);
}
}