From 77bd3ba00f46b78bbd4b745549651cd3e65e23b0 Mon Sep 17 00:00:00 2001 From: Araozu Date: Sat, 2 Sep 2023 19:42:43 -0500 Subject: [PATCH] [BE] Get & create custom_labels --- backend/src/controller/custom_label/mod.rs | 15 +++++ backend/src/controller/mod.rs | 1 + backend/src/main.rs | 1 + backend/src/model/custom_label.rs | 59 ++++++++++++++++++ backend/src/model/mod.rs | 1 + backend/src/model/person.rs | 19 +++--- backend/src/model/register.rs | 69 ++++++++++++++-------- 7 files changed, 129 insertions(+), 36 deletions(-) create mode 100644 backend/src/controller/custom_label/mod.rs create mode 100644 backend/src/model/custom_label.rs diff --git a/backend/src/controller/custom_label/mod.rs b/backend/src/controller/custom_label/mod.rs new file mode 100644 index 0000000..bd16a31 --- /dev/null +++ b/backend/src/controller/custom_label/mod.rs @@ -0,0 +1,15 @@ +use rocket::{http::Status, serde::json::Json}; + +use crate::model::custom_label::CustomLabel; + +#[get("/label")] +pub async fn get_all() -> (Status, Json>) { + match CustomLabel::get_all().await { + Ok(data) => (Status::Ok, Json(data)), + Err(err) => { + eprintln!("Error getting custom labels: {}", err); + + (Status::InternalServerError, Json(vec![])) + } + } +} diff --git a/backend/src/controller/mod.rs b/backend/src/controller/mod.rs index 256ec51..2e8eba9 100644 --- a/backend/src/controller/mod.rs +++ b/backend/src/controller/mod.rs @@ -1,3 +1,4 @@ pub mod course; +pub mod custom_label; pub mod person; pub mod register; diff --git a/backend/src/main.rs b/backend/src/main.rs index 96b98ba..d924bc1 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -48,6 +48,7 @@ async fn rocket() -> _ { controller::register::options_delete, controller::register::get_by_dni, controller::register::delete, + controller::custom_label::get_all, ], ) } diff --git a/backend/src/model/custom_label.rs b/backend/src/model/custom_label.rs new file mode 100644 index 0000000..5cba492 --- /dev/null +++ b/backend/src/model/custom_label.rs @@ -0,0 +1,59 @@ +use crate::db; +use serde::{Deserialize, Serialize}; +use sqlx::Row; + +#[derive(sqlx::FromRow, Serialize, Deserialize)] +pub struct CustomLabel { + custom_label_id: i32, + custom_label_value: String, +} + +impl CustomLabel { + pub async fn get_all() -> Result, sqlx::Error> { + let db = db(); + + let result = sqlx::query_as::<_, CustomLabel>( + r#" + SELECT custom_label_id, custom_label_value + FROM custom_label + "#, + ) + .fetch_all(db) + .await?; + + Ok(result) + } + + pub async fn get_id_by_value(value: &String) -> Result { + let db = db(); + + let result = sqlx::query!( + "SELECT custom_label_id FROM custom_label WHERE custom_label_value = ?", + value + ) + .fetch_all(db) + .await?; + + if result.is_empty() { + Ok(-1) + } else { + let id = &result[0].custom_label_id; + + Ok(*id) + } + } + + pub async fn create(value: &String) -> Result { + let db = db(); + + sqlx::query!( + "INSERT INTO custom_label (custom_label_value) VALUES (?)", + value + ) + .execute(db) + .await?; + let result = Self::get_id_by_value(value).await?; + + Ok(result) + } +} diff --git a/backend/src/model/mod.rs b/backend/src/model/mod.rs index 2cff971..929d84e 100644 --- a/backend/src/model/mod.rs +++ b/backend/src/model/mod.rs @@ -1,4 +1,5 @@ pub mod course; +pub mod custom_label; pub mod person; pub mod register; pub mod reniec_person; diff --git a/backend/src/model/person.rs b/backend/src/model/person.rs index ae6990f..1ff254e 100644 --- a/backend/src/model/person.rs +++ b/backend/src/model/person.rs @@ -1,4 +1,4 @@ -use serde::{Serialize, Deserialize}; +use serde::{Deserialize, Serialize}; use crate::db; @@ -36,9 +36,6 @@ impl Person { } } - - - #[derive(Deserialize)] pub struct PersonCreate { pub person_dni: String, @@ -51,14 +48,16 @@ impl PersonCreate { pub async fn create(&self) -> Result<(), sqlx::Error> { let db = db(); - sqlx::query!("INSERT INTO person (person_dni, person_names, person_paternal_surname, person_maternal_surname) VALUES (?, ?, ?, ?)", - self.person_dni, - self.person_names, - self.person_paternal_surname, - self.person_maternal_surname) + sqlx::query!( + "INSERT INTO person (person_dni, person_names, person_paternal_surname, person_maternal_surname) VALUES (?, ?, ?, ?)", + self.person_dni, + self.person_names, + self.person_paternal_surname, + self.person_maternal_surname + ) .execute(db) .await?; - + Ok(()) } } diff --git a/backend/src/model/register.rs b/backend/src/model/register.rs index 267b121..4138827 100644 --- a/backend/src/model/register.rs +++ b/backend/src/model/register.rs @@ -3,7 +3,7 @@ use serde::{Deserialize, Serialize}; use crate::db; -use super::course::Course; +use super::{course::Course, custom_label::CustomLabel}; #[derive(Serialize, Deserialize, Clone, Debug)] #[serde(crate = "rocket::serde")] @@ -14,35 +14,30 @@ pub struct RegisterCreate { course_id: i32, /// YYYY-MM-DD date: String, -} - -#[derive(Serialize, Deserialize, Clone)] -pub struct Register { - pub register_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 register_code: i32, - /// Date of creation of the register, in YYYY-MM-DD format - pub register_creation_date: String, - /// Date shown on the certificate, in YYYY-MM-DD format - pub register_display_date: String, - /// Some course's names can be extended with a label. - /// Points to a custom_label entity - pub register_custom_label: i32, - /// Whether this register is a preview of the certificate ("sin firmas") - pub register_is_preview: bool, - /// Foreign key to the person table - pub register_person_id: i32, - /// Foreign key to the course table - pub register_course_id: i32, + /// Foreign key to the custom_label table + custom_label: String, } impl RegisterCreate { pub async fn create(&self) -> Result<(), sqlx::Error> { let db = db(); + // Get custom_label_id from db based of self.custom_label + let custom_label_id = { + if self.custom_label.is_empty() { + 1 + } else { + // Get custom_label_id from db based of self.custom_label + let id = CustomLabel::get_id_by_value(&self.custom_label).await?; + + if id > 0 { + id + } else { + CustomLabel::create(&self.custom_label).await? + } + } + }; + let next_register_code = Self::get_next_register_code(self.course_id).await?; // Current date in YYYY-MM-DD format @@ -61,7 +56,7 @@ impl RegisterCreate { next_register_code, current_date, self.date, - 1, + custom_label_id, false, self.person_id, self.course_id @@ -106,6 +101,29 @@ impl RegisterCreate { } } +#[derive(Serialize, Deserialize, Clone)] +pub struct Register { + pub register_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 register_code: i32, + /// Date of creation of the register, in YYYY-MM-DD format + pub register_creation_date: String, + /// Date shown on the certificate, in YYYY-MM-DD format + pub register_display_date: String, + /// Some course's names can be extended with a label. + /// Points to a custom_label entity + pub register_custom_label: i32, + /// Whether this register is a preview of the certificate ("sin firmas") + pub register_is_preview: bool, + /// Foreign key to the person table + pub register_person_id: i32, + /// Foreign key to the course table + pub register_course_id: i32, +} + impl Register { pub async fn get_by_dni(dni: i32) -> Result, sqlx::Error> { let db = db(); @@ -143,4 +161,3 @@ impl Register { Ok(()) } } -