Pagination

This commit is contained in:
Haiping Chen 2023-12-04 17:42:46 -06:00
parent 447e1a04bd
commit 78d66eb9c5
14 changed files with 71 additions and 47 deletions

View file

@ -1,3 +1,5 @@
using BotSharp.Abstraction.Repositories.Filters;
namespace BotSharp.Abstraction.Conversations;
public interface IConversationService
@ -7,7 +9,7 @@ public interface IConversationService
Task<Conversation> NewConversation(Conversation conversation);
void SetConversationId(string conversationId, List<string> states);
Task<Conversation> GetConversation(string id);
Task<List<Conversation>> GetConversations();
Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter);
Task<Conversation> UpdateConversationTitle(string id, string title);
Task<List<Conversation>> GetLastConversations();
Task<bool> DeleteConversation(string id);

View file

@ -2,6 +2,7 @@ namespace BotSharp.Abstraction.Repositories.Filters;
public class ConversationFilter
{
public Pagination Pager { get; set; } = new Pagination();
public string? AgentId { get; set; }
public string? Status { get; set; }
public string? Channel { get; set; }

View file

@ -6,6 +6,7 @@ global using System.Threading.Tasks;
global using System.ComponentModel.DataAnnotations;
global using System.Text.Json.Serialization;
global using BotSharp.Abstraction.Agents.Models;
global using BotSharp.Abstraction.Utilities;
global using BotSharp.Abstraction.Conversations.Models;
global using BotSharp.Abstraction.Agents.Enums;
global using BotSharp.Abstraction.Models;

View file

@ -0,0 +1,17 @@
namespace BotSharp.Abstraction.Utilities;
public class Pagination
{
public int Page { get; set; } = 0;
/// <summary>
/// Use -1 for all records
/// </summary>
public int Size { get; set; } = 20;
public int Offset => (Page + 1) * Size;
}
public class PagedItems<T>
{
public int Count { get; set; }
public IEnumerable<T> Items { get; set; }
}

View file

@ -54,16 +54,17 @@ public partial class ConversationService : IConversationService
return conversation;
}
public async Task<List<Conversation>> GetConversations()
public async Task<PagedItems<Conversation>> GetConversations(ConversationFilter filter)
{
var db = _services.GetRequiredService<IBotSharpRepository>();
var user = db.GetUserById(_user.Id);
var filter = new ConversationFilter
{
UserId = user.Role == UserRole.CSR ? string.Empty : user?.Id
};
var conversations = db.GetConversations(filter);
return conversations.OrderByDescending(x => x.CreatedTime).ToList();
var result = new PagedItems<Conversation>
{
Count = conversations.Count(),
Items = conversations.OrderByDescending(x => x.CreatedTime)
};
return result;
}
public async Task<List<Conversation>> GetLastConversations()

View file

@ -1,7 +1,3 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.OpenAPI.ViewModels.Agents;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]

View file

@ -1,14 +1,3 @@
using BotSharp.Abstraction.Agents.Enums;
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Conversations.Enums;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Models;
using BotSharp.OpenAPI.ViewModels.Conversations;
using BotSharp.OpenAPI.ViewModels.Users;
using Microsoft.AspNetCore.Http;
using Microsoft.VisualBasic;
using System.Net.Http.Headers;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]
@ -41,13 +30,16 @@ public class ConversationController : ControllerBase, IApiAdapter
return ConversationViewModel.FromSession(conv);
}
[HttpGet("/conversations/{agentId}")]
public async Task<IEnumerable<ConversationViewModel>> GetConversations()
[HttpGet("/conversations")]
public async Task<PagedItems<ConversationViewModel>> GetConversations([FromQuery] ConversationFilter filter)
{
var service = _services.GetRequiredService<IConversationService>();
var conversations = await service.GetConversations();
var conversations = await service.GetConversations(filter);
var userService = _services.GetRequiredService<IUserService>();
var list = conversations.Select(x => ConversationViewModel.FromSession(x)).ToList();
var list = conversations.Items
.Select(x => ConversationViewModel.FromSession(x))
.ToList();
foreach (var item in list)
{
@ -55,7 +47,11 @@ public class ConversationController : ControllerBase, IApiAdapter
item.User = UserViewModel.FromUser(user);
}
return list;
return new PagedItems<ConversationViewModel>
{
Count = conversations.Count,
Items = list
};
}
[HttpGet("/conversation/{conversationId}/dialogs")]

View file

@ -1,5 +1,3 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Evaluations;
using BotSharp.Abstraction.Evaluations.Models;

View file

@ -1,6 +1,4 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Knowledges.Models;
using Microsoft.AspNetCore.Http;
using BotSharp.Abstraction.Knowledges.Settings;
namespace BotSharp.OpenAPI.Controllers;

View file

@ -1,7 +1,3 @@
using BotSharp.Abstraction.ApiAdapters;
using BotSharp.Abstraction.Users.Models;
using BotSharp.OpenAPI.ViewModels.Users;
namespace BotSharp.OpenAPI.Controllers;
[Authorize]

View file

@ -4,6 +4,8 @@ global using System.Text;
global using System.Threading.Tasks;
global using System.Linq;
global using System.Text.Json;
global using System.Net.Http.Headers;
global using Microsoft.AspNetCore.Http;
global using Microsoft.AspNetCore.Authorization;
global using Microsoft.AspNetCore.Mvc;
global using Microsoft.Extensions.DependencyInjection;
@ -13,6 +15,16 @@ global using BotSharp.Abstraction.Agents;
global using BotSharp.Abstraction.Conversations;
global using BotSharp.Abstraction.Knowledges;
global using BotSharp.Abstraction.Users;
global using BotSharp.Abstraction.Users.Models;
global using BotSharp.Abstraction.Utilities;
global using BotSharp.Abstraction.Agents.Settings;
global using BotSharp.Abstraction.Conversations.Settings;
global using BotSharp.Abstraction.Conversations.Settings;
global using BotSharp.Abstraction.Agents.Enums;
global using BotSharp.Abstraction.ApiAdapters;
global using BotSharp.Abstraction.Conversations.Enums;
global using BotSharp.Abstraction.Conversations.Models;
global using BotSharp.Abstraction.Models;
global using BotSharp.Abstraction.Repositories.Filters;
global using BotSharp.OpenAPI.ViewModels.Conversations;
global using BotSharp.OpenAPI.ViewModels.Users;
global using BotSharp.OpenAPI.ViewModels.Agents;

View file

@ -1,7 +1,5 @@
using BotSharp.Abstraction.Conversations.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.SignalR;
using Microsoft.EntityFrameworkCore;
namespace BotSharp.Plugin.ChatHub;
@ -31,14 +29,18 @@ public class SignalRHub : Hub
var hooks = _services.GetServices<IConversationHook>();
var convService = _services.GetRequiredService<IConversationService>();
_context.HttpContext.Request.Query.TryGetValue("conversationId", out var conversationId);
var conv = await convService.GetConversation(conversationId);
foreach (var hook in hooks)
if (!string.IsNullOrEmpty(conversationId))
{
// Check if user connected with agent is the first time.
if (!conv.Dialogs.Any())
var conv = await convService.GetConversation(conversationId);
foreach (var hook in hooks)
{
await hook.OnUserAgentConnectedInitially(conv);
// Check if user connected with agent is the first time.
if (!conv.Dialogs.Any())
{
await hook.OnUserAgentConnectedInitially(conv);
}
}
}

View file

@ -1,5 +1,6 @@
using BotSharp.Abstraction.Conversations;
using BotSharp.Abstraction.Conversations.Models;
using BotSharp.Abstraction.Repositories.Filters;
using BotSharp.Abstraction.Users.Models;
using BotSharp.Plugin.WeChat.Users;
using Microsoft.AspNetCore.Http;
@ -43,9 +44,12 @@ namespace BotSharp.Plugin.WeChat
var conversationService = scoped.GetRequiredService<IConversationService>();
var latestConversationId = (await conversationService.GetConversations())
.OrderByDescending(_ => _.CreatedTime)
.FirstOrDefault()?.Id;
var latestConversationId = (await conversationService.GetConversations(new ConversationFilter
{
})).Items
.OrderByDescending(_ => _.CreatedTime)
.FirstOrDefault()?.Id;
conversationService.SetConversationId(latestConversationId, new List<string>
{
"channel=wechat"

View file

@ -20,7 +20,7 @@
"prettier": "^3.0.0",
"prettier-plugin-svelte": "^3.0.0",
"sass": "^1.69.5",
"svelte": "^4.2.7",
"svelte": "^4.2.8",
"svelte-check": "^3.6.0",
"typescript": "^5.0.0",
"vite": "^4.4.2"