Skip to content

Commit 8c69c6f

Browse files
authored
Merge pull request #257 from iceljc/features/add-conversation-paging
add conversation pagination
2 parents f1278b3 + 622effa commit 8c69c6f

File tree

7 files changed

+26
-19
lines changed

7 files changed

+26
-19
lines changed

src/Infrastructure/BotSharp.Abstraction/Repositories/IBotSharpRepository.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public interface IBotSharpRepository
4343
void UpdateConversationStates(string conversationId, List<StateKeyValue> states);
4444
void UpdateConversationStatus(string conversationId, string status);
4545
Conversation GetConversation(string conversationId);
46-
List<Conversation> GetConversations(ConversationFilter filter);
46+
PagedItems<Conversation> GetConversations(ConversationFilter filter);
4747
void UpdateConversationTitle(string conversationId, string title);
4848
List<Conversation> GetLastConversations();
4949
#endregion

src/Infrastructure/BotSharp.Abstraction/Utilities/Pagination.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ namespace BotSharp.Abstraction.Utilities;
22

33
public class Pagination
44
{
5-
public int Page { get; set; } = 0;
5+
public int Page { get; set; } = 1;
66
/// <summary>
77
/// Use -1 for all records
88
/// </summary>
99
public int Size { get; set; } = 20;
10-
public int Offset => (Page + 1) * Size;
10+
public int Offset => (Page - 1) * Size;
1111
}
1212

1313
public class PagedItems<T>

src/Infrastructure/BotSharp.Core/Conversations/Services/ConversationService.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,8 @@ public async Task<Conversation> GetConversation(string id)
5757
public async Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter)
5858
{
5959
var db = _services.GetRequiredService<IBotSharpRepository>();
60-
var user = db.GetUserById(_user.Id);
6160
var conversations = db.GetConversations(filter);
62-
var result = new PagedItems<Conversation>
63-
{
64-
Count = conversations.Count(),
65-
Items = conversations.OrderByDescending(x => x.CreatedTime)
66-
};
67-
return result;
61+
return conversations;
6862
}
6963

7064
public async Task<List<Conversation>> GetLastConversations()

src/Infrastructure/BotSharp.Core/Repository/BotSharpDbContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public Conversation GetConversation(string conversationId)
136136
throw new NotImplementedException();
137137
}
138138

139-
public List<Conversation> GetConversations(ConversationFilter filter)
139+
public PagedItems<Conversation> GetConversations(ConversationFilter filter)
140140
{
141141
throw new NotImplementedException();
142142
}

src/Infrastructure/BotSharp.Core/Repository/FileRepository/FileRepository.Conversation.cs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,14 @@ public Conversation GetConversation(string conversationId)
200200
return record;
201201
}
202202

203-
public List<Conversation> GetConversations(ConversationFilter filter)
203+
public PagedItems<Conversation> GetConversations(ConversationFilter filter)
204204
{
205205
var records = new List<Conversation>();
206206
var dir = Path.Combine(_dbSettings.FileRepository, _conversationSettings.DataDir);
207+
var totalDirs = Directory.GetDirectories(dir);
208+
var dirs = totalDirs.Skip(filter.Pager.Offset).Take(filter.Pager.Size).ToList();
207209

208-
foreach (var d in Directory.GetDirectories(dir))
210+
foreach (var d in dirs)
209211
{
210212
var path = Path.Combine(d, CONVERSATION_FILE);
211213
if (!File.Exists(path)) continue;
@@ -225,7 +227,11 @@ public List<Conversation> GetConversations(ConversationFilter filter)
225227
records.Add(record);
226228
}
227229

228-
return records;
230+
return new PagedItems<Conversation>
231+
{
232+
Items = records.OrderByDescending(x => x.CreatedTime),
233+
Count = totalDirs.Count(),
234+
};
229235
}
230236

231237
public List<Conversation> GetLastConversations()

src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ public async Task<ConversationViewModel> NewConversation([FromRoute] string agen
3030
return ConversationViewModel.FromSession(conv);
3131
}
3232

33-
[HttpGet("/conversations")]
34-
public async Task<PagedItems<ConversationViewModel>> GetConversations([FromQuery] ConversationFilter filter)
33+
[HttpPost("/conversations")]
34+
public async Task<PagedItems<ConversationViewModel>> GetConversations([FromBody] ConversationFilter filter)
3535
{
3636
var service = _services.GetRequiredService<IConversationService>();
3737
var conversations = await service.GetConversations(filter);

src/Plugins/BotSharp.Plugin.MongoStorage/Repository/MongoRepository.Conversation.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ public Conversation GetConversation(string conversationId)
204204
};
205205
}
206206

207-
public List<Conversation> GetConversations(ConversationFilter filter)
207+
public PagedItems<Conversation> GetConversations(ConversationFilter filter)
208208
{
209209
var conversations = new List<Conversation>();
210210
var builder = Builders<ConversationDocument>.Filter;
@@ -215,7 +215,10 @@ public List<Conversation> GetConversations(ConversationFilter filter)
215215
if (!string.IsNullOrEmpty(filter.Channel)) filters.Add(builder.Eq(x => x.Channel, filter.Channel));
216216
if (!string.IsNullOrEmpty(filter.UserId)) filters.Add(builder.Eq(x => x.UserId, filter.UserId));
217217

218-
var conversationDocs = _dc.Conversations.Find(builder.And(filters)).ToList();
218+
var filterDef = builder.And(filters);
219+
var sortDefinition = Builders<ConversationDocument>.Sort.Descending(x => x.CreatedTime);
220+
var conversationDocs = _dc.Conversations.Find(filterDef).Sort(sortDefinition).Skip(filter.Pager.Offset).Limit(filter.Pager.Size).ToList();
221+
var count = _dc.Conversations.CountDocuments(filterDef);
219222

220223
foreach (var conv in conversationDocs)
221224
{
@@ -233,7 +236,11 @@ public List<Conversation> GetConversations(ConversationFilter filter)
233236
});
234237
}
235238

236-
return conversations;
239+
return new PagedItems<Conversation>
240+
{
241+
Items = conversations,
242+
Count = (int)count
243+
};
237244
}
238245

239246
public List<Conversation> GetLastConversations()

0 commit comments

Comments
 (0)