[Classroom][BE] Code for course enrollment
This commit is contained in:
parent
b162b14eb3
commit
2f924d875c
@ -1,9 +1,10 @@
|
|||||||
use crate::{
|
use crate::{
|
||||||
json_result::JsonResult,
|
json_result::JsonResult,
|
||||||
model::classroom_user::ClassroomPersonCreate,
|
model::classroom_user::{ClassroomPersonCreate, ClassroomCourseRegistration},
|
||||||
online_classroom::{create_user::create, get_courses::ClassroomCourse},
|
online_classroom::{create_user::create, get_courses::ClassroomCourse},
|
||||||
};
|
};
|
||||||
use rocket::{http::Status, serde::json::Json};
|
use rocket::{http::Status, serde::json::Json};
|
||||||
|
use crate::online_classroom::register_course::register_course;
|
||||||
|
|
||||||
#[options("/classroom/user")]
|
#[options("/classroom/user")]
|
||||||
pub fn create_user_options() -> Status {
|
pub fn create_user_options() -> Status {
|
||||||
@ -25,3 +26,12 @@ pub async fn get_courses(user_id: i32) -> (Status, Json<JsonResult<Vec<Classroom
|
|||||||
Err(err) => return (Status::InternalServerError, JsonResult::err(err)),
|
Err(err) => return (Status::InternalServerError, JsonResult::err(err)),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[post("/classroom/course/<user_id>", format = "json", data = "<data>")]
|
||||||
|
pub async fn register_course_contr(user_id: i32, data: Json<ClassroomCourseRegistration>) -> (Status, Json<JsonResult<()>>) {
|
||||||
|
match register_course(user_id, &data.surname_first_letter, &data.courses).await {
|
||||||
|
Ok(()) => return (Status::Ok, JsonResult::ok(())),
|
||||||
|
Err(err) => return (Status::InternalServerError, JsonResult::err(err)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -62,6 +62,7 @@ async fn rocket() -> _ {
|
|||||||
controller::classroom::create_user_options,
|
controller::classroom::create_user_options,
|
||||||
controller::classroom::create_user,
|
controller::classroom::create_user,
|
||||||
controller::classroom::get_courses,
|
controller::classroom::get_courses,
|
||||||
|
controller::classroom::register_course_contr,
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -18,3 +18,9 @@ pub struct ClassroomPersonCreate {
|
|||||||
pub person_username: String,
|
pub person_username: String,
|
||||||
pub person_password: String,
|
pub person_password: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Deserialize)]
|
||||||
|
pub struct ClassroomCourseRegistration {
|
||||||
|
pub surname_first_letter: String,
|
||||||
|
pub courses: Vec<String>,
|
||||||
|
}
|
||||||
|
@ -6,6 +6,7 @@ use self::session::ensure_session;
|
|||||||
|
|
||||||
pub mod create_user;
|
pub mod create_user;
|
||||||
pub mod get_courses;
|
pub mod get_courses;
|
||||||
|
pub mod register_course;
|
||||||
mod session;
|
mod session;
|
||||||
pub mod user;
|
pub mod user;
|
||||||
|
|
||||||
|
33
backend/src/online_classroom/register_course.rs
Normal file
33
backend/src/online_classroom/register_course.rs
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
use scraper::{Html, Selector};
|
||||||
|
|
||||||
|
pub async fn register_course(
|
||||||
|
user_id: i32,
|
||||||
|
surname_first_letter: &String,
|
||||||
|
courses: &Vec<String>,
|
||||||
|
) -> Result<(), String> {
|
||||||
|
let courses_uri = courses
|
||||||
|
.iter()
|
||||||
|
.map(|course| format!("CourseList%5B%5D={}", course))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join("&");
|
||||||
|
|
||||||
|
let request_body = format!(
|
||||||
|
"form_sent=1&firstLetterUser={}&firstLetterCourse=&UserList%5B%5D={}&{}",
|
||||||
|
surname_first_letter, user_id, courses_uri
|
||||||
|
);
|
||||||
|
|
||||||
|
let response_html = super::session::register_courses_request(
|
||||||
|
"/main/admin/subscribe_user2course.php".into(),
|
||||||
|
request_body,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
|
||||||
|
let success_selector = Selector::parse(".alert.alert-success").unwrap();
|
||||||
|
|
||||||
|
let fragment = Html::parse_document(&response_html);
|
||||||
|
|
||||||
|
match fragment.select(&success_selector).next() {
|
||||||
|
Some(_) => Ok(()),
|
||||||
|
None => Err("Error registrando curso".into()),
|
||||||
|
}
|
||||||
|
}
|
@ -64,7 +64,7 @@ pub async fn create_user_request(url: String, body: String) -> Result<String, St
|
|||||||
// Do the request
|
// Do the request
|
||||||
let response = Request::post(uri)
|
let response = Request::post(uri)
|
||||||
.header("Content-Type", "multipart/form-data; boundary=---------------------------83919643214156711801978607619")
|
.header("Content-Type", "multipart/form-data; boundary=---------------------------83919643214156711801978607619")
|
||||||
.cookie_jar(jar.clone())
|
.cookie_jar(jar)
|
||||||
.body(body)
|
.body(body)
|
||||||
.or_else(|err| Err(format!("Error creating request: {:?}", err)))?
|
.or_else(|err| Err(format!("Error creating request: {:?}", err)))?
|
||||||
.send();
|
.send();
|
||||||
@ -85,6 +85,34 @@ pub async fn create_user_request(url: String, body: String) -> Result<String, St
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn register_courses_request(url: String, body: String) -> Result<String, String> {
|
||||||
|
let classroom_url = std::env::var("CLASSROOM_URL").expect("CLASSROOM_URL env var is not set!");
|
||||||
|
|
||||||
|
ensure_session().await?;
|
||||||
|
|
||||||
|
// Get the stored client
|
||||||
|
let jar = SESSION_COOKIE.read().unwrap().jar.clone();
|
||||||
|
|
||||||
|
let uri = format!("{}{}", classroom_url, url);
|
||||||
|
|
||||||
|
let response = Request::post(uri)
|
||||||
|
.header("Content-Type", "application/x-www-form-urlencoded")
|
||||||
|
.cookie_jar(jar)
|
||||||
|
.body(body)
|
||||||
|
.or_else(|err| Err(format!("Error creating request: {:?}", err)))?
|
||||||
|
.send();
|
||||||
|
|
||||||
|
let mut response = match response {
|
||||||
|
Ok(r) => r,
|
||||||
|
Err(err) => return Err(format!("Error sending request: {:?}", err)),
|
||||||
|
};
|
||||||
|
|
||||||
|
match response.text() {
|
||||||
|
Ok(t) => Ok(t),
|
||||||
|
Err(err) => Err(format!("Error getting text from response: {:?}", err)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Makes sure that the session cookie is set, and that it is valid
|
/// Makes sure that the session cookie is set, and that it is valid
|
||||||
pub async fn ensure_session() -> Result<(), String> {
|
pub async fn ensure_session() -> Result<(), String> {
|
||||||
let last_usage_time = SESSION_TIME.read().unwrap().clone();
|
let last_usage_time = SESSION_TIME.read().unwrap().clone();
|
||||||
|
Loading…
Reference in New Issue
Block a user