Skip to content

Commit

Permalink
feat: chat sessions count
Browse files Browse the repository at this point in the history
  • Loading branch information
ilbertt committed Jan 6, 2025
1 parent 01f3c7b commit 94afb73
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/backend/backend.did
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
55 changes: 55 additions & 0 deletions src/backend/src/controllers/chat_session.rs
Original file line number Diff line number Diff line change
@@ -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<A: AdminService, C: ChatSessionService> {
admin_service: A,
chat_session_service: C,
}

impl Default
for ChatSessionController<
AdminServiceImpl,
ChatSessionServiceImpl<
ChatSessionRepositoryImpl,
FilesystemServiceImpl<FilesystemRepositoryImpl>,
>,
>
{
fn default() -> Self {
Self::new(
AdminServiceImpl::default(),
ChatSessionServiceImpl::default(),
)
}
}

impl<A: AdminService, C: ChatSessionService> ChatSessionController<A, C> {
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()
}
}
1 change: 1 addition & 0 deletions src/backend/src/controllers/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod chat_session;
mod http;
6 changes: 6 additions & 0 deletions src/backend/src/repositories/chat_session_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ pub trait ChatSessionRepository {
fn get_chat_session_by_chat_id(&self, chat_id: &ChatId) -> Option<ChatSession>;

fn set_chat_session_by_chat_id(&self, chat_id: ChatId, chat_session: ChatSession);

fn get_chat_session_count(&self) -> u64;
}

pub struct ChatSessionRepositoryImpl {}
Expand All @@ -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 {
Expand Down
28 changes: 28 additions & 0 deletions src/backend/src/services/admin_service.rs
Original file line number Diff line number Diff line change
@@ -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 {}
}
}
6 changes: 6 additions & 0 deletions src/backend/src/services/chat_session_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -84,6 +86,10 @@ impl<T: ChatSessionRepository, F: FilesystemService> 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,
Expand Down
2 changes: 2 additions & 0 deletions src/backend/src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -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::*;

0 comments on commit 94afb73

Please sign in to comment.