feat: add teacher_id filter to student daka query
Allow students to filter dakas by teacher_id when querying. Both student_id and teacher_id can now coexist in the request. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -831,8 +831,9 @@ pub async fn raw_find_dakas_with_sections_by_user_id(
|
||||
debug!("raw_find_dakas_with_sections_by_user_id -> teacher_id: {:?} / student_id: {:?} / scope: {:?}", teacher_id, student_id, scope);
|
||||
let current_org_id = required_current_org_id_from_sudoer_token_str(&_root.0)?;
|
||||
|
||||
if (teacher_id.is_some() && student_id.is_some())
|
||||
|| (teacher_id.is_none() && student_id.is_none())
|
||||
// student_id and teacher_id can coexist (student filtering by teacher)
|
||||
// but at least one must be present
|
||||
if teacher_id.is_none() && student_id.is_none()
|
||||
{
|
||||
return Err(anyhow!(HtyErr {
|
||||
code: HtyErrCode::WebErr,
|
||||
@@ -898,7 +899,7 @@ pub async fn raw_find_dakas_with_sections_by_user_id(
|
||||
|
||||
dakas = dakas_with_pages.0;
|
||||
} else {
|
||||
// student_id
|
||||
// student_id (with optional teacher_id filter)
|
||||
let id_student = student_id
|
||||
.as_ref()
|
||||
.ok_or_else(|| anyhow!("student_id is required"))?;
|
||||
@@ -908,6 +909,7 @@ pub async fn raw_find_dakas_with_sections_by_user_id(
|
||||
¤t_org_id,
|
||||
page,
|
||||
page_size,
|
||||
*teacher_id,
|
||||
extract_conn(fetch_db_conn(&db_pool)?).deref_mut(),
|
||||
)?;
|
||||
dakas = dakas_with_pages.0;
|
||||
|
||||
@@ -3456,13 +3456,19 @@ impl Daka {
|
||||
id_org: &String,
|
||||
page: i64,
|
||||
page_size: i64,
|
||||
id_teacher: Option<&String>,
|
||||
conn: &mut PgConnection,
|
||||
) -> anyhow::Result<(Vec<Daka>, i64, i64)> {
|
||||
// 从students里面jsonb查询有这个`id_student`的dakas
|
||||
// 可以参考Daka::find_active_dakas_by_all_teachers_with_pagination
|
||||
use diesel::prelude::*;
|
||||
|
||||
let q_total = format!("select count(*) as result from daka where is_delete is not True AND org_id='{}' AND jsonb_path_exists(students, '$.val.users.vals[*].user_id ? (@ == \"{}\")')", id_org, id_student).to_string();
|
||||
let teacher_filter = match id_teacher {
|
||||
Some(tid) => format!(" AND teacher_id='{}'", tid),
|
||||
None => "".to_string(),
|
||||
};
|
||||
|
||||
let q_total = format!("select count(*) as result from daka where is_delete is not True AND org_id='{}' AND jsonb_path_exists(students, '$.val.users.vals[*].user_id ? (@ == \"{}\")'){}", id_org, id_student, teacher_filter).to_string();
|
||||
debug!(
|
||||
"find_active_dakas_by_student_id_with_pagination -> q_total: {:?}",
|
||||
q_total
|
||||
@@ -3473,7 +3479,7 @@ impl Daka {
|
||||
.result;
|
||||
let total_page = total_len.clone() / page_size;
|
||||
|
||||
let q = format!("select * from daka where is_delete is not True AND org_id='{}' AND jsonb_path_exists(students, '$.val.users.vals[*].user_id ? (@ == \"{}\")') ORDER BY updated_at DESC, created_at DESC LIMIT {} OFFSET ({} - 1) * {}", id_org, id_student, page_size.to_string(), page.to_string(), page_size.to_string()).to_string();
|
||||
let q = format!("select * from daka where is_delete is not True AND org_id='{}' AND jsonb_path_exists(students, '$.val.users.vals[*].user_id ? (@ == \"{}\")'){} ORDER BY updated_at DESC, created_at DESC LIMIT {} OFFSET ({} - 1) * {}", id_org, id_student, teacher_filter, page_size.to_string(), page.to_string(), page_size.to_string()).to_string();
|
||||
debug!(
|
||||
"find_active_dakas_by_student_id_with_pagination -> q: {:?}",
|
||||
q
|
||||
|
||||
Reference in New Issue
Block a user