[BE][Certs] Get courses from DB

This commit is contained in:
Araozu 2023-08-30 07:31:42 -05:00
parent 6dc381c0ab
commit ca2d725876
4 changed files with 36 additions and 22 deletions

View File

@ -1,5 +1,7 @@
-- Mysql schema
-- docker run --name eegsac -e MYSQL_ROOT_PASSWORD=123456789 -p 3306:3306 -d mysql:8.0-debian
CREATE TABLE person (
person_id INTEGER PRIMARY KEY AUTO_INCREMENT,
person_dni VARCHAR(8) NOT NULL,

View File

@ -1,25 +1,15 @@
use rocket::serde::json::Json;
use rocket::{http::Status, serde::json::Json};
use crate::model::course::Course;
#[get("/course")]
pub async fn get_all() -> Json<Vec<Course>> {
// TODO: get from database
let c = Course {
course_id: 1,
course_name: "4x4".to_string(),
course_display_name: "Manejo en 4x4 road danger".to_string(),
course_days_amount: 2,
course_has_custom_label: false,
};
pub async fn get_all() -> (Status, Json<Vec<Course>>) {
match Course::get_all().await {
Ok(data) => (Status::Ok, Json(data)),
Err(err) => {
eprintln!("Error getting courses: {}", err);
let c2 = Course {
course_id: 2,
course_name: "Mec. Basica".to_string(),
course_display_name: "Mecánica Básica".to_string(),
course_days_amount: 2,
course_has_custom_label: false,
};
Json(vec![c, c2])
(Status::InternalServerError, Json(vec![]))
}
}
}

View File

@ -1,9 +1,9 @@
use reqwest::Client;
use rocket::http::Status;
use rocket::serde::json::Json;
use reqwest::Client;
use crate::{db, model::person::Person};
use crate::model::reniec_person::ReniecPerson;
use crate::{db, model::person::Person};
#[get("/person/<dni>")]
pub async fn get_by_dni(dni: i32) -> (Status, Json<Person>) {
@ -91,7 +91,6 @@ pub async fn get_by_dni(dni: i32) -> (Status, Json<Person>) {
return (Status::Ok, Json(person));
}
// Return error
(Status::NotFound, Json(Person::default()))
}

View File

@ -1,5 +1,7 @@
use rocket::serde::Serialize;
use crate::db;
#[derive(Serialize, Clone)]
#[serde(crate = "rocket::serde")]
pub struct Course {
@ -22,3 +24,24 @@ pub struct Course {
/// and the custom label the manufacturer and series (i.e. `320D CAT`).
pub course_has_custom_label: bool,
}
impl Course {
pub async fn get_all() -> Result<Vec<Course>, sqlx::Error> {
let db = db();
let results = sqlx::query!("SELECT * FROM course")
.fetch_all(db)
.await?
.iter()
.map(|d| Course {
course_id: d.course_id,
course_name: d.course_name.clone(),
course_display_name: d.course_display_name.clone(),
course_days_amount: d.course_days_amount,
course_has_custom_label: d.course_has_custom_label != 0,
})
.collect();
Ok(results)
}
}