SaveChatMessageHandler

master
Tudor Stanciu 2020-06-06 23:44:49 +03:00
parent 50e9c3b311
commit 235b6b52cc
8 changed files with 116 additions and 0 deletions

View File

@ -0,0 +1,44 @@
using AutoMapper;
using Chatbot.Api.Application.Commands;
using Chatbot.Api.Application.Events;
using Chatbot.Api.Domain.Entities;
using Chatbot.Api.Domain.Repositories;
using MediatR;
using Microsoft.Extensions.Logging;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Chatbot.Api.Application.CommandHandlers
{
public class SaveChatMessageHandler : IRequestHandler<SaveChatMessage, ChatMessageSaved>
{
private readonly ILogger<SaveChatMessageHandler> _logger;
private readonly IMapper _mapper;
private readonly IChatRepository _chatRepository;
public SaveChatMessageHandler(ILogger<SaveChatMessageHandler> logger, IMapper mapper, IChatRepository chatRepository)
{
_logger = logger;
_mapper = mapper;
_chatRepository = chatRepository;
}
public async Task<ChatMessageSaved> Handle(SaveChatMessage request, CancellationToken cancellationToken)
{
try
{
var message = _mapper.Map<ChatMessage>(request);
await _chatRepository.Add(message);
return new ChatMessageSaved(true, null);
}
catch (Exception ex)
{
var correlationIdMsg = $"CorrelationId: {request.Metadata.CorrelationId}";
_logger.LogError(ex, $"An unexpected error has occurred. {correlationIdMsg}");
return new ChatMessageSaved(false, $"{ex.Message} {correlationIdMsg}");
}
}
}
}

View File

@ -0,0 +1,22 @@
using Chatbot.Api.Application.Events;
using System;
namespace Chatbot.Api.Application.Commands
{
public class SaveChatMessage : Command<ChatMessageSaved>
{
public Guid ChatId { get; }
public int MessageSourceId { get; }
public DateTime MessageDate { get; }
public string MessageContent { get; }
public SaveChatMessage(Guid chatId, int messageSourceId, DateTime messageDate, string messageContent)
: base(new Metadata() { CorrelationId = Guid.NewGuid() })
{
ChatId = chatId;
MessageSourceId = messageSourceId;
MessageDate = messageDate;
MessageContent = messageContent;
}
}
}

View File

@ -0,0 +1,14 @@
namespace Chatbot.Api.Application.Events
{
public class ChatMessageSaved
{
public bool Success { get; }
public string Message { get; }
public ChatMessageSaved(bool success, string message)
{
Success = success;
Message = message;
}
}
}

View File

@ -20,6 +20,7 @@ namespace Chatbot.Api.Domain.Data.DbContexts
base.OnModelCreating(modelBuilder);
modelBuilder.ApplyConfiguration(new ChatConfiguration());
modelBuilder.ApplyConfiguration(new ChatMessageConfiguration());
}
}
}

View File

@ -0,0 +1,15 @@
using Chatbot.Api.Domain.Entities;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
namespace Chatbot.Api.Domain.Data.EntityTypeConfiguration
{
class ChatMessageConfiguration : IEntityTypeConfiguration<ChatMessage>
{
public void Configure(EntityTypeBuilder<ChatMessage> builder)
{
builder.ToTable("ChatMessage").HasKey(key => key.MessageId);
builder.Property(e => e.MessageId).ValueGeneratedOnAdd();
}
}
}

View File

@ -29,5 +29,11 @@ namespace Chatbot.Api.Domain.Data.Repositories
return chat;
}
public async Task Add<T>(T entity) where T : class
{
_dbContext.Add(entity);
await _dbContext.SaveChangesAsync();
}
}
}

View File

@ -0,0 +1,13 @@
using System;
namespace Chatbot.Api.Domain.Entities
{
public class ChatMessage
{
public int MessageId { get; set; }
public Guid ChatId { get; set; }
public int MessageSourceId { get; set; }
public DateTime MessageDate { get; set; }
public string MessageContent { get; set; }
}
}

View File

@ -7,5 +7,6 @@ namespace Chatbot.Api.Domain.Repositories
public interface IChatRepository
{
Task<Chat> CreateChat(Guid sessionId);
Task Add<T>(T entity) where T : class;
}
}