close chat method

master
Tudor Stanciu 2020-06-07 02:38:52 +03:00
parent 5a649681e9
commit b8840707db
7 changed files with 76 additions and 1 deletions

View File

@ -32,5 +32,12 @@ namespace Chatbot.Api.Controllers
var result = await _mediator.Send(saveChatMessage);
return Ok(result);
}
[HttpPost("close")]
public async Task<IActionResult> CloseChat([FromBody] CloseChat closeChat)
{
var result = await _mediator.Send(closeChat);
return Ok(result);
}
}
}

View File

@ -0,0 +1,25 @@
using Chatbot.Api.Application.Commands;
using Chatbot.Api.Application.Events;
using Chatbot.Api.Domain.Repositories;
using MediatR;
using System.Threading;
using System.Threading.Tasks;
namespace Chatbot.Api.Application.CommandHandlers
{
public class CloseChatHandler : IRequestHandler<CloseChat, ChatClosed>
{
private readonly IChatRepository _chatRepository;
public CloseChatHandler(IChatRepository chatRepository)
{
_chatRepository = chatRepository;
}
public async Task<ChatClosed> Handle(CloseChat request, CancellationToken cancellationToken)
{
await _chatRepository.CloseChat(request.ChatId);
return new ChatClosed(request.ChatId);
}
}
}

View File

@ -0,0 +1,16 @@
using Chatbot.Api.Application.Events;
using System;
namespace Chatbot.Api.Application.Commands
{
public class CloseChat : Command<ChatClosed>
{
public Guid ChatId { get; }
public CloseChat(Guid chatId)
: base(new Metadata() { CorrelationId = Guid.NewGuid() })
{
ChatId = chatId;
}
}
}

View File

@ -0,0 +1,14 @@
using System;
namespace Chatbot.Api.Application.Events
{
public class ChatClosed
{
public Guid ChatId { get; }
public ChatClosed(Guid chatId)
{
ChatId = chatId;
}
}
}

View File

@ -1,6 +1,7 @@
using Chatbot.Api.Domain.Data.DbContexts;
using Chatbot.Api.Domain.Entities;
using Chatbot.Api.Domain.Repositories;
using Microsoft.EntityFrameworkCore;
using System;
using System.Threading.Tasks;
@ -35,5 +36,16 @@ namespace Chatbot.Api.Domain.Data.Repositories
_dbContext.Add(entity);
await _dbContext.SaveChangesAsync();
}
public async Task CloseChat(Guid chatId)
{
var chat = await _dbContext.Chats.FirstOrDefaultAsync(z => z.ChatId == chatId);
if (chat == null)
return;
chat.StopDate = DateTime.Now;
_dbContext.Update(chat);
await _dbContext.SaveChangesAsync();
}
}
}

View File

@ -36,7 +36,7 @@ begin
StartDate datetime constraint DF_Chat_StartDate default getdate(),
StopDate datetime,
constraint PK_Chat primary key (ChatId),
constraint FK_Chat_BotSession foreign key (ChatId) references BotSession(SessionId)
constraint FK_Chat_BotSession foreign key (SessionId) references BotSession(SessionId)
)
end
go

View File

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