add conversation pagination
This commit is contained in:
parent
f1278b3384
commit
622effa2dd
|
|
@ -43,7 +43,7 @@ public interface IBotSharpRepository
|
|||
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
|
||||
void UpdateConversationStatus(string conversationId, string status);
|
||||
Conversation GetConversation(string conversationId);
|
||||
List<Conversation> GetConversations(ConversationFilter filter);
|
||||
PagedItems<Conversation> GetConversations(ConversationFilter filter);
|
||||
void UpdateConversationTitle(string conversationId, string title);
|
||||
List<Conversation> GetLastConversations();
|
||||
#endregion
|
||||
|
|
|
|||
|
|
@ -2,12 +2,12 @@ namespace BotSharp.Abstraction.Utilities;
|
|||
|
||||
public class Pagination
|
||||
{
|
||||
public int Page { get; set; } = 0;
|
||||
public int Page { get; set; } = 1;
|
||||
/// <summary>
|
||||
/// Use -1 for all records
|
||||
/// </summary>
|
||||
public int Size { get; set; } = 20;
|
||||
public int Offset => (Page + 1) * Size;
|
||||
public int Offset => (Page - 1) * Size;
|
||||
}
|
||||
|
||||
public class PagedItems<T>
|
||||
|
|
|
|||
|
|
@ -57,14 +57,8 @@ public partial class ConversationService : IConversationService
|
|||
public async Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter)
|
||||
{
|
||||
var db = _services.GetRequiredService<IBotSharpRepository>();
|
||||
var user = db.GetUserById(_user.Id);
|
||||
var conversations = db.GetConversations(filter);
|
||||
var result = new PagedItems<Conversation>
|
||||
{
|
||||
Count = conversations.Count(),
|
||||
Items = conversations.OrderByDescending(x => x.CreatedTime)
|
||||
};
|
||||
return result;
|
||||
return conversations;
|
||||
}
|
||||
|
||||
public async Task<List<Conversation>> GetLastConversations()
|
||||
|
|
|
|||
|
|
@ -136,7 +136,7 @@ public class BotSharpDbContext : Database, IBotSharpRepository
|
|||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
public List<Conversation> GetConversations(ConversationFilter filter)
|
||||
public PagedItems<Conversation> GetConversations(ConversationFilter filter)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -200,12 +200,14 @@ namespace BotSharp.Core.Repository
|
|||
return record;
|
||||
}
|
||||
|
||||
public List<Conversation> GetConversations(ConversationFilter filter)
|
||||
public PagedItems<Conversation> GetConversations(ConversationFilter filter)
|
||||
{
|
||||
var records = new List<Conversation>();
|
||||
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
|
||||
var totalDirs = Directory.GetDirectories(dir);
|
||||
var dirs = totalDirs.Skip(filter.Pager.Offset).Take(filter.Pager.Size).ToList();
|
||||
|
||||
foreach (var d in Directory.GetDirectories(dir))
|
||||
foreach (var d in dirs)
|
||||
{
|
||||
var path = Path.Combine(d, CONVERSATION_FILE);
|
||||
if (!File.Exists(path)) continue;
|
||||
|
|
@ -225,7 +227,11 @@ namespace BotSharp.Core.Repository
|
|||
records.Add(record);
|
||||
}
|
||||
|
||||
return records;
|
||||
return new PagedItems<Conversation>
|
||||
{
|
||||
Items = records.OrderByDescending(x => x.CreatedTime),
|
||||
Count = totalDirs.Count(),
|
||||
};
|
||||
}
|
||||
|
||||
public List<Conversation> GetLastConversations()
|
||||
|
|
|
|||
|
|
@ -30,8 +30,8 @@ public class ConversationController : ControllerBase
|
|||
return ConversationViewModel.FromSession(conv);
|
||||
}
|
||||
|
||||
[HttpGet("/conversations")]
|
||||
public async Task<PagedItems<ConversationViewModel>> GetConversations([FromQuery] ConversationFilter filter)
|
||||
[HttpPost("/conversations")]
|
||||
public async Task<PagedItems<ConversationViewModel>> GetConversations([FromBody] ConversationFilter filter)
|
||||
{
|
||||
var service = _services.GetRequiredService<IConversationService>();
|
||||
var conversations = await service.GetConversations(filter);
|
||||
|
|
|
|||
|
|
@ -204,7 +204,7 @@ public partial class MongoRepository
|
|||
};
|
||||
}
|
||||
|
||||
public List<Conversation> GetConversations(ConversationFilter filter)
|
||||
public PagedItems<Conversation> GetConversations(ConversationFilter filter)
|
||||
{
|
||||
var conversations = new List<Conversation>();
|
||||
var builder = Builders<ConversationDocument>.Filter;
|
||||
|
|
@ -215,7 +215,10 @@ public partial class MongoRepository
|
|||
if (!string.IsNullOrEmpty(filter.Channel)) filters.Add(builder.Eq(x => x.Channel, filter.Channel));
|
||||
if (!string.IsNullOrEmpty(filter.UserId)) filters.Add(builder.Eq(x => x.UserId, filter.UserId));
|
||||
|
||||
var conversationDocs = _dc.Conversations.Find(builder.And(filters)).ToList();
|
||||
var filterDef = builder.And(filters);
|
||||
var sortDefinition = Builders<ConversationDocument>.Sort.Descending(x => x.CreatedTime);
|
||||
var conversationDocs = _dc.Conversations.Find(filterDef).Sort(sortDefinition).Skip(filter.Pager.Offset).Limit(filter.Pager.Size).ToList();
|
||||
var count = _dc.Conversations.CountDocuments(filterDef);
|
||||
|
||||
foreach (var conv in conversationDocs)
|
||||
{
|
||||
|
|
@ -233,7 +236,11 @@ public partial class MongoRepository
|
|||
});
|
||||
}
|
||||
|
||||
return conversations;
|
||||
return new PagedItems<Conversation>
|
||||
{
|
||||
Items = conversations,
|
||||
Count = (int)count
|
||||
};
|
||||
}
|
||||
|
||||
public List<Conversation> GetLastConversations()
|
||||
|
|
|
|||
Loading…
Reference in a new issue