close chat method
parent
5a649681e9
commit
b8840707db
|
@ -32,5 +32,12 @@ namespace Chatbot.Api.Controllers
|
||||||
var result = await _mediator.Send(saveChatMessage);
|
var result = await _mediator.Send(saveChatMessage);
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[HttpPost("close")]
|
||||||
|
public async Task<IActionResult> CloseChat([FromBody] CloseChat closeChat)
|
||||||
|
{
|
||||||
|
var result = await _mediator.Send(closeChat);
|
||||||
|
return Ok(result);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,14 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Chatbot.Api.Application.Events
|
||||||
|
{
|
||||||
|
public class ChatClosed
|
||||||
|
{
|
||||||
|
public Guid ChatId { get; }
|
||||||
|
|
||||||
|
public ChatClosed(Guid chatId)
|
||||||
|
{
|
||||||
|
ChatId = chatId;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,6 +1,7 @@
|
||||||
using Chatbot.Api.Domain.Data.DbContexts;
|
using Chatbot.Api.Domain.Data.DbContexts;
|
||||||
using Chatbot.Api.Domain.Entities;
|
using Chatbot.Api.Domain.Entities;
|
||||||
using Chatbot.Api.Domain.Repositories;
|
using Chatbot.Api.Domain.Repositories;
|
||||||
|
using Microsoft.EntityFrameworkCore;
|
||||||
using System;
|
using System;
|
||||||
using System.Threading.Tasks;
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
|
@ -35,5 +36,16 @@ namespace Chatbot.Api.Domain.Data.Repositories
|
||||||
_dbContext.Add(entity);
|
_dbContext.Add(entity);
|
||||||
await _dbContext.SaveChangesAsync();
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ begin
|
||||||
StartDate datetime constraint DF_Chat_StartDate default getdate(),
|
StartDate datetime constraint DF_Chat_StartDate default getdate(),
|
||||||
StopDate datetime,
|
StopDate datetime,
|
||||||
constraint PK_Chat primary key (ChatId),
|
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
|
end
|
||||||
go
|
go
|
||||||
|
|
|
@ -8,5 +8,6 @@ namespace Chatbot.Api.Domain.Repositories
|
||||||
{
|
{
|
||||||
Task<Chat> CreateChat(Guid sessionId);
|
Task<Chat> CreateChat(Guid sessionId);
|
||||||
Task Add<T>(T entity) where T : class;
|
Task Add<T>(T entity) where T : class;
|
||||||
|
Task CloseChat(Guid chatId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue