File tree Expand file tree Collapse file tree 7 files changed +26
-19
lines changed
BotSharp.OpenAPI/Controllers
Plugins/BotSharp.Plugin.MongoStorage/Repository Expand file tree Collapse file tree 7 files changed +26
-19
lines changed Original file line number Diff line number Diff line change @@ -43,7 +43,7 @@ public interface IBotSharpRepository
43
43
void UpdateConversationStates ( string conversationId , List < StateKeyValue > states ) ;
44
44
void UpdateConversationStatus ( string conversationId , string status ) ;
45
45
Conversation GetConversation ( string conversationId ) ;
46
- List < Conversation > GetConversations ( ConversationFilter filter ) ;
46
+ PagedItems < Conversation > GetConversations ( ConversationFilter filter ) ;
47
47
void UpdateConversationTitle ( string conversationId , string title ) ;
48
48
List < Conversation > GetLastConversations ( ) ;
49
49
#endregion
Original file line number Diff line number Diff line change @@ -2,12 +2,12 @@ namespace BotSharp.Abstraction.Utilities;
2
2
3
3
public class Pagination
4
4
{
5
- public int Page { get ; set ; } = 0 ;
5
+ public int Page { get ; set ; } = 1 ;
6
6
/// <summary>
7
7
/// Use -1 for all records
8
8
/// </summary>
9
9
public int Size { get ; set ; } = 20 ;
10
- public int Offset => ( Page + 1 ) * Size ;
10
+ public int Offset => ( Page - 1 ) * Size ;
11
11
}
12
12
13
13
public class PagedItems < T >
Original file line number Diff line number Diff line change @@ -57,14 +57,8 @@ public async Task<Conversation> GetConversation(string id)
57
57
public async Task < PagedItems < Conversation > > GetConversations ( ConversationFilter filter )
58
58
{
59
59
var db = _services . GetRequiredService < IBotSharpRepository > ( ) ;
60
- var user = db . GetUserById ( _user . Id ) ;
61
60
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 ;
68
62
}
69
63
70
64
public async Task < List < Conversation > > GetLastConversations ( )
Original file line number Diff line number Diff line change @@ -136,7 +136,7 @@ public Conversation GetConversation(string conversationId)
136
136
throw new NotImplementedException ( ) ;
137
137
}
138
138
139
- public List < Conversation > GetConversations ( ConversationFilter filter )
139
+ public PagedItems < Conversation > GetConversations ( ConversationFilter filter )
140
140
{
141
141
throw new NotImplementedException ( ) ;
142
142
}
Original file line number Diff line number Diff line change @@ -200,12 +200,14 @@ public Conversation GetConversation(string conversationId)
200
200
return record ;
201
201
}
202
202
203
- public List < Conversation > GetConversations ( ConversationFilter filter )
203
+ public PagedItems < Conversation > GetConversations ( ConversationFilter filter )
204
204
{
205
205
var records = new List < Conversation > ( ) ;
206
206
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 ( ) ;
207
209
208
- foreach ( var d in Directory . GetDirectories ( dir ) )
210
+ foreach ( var d in dirs )
209
211
{
210
212
var path = Path . Combine ( d , CONVERSATION_FILE ) ;
211
213
if ( ! File . Exists ( path ) ) continue ;
@@ -225,7 +227,11 @@ public List<Conversation> GetConversations(ConversationFilter filter)
225
227
records . Add ( record ) ;
226
228
}
227
229
228
- return records ;
230
+ return new PagedItems < Conversation >
231
+ {
232
+ Items = records . OrderByDescending ( x => x . CreatedTime ) ,
233
+ Count = totalDirs . Count ( ) ,
234
+ } ;
229
235
}
230
236
231
237
public List < Conversation > GetLastConversations ( )
Original file line number Diff line number Diff line change @@ -30,8 +30,8 @@ public async Task<ConversationViewModel> NewConversation([FromRoute] string agen
30
30
return ConversationViewModel . FromSession ( conv ) ;
31
31
}
32
32
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 )
35
35
{
36
36
var service = _services . GetRequiredService < IConversationService > ( ) ;
37
37
var conversations = await service . GetConversations ( filter ) ;
Original file line number Diff line number Diff line change @@ -204,7 +204,7 @@ public Conversation GetConversation(string conversationId)
204
204
} ;
205
205
}
206
206
207
- public List < Conversation > GetConversations ( ConversationFilter filter )
207
+ public PagedItems < Conversation > GetConversations ( ConversationFilter filter )
208
208
{
209
209
var conversations = new List < Conversation > ( ) ;
210
210
var builder = Builders < ConversationDocument > . Filter ;
@@ -215,7 +215,10 @@ public List<Conversation> GetConversations(ConversationFilter filter)
215
215
if ( ! string . IsNullOrEmpty ( filter . Channel ) ) filters . Add ( builder . Eq ( x => x . Channel , filter . Channel ) ) ;
216
216
if ( ! string . IsNullOrEmpty ( filter . UserId ) ) filters . Add ( builder . Eq ( x => x . UserId , filter . UserId ) ) ;
217
217
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 ) ;
219
222
220
223
foreach ( var conv in conversationDocs )
221
224
{
@@ -233,7 +236,11 @@ public List<Conversation> GetConversations(ConversationFilter filter)
233
236
} ) ;
234
237
}
235
238
236
- return conversations ;
239
+ return new PagedItems < Conversation >
240
+ {
241
+ Items = conversations ,
242
+ Count = ( int ) count
243
+ } ;
237
244
}
238
245
239
246
public List < Conversation > GetLastConversations ( )
You can’t perform that action at this time.
0 commit comments