create chat
parent
4157845744
commit
50e9c3b311
|
@ -10,6 +10,7 @@ namespace Chatbot.Api.Application.Mappings
|
|||
{
|
||||
CreateMap<Bot, GetBots.Model>();
|
||||
CreateMap<BotSession, GetSession.Model>();
|
||||
CreateMap<Chat, GetChat.Model>();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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
|
||||
{
|
||||
public class ChatDbContext : DbContext
|
||||
{
|
||||
public DbSet<Chat> Chats { get; set; }
|
||||
|
||||
public ChatDbContext(DbContextOptions<ChatDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
|
@ -11,6 +15,11 @@ namespace Chatbot.Api.Domain.Data.DbContexts
|
|||
base.ChangeTracker.AutoDetectChangesEnabled = true;
|
||||
}
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
base.OnModelCreating(modelBuilder);
|
||||
|
||||
modelBuilder.ApplyConfiguration(new ChatConfiguration());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,7 @@ namespace Chatbot.Api.Domain.Data
|
|||
{
|
||||
services.AddScoped<IBotRepository, BotRepository>();
|
||||
services.AddScoped<ISessionRepository, SessionRepository>();
|
||||
services.AddScoped<IChatRepository, ChatRepository>();
|
||||
|
||||
services
|
||||
.AddDbContextPool<BotDbContext>(
|
||||
|
@ -31,6 +32,15 @@ namespace Chatbot.Api.Domain.Data
|
|||
var connectionString = configuration.GetConnectionString("DatabaseConnection");
|
||||
options.UseSqlServer(connectionString);
|
||||
});
|
||||
|
||||
services
|
||||
.AddDbContextPool<ChatDbContext>(
|
||||
(serviceProvider, options) =>
|
||||
{
|
||||
var configuration = serviceProvider.GetService<IConfiguration>();
|
||||
var connectionString = configuration.GetConnectionString("DatabaseConnection");
|
||||
options.UseSqlServer(connectionString);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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; }
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue