feat: Add session counting methods for mentors and users; enhance registration queries with related data

This commit is contained in:
MythEclipse
2025-10-28 14:57:20 +07:00
parent b9a51ce6cc
commit 5e2b0d3caf
5 changed files with 221 additions and 25 deletions
@@ -266,6 +266,86 @@ impl<'a> SessionsRepository<'a> {
updated.ok_or_else(|| "Session update returned None".to_string())
}
// ============================================
// Count Mentor Sessions
// ============================================
pub async fn count_mentor_sessions(
&self,
mentor_id: &Thing,
status_filter: Option<String>,
) -> Result<usize, String> {
let query = if status_filter.is_some() {
"SELECT count() FROM sessions WHERE mentor_id = $mentor_id AND status = $status GROUP ALL"
} else {
"SELECT count() FROM sessions WHERE mentor_id = $mentor_id GROUP ALL"
};
let db = &self.state.surrealdb_ws;
let mentor_id_clone = mentor_id.clone();
let mut result = if let Some(status_val) = status_filter {
db.query(query)
.bind(("mentor_id", mentor_id_clone))
.bind(("status", status_val))
.await
} else {
db.query(query)
.bind(("mentor_id", mentor_id_clone))
.await
}
.map_err(|e| format!("Failed to count mentor sessions: {}", e))?;
#[derive(serde::Deserialize)]
struct CountResult {
count: usize,
}
let count_result: Option<CountResult> = result
.take(0)
.map_err(|e| format!("Failed to parse count: {}", e))?;
Ok(count_result.map(|r| r.count).unwrap_or(0))
}
// ============================================
// Count User Sessions
// ============================================
pub async fn count_user_sessions(
&self,
user_id: &Thing,
status_filter: Option<String>,
) -> Result<usize, String> {
let query = if status_filter.is_some() {
"SELECT count() FROM sessions WHERE mentee_id = $user_id AND status = $status GROUP ALL"
} else {
"SELECT count() FROM sessions WHERE mentee_id = $user_id GROUP ALL"
};
let db = &self.state.surrealdb_ws;
let user_id_clone = user_id.clone();
let mut result = if let Some(status_val) = status_filter {
db.query(query)
.bind(("user_id", user_id_clone))
.bind(("status", status_val))
.await
} else {
db.query(query)
.bind(("user_id", user_id_clone))
.await
}
.map_err(|e| format!("Failed to count user sessions: {}", e))?;
#[derive(serde::Deserialize)]
struct CountResult {
count: usize,
}
let count_result: Option<CountResult> = result
.take(0)
.map_err(|e| format!("Failed to parse count: {}", e))?;
Ok(count_result.map(|r| r.count).unwrap_or(0))
}
// ============================================
// Delete Session (soft delete)
// ============================================
@@ -64,6 +64,13 @@ impl SessionsService {
let mentor_thing = make_thing("mentors", &mentor_id);
let repo = SessionsRepository::new(state);
// Get count and sessions
let count = match repo.count_mentor_sessions(&mentor_thing, status_filter.clone()).await {
Ok(c) => c,
Err(e) => return common_response(StatusCode::INTERNAL_SERVER_ERROR, &format!("Failed to count sessions: {}", e)),
};
match repo.query_mentor_sessions(&mentor_thing, status_filter).await {
Ok(sessions) => {
let session_items: Vec<SessionListItemDto> = sessions
@@ -86,7 +93,7 @@ impl SessionsService {
let response = SessionListResponseDto {
sessions: session_items,
total: 0, // TODO: implement proper pagination
total: count,
};
success_response(ResponseSuccessDto { data: response })
}
@@ -105,6 +112,13 @@ impl SessionsService {
let user_thing = make_thing("users", &user_id);
let repo = SessionsRepository::new(state);
// Get count and sessions
let count = match repo.count_user_sessions(&user_thing, status_filter.clone()).await {
Ok(c) => c,
Err(e) => return common_response(StatusCode::INTERNAL_SERVER_ERROR, &format!("Failed to count sessions: {}", e)),
};
match repo.query_user_sessions(&user_thing, status_filter).await {
Ok(sessions) => {
let session_items: Vec<SessionListItemDto> = sessions
@@ -127,7 +141,7 @@ impl SessionsService {
let response = SessionListResponseDto {
sessions: session_items,
total: 0, // TODO: implement proper pagination
total: count,
};
success_response(ResponseSuccessDto { data: response })
}