[BE] Model and minimal controllers for Person & Course

This commit is contained in:
Araozu 2023-08-25 17:31:46 -05:00
parent a2ad5fde38
commit a4f46c75cf
7 changed files with 69 additions and 5 deletions

View File

@ -0,0 +1,18 @@
use rocket::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_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])
}

View File

@ -1 +1,2 @@
pub mod course;
pub mod person; pub mod person;

View File

@ -1,9 +1,10 @@
use rocket::{http::ContentType, serde::json::Json, Response}; use rocket::serde::json::Json;
use crate::model::person::Person; use crate::model::person::Person;
#[get("/person/<dni>")] #[get("/person/<dni>")]
pub async fn get_by_dni(dni: i32) -> Json<Person> { pub async fn get_by_dni(dni: i32) -> Json<Person> {
// TODO: get from database
Json(Person { Json(Person {
person_id: 1, person_id: 1,
person_dni: format!("{}", dni), person_dni: format!("{}", dni),

View File

@ -37,7 +37,8 @@ impl Fairing for Cors {
fn rocket() -> _ { fn rocket() -> _ {
rocket::build() rocket::build()
.attach(Cors {}) .attach(Cors {})
.mount("/api", routes![controller::person::get_by_dni]) .mount("/api", routes![
controller::person::get_by_dni,
controller::course::get_all,
])
} }

View File

@ -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,
}

View File

@ -1 +1,2 @@
pub mod course;
pub mod person; pub mod person;

View File

@ -1,11 +1,24 @@
use rocket::serde::Serialize; use rocket::serde::Serialize;
#[derive(Serialize)] #[derive(Serialize, Clone)]
#[serde(crate = "rocket::serde")] #[serde(crate = "rocket::serde")]
pub struct Person { pub struct Person {
/// Internal id
pub person_id: i32, pub person_id: i32,
/// Country-specific id. For now only supports Peru's DNI.
///
/// Example: `74185293`
pub person_dni: String, pub person_dni: String,
/// Names
///
/// Example: `Juan Carlos`
pub person_names: String, pub person_names: String,
/// First surname
///
/// Example: `Perez`
pub person_paternal_surname: String, pub person_paternal_surname: String,
/// Second surname
///
/// Example: `Gomez`
pub person_maternal_surname: String, pub person_maternal_surname: String,
} }