From a4f46c75cf71238bdeac17afeaef5d9538997b0e Mon Sep 17 00:00:00 2001 From: Araozu Date: Fri, 25 Aug 2023 17:31:46 -0500 Subject: [PATCH] [BE] Model and minimal controllers for Person & Course --- backend/src/controller/course/mod.rs | 18 +++++++++++++++++ backend/src/controller/mod.rs | 1 + backend/src/controller/person/mod.rs | 3 ++- backend/src/main.rs | 7 ++++--- backend/src/model/course.rs | 29 ++++++++++++++++++++++++++++ backend/src/model/mod.rs | 1 + backend/src/model/person.rs | 15 +++++++++++++- 7 files changed, 69 insertions(+), 5 deletions(-) create mode 100644 backend/src/controller/course/mod.rs create mode 100644 backend/src/model/course.rs diff --git a/backend/src/controller/course/mod.rs b/backend/src/controller/course/mod.rs new file mode 100644 index 0000000..0c4dc7a --- /dev/null +++ b/backend/src/controller/course/mod.rs @@ -0,0 +1,18 @@ +use rocket::serde::json::Json; + +use crate::model::course::Course; + +#[get("/course")] +pub async fn get_all() -> Json> { + // TODO: get from database + let c = Course { + course_id: 1, + course_code: 322, + course_name: "4x4".to_string(), + course_display_name: "Manejo en 4x4 road danger".to_string(), + course_days_amount: 2, + course_has_custom_label: false, + }; + + Json(vec![c.clone(), c]) +} diff --git a/backend/src/controller/mod.rs b/backend/src/controller/mod.rs index 4a45102..7f554cd 100644 --- a/backend/src/controller/mod.rs +++ b/backend/src/controller/mod.rs @@ -1 +1,2 @@ +pub mod course; pub mod person; diff --git a/backend/src/controller/person/mod.rs b/backend/src/controller/person/mod.rs index 275782f..7f8d288 100644 --- a/backend/src/controller/person/mod.rs +++ b/backend/src/controller/person/mod.rs @@ -1,9 +1,10 @@ -use rocket::{http::ContentType, serde::json::Json, Response}; +use rocket::serde::json::Json; use crate::model::person::Person; #[get("/person/")] pub async fn get_by_dni(dni: i32) -> Json { + // TODO: get from database Json(Person { person_id: 1, person_dni: format!("{}", dni), diff --git a/backend/src/main.rs b/backend/src/main.rs index 6e80644..c1a8a9a 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -37,7 +37,8 @@ impl Fairing for Cors { fn rocket() -> _ { rocket::build() .attach(Cors {}) - .mount("/api", routes![controller::person::get_by_dni]) + .mount("/api", routes![ + controller::person::get_by_dni, + controller::course::get_all, + ]) } - - diff --git a/backend/src/model/course.rs b/backend/src/model/course.rs new file mode 100644 index 0000000..bc5261b --- /dev/null +++ b/backend/src/model/course.rs @@ -0,0 +1,29 @@ +use rocket::serde::Serialize; + +#[derive(Serialize, Clone)] +#[serde(crate = "rocket::serde")] +pub struct Course { + pub course_id: i32, + /// Display code, shown on the certificate. + /// Currently a 4-digit number. After 9999, it will be a 4-digit hex number. + /// + /// Example: `0322` + pub course_code: i32, + /// Internal name, shown in the admin panel + /// + /// Example: `4x4` + pub course_name: String, + /// Display name, partially shown on the certificate & public page + /// + /// Example: `Manejo en 4x4 road danger` + pub course_display_name: String, + /// Number of days of the course. + /// Used to calculate how to space the courses + /// + /// Example: `2` + pub course_days_amount: i32, + /// Whether the course name can be extended with a label. + /// Used in machinery courses, where the course name is the type of machinery, + /// and the custom label the manufacturer and series (i.e. `320D CAT`). + pub course_has_custom_label: bool, +} diff --git a/backend/src/model/mod.rs b/backend/src/model/mod.rs index 4a45102..7f554cd 100644 --- a/backend/src/model/mod.rs +++ b/backend/src/model/mod.rs @@ -1 +1,2 @@ +pub mod course; pub mod person; diff --git a/backend/src/model/person.rs b/backend/src/model/person.rs index 7c34cdf..c662c5f 100644 --- a/backend/src/model/person.rs +++ b/backend/src/model/person.rs @@ -1,11 +1,24 @@ use rocket::serde::Serialize; -#[derive(Serialize)] +#[derive(Serialize, Clone)] #[serde(crate = "rocket::serde")] pub struct Person { + /// Internal id pub person_id: i32, + /// Country-specific id. For now only supports Peru's DNI. + /// + /// Example: `74185293` pub person_dni: String, + /// Names + /// + /// Example: `Juan Carlos` pub person_names: String, + /// First surname + /// + /// Example: `Perez` pub person_paternal_surname: String, + /// Second surname + /// + /// Example: `Gomez` pub person_maternal_surname: String, }