From e2ab68c154c8de5881644226b0aff484d83ee98b Mon Sep 17 00:00:00 2001 From: Gil Zhang Date: Thu, 22 Aug 2024 11:59:40 +0800 Subject: [PATCH] Add conversation title update and fix query conditions --- .../Controllers/ConversationController.cs | 26 ++++++++++++++++++- .../UpdateConversationTitleModel.cs | 9 +++++++ 2 files changed, 34 insertions(+), 1 deletion(-) create mode 100644 src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/UpdateConversationTitleModel.cs diff --git a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs index 89cdc6e9..9b964dff 100644 --- a/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs +++ b/src/Infrastructure/BotSharp.OpenAPI/Controllers/ConversationController.cs @@ -52,7 +52,7 @@ public class ConversationController : ControllerBase return new PagedItems(); } - filter.UserId = user.Role != UserRole.Admin ? user.Id : null; + filter.UserId = user.Role != UserRole.Admin ? user.Id : filter.UserId; var conversations = await convService.GetConversations(filter); var agentService = _services.GetRequiredService(); var list = conversations.Items.Select(x => ConversationViewModel.FromSession(x)).ToList(); @@ -154,6 +154,7 @@ public class ConversationController : ControllerBase var result = ConversationViewModel.FromSession(conversations.Items.First()); var state = _services.GetRequiredService(); result.States = state.Load(conversationId, isReadOnly: true); + user = await userService.GetUser(result.User.Id); result.User = UserViewModel.FromUser(user); return result; @@ -195,6 +196,29 @@ public class ConversationController : ControllerBase return UserViewModel.FromUser(user); } + [HttpPut("/conversation/{conversationId}/update-title")] + public async Task UpdateConversationTitle([FromRoute] string conversationId, [FromBody] UpdateConversationTitleModel newTile) + { + var userService = _services.GetRequiredService(); + var conversationService = _services.GetRequiredService(); + + var user = await userService.GetUser(_user.Id); + var filter = new ConversationFilter + { + Id = conversationId, + UserId = user.Role != UserRole.Admin ? user.Id : null + }; + var conversations = await conversationService.GetConversations(filter); + + if (conversations.Items.IsNullOrEmpty()) + { + return false; + } + + var response = await conversationService.UpdateConversationTitle(conversationId, newTile.NewTitle); + return response != null; + } + [HttpDelete("/conversation/{conversationId}")] public async Task DeleteConversation([FromRoute] string conversationId) { diff --git a/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/UpdateConversationTitleModel.cs b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/UpdateConversationTitleModel.cs new file mode 100644 index 00000000..3a9ed2bf --- /dev/null +++ b/src/Infrastructure/BotSharp.OpenAPI/ViewModels/Conversations/UpdateConversationTitleModel.cs @@ -0,0 +1,9 @@ +using System.ComponentModel.DataAnnotations; + +namespace BotSharp.OpenAPI.ViewModels.Conversations; + +public class UpdateConversationTitleModel +{ + [Required] + public string NewTitle { get; set; } +}