From 94afb73a19f32952c04c03829c99421acec3e962 Mon Sep 17 00:00:00 2001 From: ilbertt Date: Mon, 6 Jan 2025 18:11:41 +0100 Subject: [PATCH] feat: chat sessions count --- src/backend/backend.did | 2 + src/backend/src/controllers/chat_session.rs | 55 +++++++++++++++++++ src/backend/src/controllers/mod.rs | 1 + .../repositories/chat_session_repository.rs | 6 ++ src/backend/src/services/admin_service.rs | 28 ++++++++++ .../src/services/chat_session_service.rs | 6 ++ src/backend/src/services/mod.rs | 2 + 7 files changed, 100 insertions(+) create mode 100644 src/backend/src/controllers/chat_session.rs create mode 100644 src/backend/src/services/admin_service.rs diff --git a/src/backend/backend.did b/src/backend/backend.did index a7461c9..62c921f 100644 --- a/src/backend/backend.did +++ b/src/backend/backend.did @@ -44,4 +44,6 @@ type StreamingStrategy = variant { service : { http_request : (request : HttpRequest) -> (HttpResponse) query; http_request_update : (request : HttpUpdateRequest) -> (HttpResponse); + + get_chat_sessions_count : () -> (nat32) query; }; diff --git a/src/backend/src/controllers/chat_session.rs b/src/backend/src/controllers/chat_session.rs new file mode 100644 index 0000000..2f0b80a --- /dev/null +++ b/src/backend/src/controllers/chat_session.rs @@ -0,0 +1,55 @@ +use candid::Principal; +use ic_cdk::{caller, query}; + +use crate::{ + repositories::{ChatSessionRepositoryImpl, FilesystemRepositoryImpl}, + services::{ + AdminService, AdminServiceImpl, ChatSessionService, ChatSessionServiceImpl, + FilesystemServiceImpl, + }, +}; + +#[query] +fn get_chat_sessions_count() -> u32 { + let calling_principal = caller(); + + ChatSessionController::default().get_chat_sessions_count(calling_principal) +} + +struct ChatSessionController { + admin_service: A, + chat_session_service: C, +} + +impl Default + for ChatSessionController< + AdminServiceImpl, + ChatSessionServiceImpl< + ChatSessionRepositoryImpl, + FilesystemServiceImpl, + >, + > +{ + fn default() -> Self { + Self::new( + AdminServiceImpl::default(), + ChatSessionServiceImpl::default(), + ) + } +} + +impl ChatSessionController { + fn new(admin_service: A, chat_session_service: C) -> Self { + Self { + admin_service, + chat_session_service, + } + } + + fn get_chat_sessions_count(&self, calling_principal: Principal) -> u32 { + self.admin_service + .asset_caller_is_controller(&calling_principal); + + self.chat_session_service.get_chat_sessions_count() + } +} diff --git a/src/backend/src/controllers/mod.rs b/src/backend/src/controllers/mod.rs index 8074a0f..f2068be 100644 --- a/src/backend/src/controllers/mod.rs +++ b/src/backend/src/controllers/mod.rs @@ -1 +1,2 @@ +mod chat_session; mod http; diff --git a/src/backend/src/repositories/chat_session_repository.rs b/src/backend/src/repositories/chat_session_repository.rs index 3488e69..82a8e4b 100644 --- a/src/backend/src/repositories/chat_session_repository.rs +++ b/src/backend/src/repositories/chat_session_repository.rs @@ -6,6 +6,8 @@ pub trait ChatSessionRepository { fn get_chat_session_by_chat_id(&self, chat_id: &ChatId) -> Option; fn set_chat_session_by_chat_id(&self, chat_id: ChatId, chat_session: ChatSession); + + fn get_chat_session_count(&self) -> u64; } pub struct ChatSessionRepositoryImpl {} @@ -24,6 +26,10 @@ impl ChatSessionRepository for ChatSessionRepositoryImpl { fn set_chat_session_by_chat_id(&self, chat_id: ChatId, chat_session: ChatSession) { STATE.with_borrow_mut(|s| s.chat_session.insert(chat_id, chat_session)); } + + fn get_chat_session_count(&self) -> u64 { + STATE.with_borrow(|s| s.chat_session.len()) + } } impl ChatSessionRepositoryImpl { diff --git a/src/backend/src/services/admin_service.rs b/src/backend/src/services/admin_service.rs new file mode 100644 index 0000000..8f00bf7 --- /dev/null +++ b/src/backend/src/services/admin_service.rs @@ -0,0 +1,28 @@ +use candid::Principal; +use ic_cdk::{api::is_controller, trap}; + +pub trait AdminService { + fn asset_caller_is_controller(&self, calling_principal: &Principal); +} + +pub struct AdminServiceImpl {} + +impl Default for AdminServiceImpl { + fn default() -> Self { + Self::new() + } +} + +impl AdminService for AdminServiceImpl { + fn asset_caller_is_controller(&self, calling_principal: &Principal) { + if !is_controller(calling_principal) { + trap("caller is not a controller"); + } + } +} + +impl AdminServiceImpl { + fn new() -> Self { + Self {} + } +} diff --git a/src/backend/src/services/chat_session_service.rs b/src/backend/src/services/chat_session_service.rs index b2473f0..0954324 100644 --- a/src/backend/src/services/chat_session_service.rs +++ b/src/backend/src/services/chat_session_service.rs @@ -29,6 +29,8 @@ pub trait ChatSessionService { fn update_chat_session(&self, chat_id: ChatId, chat_session: ChatSession); + fn get_chat_sessions_count(&self) -> u32; + fn handle_update_content_message( &self, chat_id: ChatId, @@ -84,6 +86,10 @@ impl ChatSessionService .set_chat_session_by_chat_id(chat_id, chat_session); } + fn get_chat_sessions_count(&self) -> u32 { + self.chat_session_repository.get_chat_session_count() as u32 + } + fn handle_update_content_message( &self, chat_id: ChatId, diff --git a/src/backend/src/services/mod.rs b/src/backend/src/services/mod.rs index 2e481fa..3b26b20 100644 --- a/src/backend/src/services/mod.rs +++ b/src/backend/src/services/mod.rs @@ -1,5 +1,7 @@ +mod admin_service; mod chat_session_service; mod filesystem_service; +pub use admin_service::*; pub use chat_session_service::*; pub use filesystem_service::*;