pub async fn query(auth: AuthCookie, conn: Database) -> Result<Self, diesel::result::Error> {
        conn.run(move |c| {
            let sync_tasks = class_synchronous_task::table
                .inner_join(
                    class::table
                        .left_join(
                            class_student::table.inner_join(student_class_asynchronous_task::table),
                        )
                        .left_join(class_teacher::table),
                )
                .filter(
                    class_student::user_id
                        .eq(auth.0)
                        .or(class_teacher::user_id.eq(auth.0)),
                )
                .filter(class_synchronous_task::start_time.ge(chrono::Utc::now().naive_utc()))
                .select((class_synchronous_task::all_columns, class::all_columns))
                .load::<(ClassSynchronousTask, crate::models::Class)>(c)?
                .into_iter()
                .map(|(task, class)| SynchronousTask { task, class })
                .collect::<Vec<_>>();
            let async_tasks = class_asynchronous_task::table
                .inner_join(
                    class::table
                        .left_join(
                            class_student::table.inner_join(student_class_asynchronous_task::table),
                        )
                        .left_join(class_teacher::table),
                )
                .filter(class_asynchronous_task::due_date.ge(chrono::Utc::now().naive_utc()))
                .filter(
                    class_student::user_id
                        .eq(auth.0)
                        .or(class_teacher::user_id.eq(auth.0)),
                )
                // for each teacher task we have to send another query to the database, so to this
                // is here (the number is a total guess) to keep response times reasonable.
                .limit(5)
                .load::<(
                    ClassAsynchronousTask,
                    (
                        crate::models::Class,
                        Option<(ClassStudent, StudentClassAsynchronousTask)>,
                        Option<ClassTeacher>,
                    ),
                )>(c)
                .and_then(|list| {
                    let mut output = vec![];
                    for (task, (class, class_student, class_teacher)) in list {
                        if class_student.is_none() && class_teacher.is_none() {
                            continue;
                        }
                        let is_class_student = class_student
                            .as_ref()
                            .map(|(student, _)| student.user_id == auth.0)
                            .unwrap_or(false);
                        if is_class_student {
                            if let Some((_, student_task)) = class_student {
                                output.push(AsynchronousTask::Student(StudentTask {
                                    task,
                                    student_task,
                                    class,
                                }))
                            } else {
                                unreachable!()
                            }
                        } else if class_teacher.is_some() {
                            let student_tasks = StudentClassAsynchronousTask::belonging_to(&task)
                                .inner_join(class_student::table.inner_join(users::table))
                                .select((
                                    crate::schema::users::all_columns,
                                    crate::schema::student_class_asynchronous_task::all_columns,
                                ))
                                .load::<(User, StudentClassAsynchronousTask)>(c)?;
                            output.push(AsynchronousTask::Teacher(TeacherTask {
                                task,
                                number_set_to: student_tasks.len() as i32,
                                number_completed: student_tasks
                                    .into_iter()
                                    .map(|(_, task)| if task.completed { 1 } else { 0 })
                                    .sum::<i32>(),
                                class,
                            }))
                        } else {
                            unreachable!()
                        }
                    }
                    Ok(output)
                })?;

            Ok(Self {
                sync_tasks,
                async_tasks,
            })
        })
        .await
    }
/// Retrieve the dashboard from the database.
Retrieve the dashboard from the database.
Retrieve the dashboard from the database .
pub async fn query(auth: AuthCookie, conn: Database) -> Result<Self, diesel::result::Error> {
        conn.run(move |c| {
            let sync_tasks = class_synchronous_task::table
                .inner_join(
                    class::table
                        .left_join(
                            class_student::table.inner_join(student_class_asynchronous_task::table),
                        )
                        .left_join(class_teacher::table),
                )
                .filter(
                    class_student::user_id
                        .eq(auth.0)
                        .or(class_teacher::user_id.eq(auth.0)),
                )
                .filter(class_synchronous_task::start_time.ge(chrono::Utc::now().naive_utc()))
                .select((class_synchronous_task::all_columns, class::all_columns))
                .load::<(ClassSynchronousTask, crate::models::Class)>(c)?
                .into_iter()
                .map(|(task, class)| SynchronousTask { task, class })
                .collect::<Vec<_>>();
            let async_tasks = class_asynchronous_task::table
                .inner_join(
                    class::table
                        .left_join(
                            class_student::table.inner_join(student_class_asynchronous_task::table),
                        )
                        .left_join(class_teacher::table),
                )
                .filter(class_asynchronous_task::due_date.ge(chrono::Utc::now().naive_utc()))
                .filter(
                    class_student::user_id
                        .eq(auth.0)
                        .or(class_teacher::user_id.eq(auth.0)),
                )
                // for each teacher task we have to send another query to the database, so to this
                // is here (the number is a total guess) to keep response times reasonable.
                .limit(5)
                .load::<(
                    ClassAsynchronousTask,
                    (
                        crate::models::Class,
                        Option<(ClassStudent, StudentClassAsynchronousTask)>,
                        Option<ClassTeacher>,
                    ),
                )>(c)
                .and_then(|list| {
                    let mut output = vec![];
                    for (task, (class, class_student, class_teacher)) in list {
                        if class_student.is_none() && class_teacher.is_none() {
                            continue;
                        }
                        let is_class_student = class_student
                            .as_ref()
                            .map(|(student, _)| student.user_id == auth.0)
                            .unwrap_or(false);
                        if is_class_student {
                            if let Some((_, student_task)) = class_student {
                                output.push(AsynchronousTask::Student(StudentTask {
                                    task,
                                    student_task,
                                    class,
                                }))
                            } else {
                                unreachable!()
                            }
                        } else if class_teacher.is_some() {
                            let student_tasks = StudentClassAsynchronousTask::belonging_to(&task)
                                .inner_join(class_student::table.inner_join(users::table))
                                .select((
                                    crate::schema::users::all_columns,
                                    crate::schema::student_class_asynchronous_task::all_columns,
                                ))
                                .load::<(User, StudentClassAsynchronousTask)>(c)?;
                            output.push(AsynchronousTask::Teacher(TeacherTask {
                                task,
                                number_set_to: student_tasks.len() as i32,
                                number_completed: student_tasks
                                    .into_iter()
                                    .map(|(_, task)| if task.completed { 1 } else { 0 })
                                    .sum::<i32>(),
                                class,
                            }))
                        } else {
                            unreachable!()
                        }
                    }
                    Ok(output)
                })?;
            Ok(Self {
                sync_tasks,
                async_tasks,
            })
        })
        .await
    }
pub async fn query ( auth : AuthCookie , conn : Database ) -> Result < Self , diesel :: result :: Error > { conn . run ( move | c | { let sync_tasks = class_synchronous_task :: table . inner_join ( class :: table . left_join ( class_student :: table . inner_join ( student_class_asynchronous_task :: table ) , ) . left_join ( class_teacher :: table ) , ) . filter ( class_student :: user_id . eq ( auth . 0 ) . or ( class_teacher :: user_id . eq ( auth . 0 ) ) , ) . filter ( class_synchronous_task :: start_time . ge ( chrono :: Utc :: now ( ) . naive_utc ( ) ) ) . select ( ( class_synchronous_task :: all_columns , class :: all_columns ) ) . load :: < ( ClassSynchronousTask , crate :: models :: Class ) > ( c ) ? . into_iter ( ) . map ( | ( task , class ) | SynchronousTask { task , class } ) . collect :: < Vec < _ > > ( ) ; let async_tasks = class_asynchronous_task :: table . inner_join ( class :: table . left_join ( class_student :: table . inner_join ( student_class_asynchronous_task :: table ) , ) . left_join ( class_teacher :: table ) , ) . filter ( class_asynchronous_task :: due_date . ge ( chrono :: Utc :: now ( ) . naive_utc ( ) ) ) . filter ( class_student :: user_id . eq ( auth . 0 ) . or ( class_teacher :: user_id . eq ( auth . 0 ) ) , ) // for each teacher task we have to send another query to the database, so to this // is here (the number is a total guess) to keep response times reasonable. . limit ( 5 ) . load :: < ( ClassAsynchronousTask , ( crate :: models :: Class , Option < ( ClassStudent , StudentClassAsynchronousTask ) > , Option < ClassTeacher > , ) , ) > ( c ) . and_then ( | list | { let mut output = vec ! [ ] ; for ( task , ( class , class_student , class_teacher ) ) in list { if class_student . is_none ( ) && class_teacher . is_none ( ) { continue ; } let is_class_student = class_student . as_ref ( ) . map ( | ( student , _ ) | student . user_id == auth . 0 ) . unwrap_or ( false ) ; if is_class_student { if let Some ( ( _ , student_task ) ) = class_student { output . push ( AsynchronousTask :: Student ( StudentTask { task , student_task , class , } ) ) } else { unreachable ! ( ) } } else if class_teacher . is_some ( ) { let student_tasks = StudentClassAsynchronousTask :: belonging_to ( & task ) . inner_join ( class_student :: table . inner_join ( users :: table ) ) . select ( ( crate :: schema :: users :: all_columns , crate :: schema :: student_class_asynchronous_task :: all_columns , ) ) . load :: < ( User , StudentClassAsynchronousTask ) > ( c ) ? ; output . push ( AsynchronousTask :: Teacher ( TeacherTask { task , number_set_to : student_tasks . len ( ) as i32 , number_completed : student_tasks . into_iter ( ) . map ( | ( _ , task ) | if task . completed { 1 } else { 0 } ) . sum :: < i32 > ( ) , class , } ) ) } else { unreachable ! ( ) } } Ok ( output ) } ) ? ; Ok ( Self { sync_tasks , async_tasks , } ) } ) . await }
Retrieve the dashboard from the database.
Retrieve the dashboard from the database .
auth
AuthCookie
conn
Database