[BE][Classroom] Parse & send courseid in the user courses list

This commit is contained in:
Araozu 2023-12-22 15:45:52 -05:00
parent 815db6d7ed
commit 54c61b8b2a
9 changed files with 84 additions and 20 deletions

23
backend/Cargo.lock generated
View File

@ -159,6 +159,7 @@ dependencies = [
"lazy_static", "lazy_static",
"log", "log",
"once_cell", "once_cell",
"regex",
"reqwest", "reqwest",
"rocket", "rocket",
"scraper", "scraper",
@ -1577,9 +1578,9 @@ dependencies = [
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.5.0" version = "2.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d" checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
[[package]] [[package]]
name = "memoffset" name = "memoffset"
@ -2335,14 +2336,14 @@ dependencies = [
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.9.3" version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81bc1d4caf89fac26a70747fe603c130093b53c773888797a6329091246d651a" checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
"regex-automata 0.3.6", "regex-automata 0.4.3",
"regex-syntax 0.7.4", "regex-syntax 0.8.2",
] ]
[[package]] [[package]]
@ -2356,13 +2357,13 @@ dependencies = [
[[package]] [[package]]
name = "regex-automata" name = "regex-automata"
version = "0.3.6" version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fed1ceff11a1dddaee50c9dc8e4938bd106e9d89ae372f192311e7da498e3b69" checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
dependencies = [ dependencies = [
"aho-corasick", "aho-corasick",
"memchr", "memchr",
"regex-syntax 0.7.4", "regex-syntax 0.8.2",
] ]
[[package]] [[package]]
@ -2373,9 +2374,9 @@ checksum = "f162c6dd7b008981e4d40210aca20b4bd0f9b60ca9271061b07f78537722f2e1"
[[package]] [[package]]
name = "regex-syntax" name = "regex-syntax"
version = "0.7.4" version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5ea92a5b6195c6ef2a0295ea818b312502c6fc94dde986c5553242e18fd4ce2" checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]] [[package]]
name = "reqwest" name = "reqwest"

View File

@ -22,5 +22,6 @@ env_logger = "0.10.0"
bardecoder = "0.5.0" bardecoder = "0.5.0"
image = "0.24.7" image = "0.24.7"
imageproc = "0.23.0" imageproc = "0.23.0"
regex = "1.10.2"

View File

@ -4,7 +4,6 @@ use log::{error, info};
use reqwest::Client; use reqwest::Client;
use rocket::http::Status; use rocket::http::Status;
use rocket::serde::json::Json; use rocket::serde::json::Json;
use sqlx::Connection;
use crate::json_result::JsonResult; use crate::json_result::JsonResult;
use crate::model::person::{PersonCreate, PersonLink}; use crate::model::person::{PersonCreate, PersonLink};

View File

@ -1,9 +1,7 @@
use cors::Cors; use cors::Cors;
use once_cell::sync::OnceCell; use once_cell::sync::OnceCell;
use sqlx::mysql::MySqlPoolOptions; use sqlx::mysql::MySqlPoolOptions;
use sqlx::Connection;
use sqlx::MySql; use sqlx::MySql;
use sqlx::MySqlConnection;
use sqlx::Pool; use sqlx::Pool;
use std::env; use std::env;
use std::time::Instant; use std::time::Instant;

View File

@ -1,10 +1,12 @@
use super::session::request; use super::session::request;
use regex::Regex;
use scraper::{Html, Selector}; use scraper::{Html, Selector};
use serde::Serialize; use serde::Serialize;
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
pub struct ClassroomCourse { pub struct ClassroomCourse {
name: String, name: String,
course_id: i32,
} }
/// Returns all the courses the user is enrolled in /// Returns all the courses the user is enrolled in
@ -59,7 +61,62 @@ pub async fn get_courses(user_id: i32) -> Result<Vec<ClassroomCourse>, String> {
} }
let course_name = td_els[1].inner_html(); let course_name = td_els[1].inner_html();
courses.push(ClassroomCourse { name: course_name })
let course_actions_container = td_els[5];
let anchor_container_children = course_actions_container.children().collect::<Vec<_>>();
let delete_anchor = anchor_container_children.get(5);
let delete_anchor = match delete_anchor {
Some(anchor) => anchor,
None => {
return Err(format!(
"Error parsing tr: delete anchor not found in td[5] inner html: {}",
course_name
));
}
};
// user_information.php?action=unsubscribe&course_id=14&user_id=2421
let delete_anchor_href = match delete_anchor.value().as_element().unwrap().attr("href") {
Some(href) => href,
None => {
return Err(format!(
"Error parsing tr: delete anchor href not found: {}",
course_name
));
}
};
let re = Regex::new(r"course_id=(\d+)").unwrap();
let captures = match re.captures(delete_anchor_href) {
Some(c) => c,
None => {
log::info!("course_href: {}", delete_anchor_href);
return Err(format!(
"Error parsing tr: link course id not found. course: {}",
course_name
));
}
};
let course_id_str = captures.get(1).unwrap().as_str();
let course_id_result = course_id_str.parse::<i32>();
let course_id = match course_id_result {
Ok(id) => id,
Err(_) => {
log::info!("matched course_id: {}", course_id_str);
return Err(format!(
"Error parsing course_id: not a number. course: {}",
course_name
));
}
};
courses.push(ClassroomCourse {
name: course_name,
course_id,
})
} }
Ok(courses) Ok(courses)

View File

@ -11,6 +11,7 @@ pub mod register_course;
mod session; mod session;
pub mod update_expiration_date; pub mod update_expiration_date;
pub mod user; pub mod user;
pub mod course_disenroll;
/// Tries to connect to the online classroom, and gets a session cookie /// Tries to connect to the online classroom, and gets a session cookie
/// if neccesary. /// if neccesary.

View File

@ -1,4 +1,4 @@
import { For, Show, createEffect, onMount } from "solid-js"; import { For, Show, createEffect } from "solid-js";
import { LoadingStatus, backend, useLoading, wait } from "../../utils/functions"; import { LoadingStatus, backend, useLoading, wait } from "../../utils/functions";
import { JsonResult } from "../../types/JsonResult"; import { JsonResult } from "../../types/JsonResult";
import { ClassroomCourse } from "../../types/ClassroomCourse"; import { ClassroomCourse } from "../../types/ClassroomCourse";
@ -8,8 +8,7 @@ import { XIcon } from "../../icons/XIcon";
import { QuestionIcon } from "../../icons/QuestionIcon"; import { QuestionIcon } from "../../icons/QuestionIcon";
export function CoursesList(props: {userid: number, updateSignal: number, courses: Array<ClassroomCourse>, setCourses: (c: Array<ClassroomCourse>) => void}) { export function CoursesList(props: {userid: number, updateSignal: number, courses: Array<ClassroomCourse>, setCourses: (c: Array<ClassroomCourse>) => void}) {
const {error, setError, status, setStatus} = useLoading();
const {setError, status, setStatus} = useLoading();
const loadCourses = async() => { const loadCourses = async() => {
setStatus(LoadingStatus.Loading); setStatus(LoadingStatus.Loading);
@ -30,7 +29,7 @@ export function CoursesList(props: {userid: number, updateSignal: number, course
}); });
}; };
onMount(loadCourses); // onMount(loadCourses);
createEffect(() => { createEffect(() => {
// Update courses when updateSignal changes // Update courses when updateSignal changes
@ -61,6 +60,13 @@ export function CoursesList(props: {userid: number, updateSignal: number, course
<For each={props.courses}> <For each={props.courses}>
{(c) => <Course course={c} />} {(c) => <Course course={c} />}
</For> </For>
<Show when={error() !== ""}>
<p class="text-c-error">
Error recuperando cursos: {error()}
</p>
</Show>
<Show when={status() === LoadingStatus.Ok && props.courses.length === 0}> <Show when={status() === LoadingStatus.Ok && props.courses.length === 0}>
<div class="px-4 pb-2 text-center"> <div class="px-4 pb-2 text-center">
<div class="text-center pt-6 pb-4"> <div class="text-center pt-6 pb-4">

View File

@ -1,5 +1,6 @@
export type ClassroomCourse = { export type ClassroomCourse = {
name: string name: string,
course_id: number,
} }
export type ClassroomCourseValue = export type ClassroomCourseValue =