52 lines
1.3 KiB
C#
52 lines
1.3 KiB
C#
|
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;
|
|||
|
|
|||
|
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;
|
|||
|
}
|
|||
|
|
|||
|
public async Task Add<T>(T entity) where T : class
|
|||
|
{
|
|||
|
_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();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|