[BE] Get & create custom_labels

This commit is contained in:
Araozu 2023-09-02 19:42:43 -05:00
parent c6b77b446d
commit 77bd3ba00f
7 changed files with 129 additions and 36 deletions

View File

@ -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<Vec<CustomLabel>>) {
match CustomLabel::get_all().await {
Ok(data) => (Status::Ok, Json(data)),
Err(err) => {
eprintln!("Error getting custom labels: {}", err);
(Status::InternalServerError, Json(vec![]))
}
}
}

View File

@ -1,3 +1,4 @@
pub mod course;
pub mod custom_label;
pub mod person;
pub mod register;

View File

@ -48,6 +48,7 @@ async fn rocket() -> _ {
controller::register::options_delete,
controller::register::get_by_dni,
controller::register::delete,
controller::custom_label::get_all,
],
)
}

View File

@ -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<Vec<CustomLabel>, 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<i32, sqlx::Error> {
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<i32, sqlx::Error> {
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)
}
}

View File

@ -1,4 +1,5 @@
pub mod course;
pub mod custom_label;
pub mod person;
pub mod register;
pub mod reniec_person;

View File

@ -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,11 +48,13 @@ 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 (?, ?, ?, ?)",
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)
self.person_maternal_surname
)
.execute(db)
.await?;

View File

@ -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<Vec<Register>, sqlx::Error> {
let db = db();
@ -143,4 +161,3 @@ impl Register {
Ok(())
}
}