AuthorBuddy/Services/ChatService.cs

39 lines
941 B
C#
Raw Normal View History

2026-06-21 07:52:03 +02:00
using AuthorBuddy.Web.Data;
using LiteDB;
namespace AuthorBuddy.Web.Services;
public class ChatService : IChatService
{
private readonly ILiteDatabase _db;
public ChatService(ILiteDatabase db)
{
_db = db;
}
public Task<List<ChatEntity>> GetHistoryAsync(int userId)
{
var col = _db.GetCollection<ChatEntity>("chats");
return Task.FromResult(col.Find(c => c.UserId == userId)
.OrderByDescending(c => c.Timestamp)
.Take(100)
.Reverse()
.ToList());
}
public Task AddMessageAsync(ChatEntity message)
{
var col = _db.GetCollection<ChatEntity>("chats");
col.Insert(message);
return Task.CompletedTask;
}
public Task ClearHistoryAsync(int userId)
{
var col = _db.GetCollection<ChatEntity>("chats");
col.DeleteMany(c => c.UserId == userId);
return Task.CompletedTask;
}
}