39 lines
941 B
C#
39 lines
941 B
C#
|
|
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;
|
||
|
|
}
|
||
|
|
}
|