eeg_certs/backend/src/model/register.rs

112 lines
3.4 KiB
Rust
Raw Normal View History

2023-08-30 13:55:05 +00:00
use rocket::{
form::validate::Contains,
serde::{Deserialize, Serialize},
};
2023-08-28 22:28:58 +00:00
2023-08-30 13:55:05 +00:00
use crate::db;
use super::course::Course;
#[derive(Serialize, Deserialize, Clone, Debug)]
2023-08-28 22:28:58 +00:00
#[serde(crate = "rocket::serde")]
/// Represents a single register send by the frontend
/// to create a new register in the database
pub struct RegisterCreate {
person_id: i32,
course_id: i32,
/// YYYY-MM-DD
date: String,
2023-08-28 22:28:58 +00:00
}
#[derive(Serialize, Deserialize, Clone)]
#[serde(crate = "rocket::serde")]
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.
/// 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 register_custom_label: String,
/// Whether this register is a preview of the certificate ("sin firmas")
pub register_is_preview: bool,
2023-08-28 22:28:58 +00:00
/// Foreign key to the person table
pub register_person_id: i32,
/// Foreign key to the course table
pub register_course_id: i32,
}
2023-08-30 13:55:05 +00:00
impl RegisterCreate {
pub async fn create(&self) -> Result<(), sqlx::Error> {
let db = db();
let next_register_code = Self::get_next_register_code(self.course_id).await?;
// Current date in YYYY-MM-DD format
let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();
let _ = sqlx::query!(
"INSERT INTO register (
register_code,
register_creation_date,
register_display_date,
register_custom_label,
register_is_preview,
register_person_id,
register_course_id
) VALUES (?, ?, ?, ?, ?, ?, ?)",
next_register_code,
current_date,
self.date,
"",
false,
self.person_id,
self.course_id
)
.execute(db)
.await?;
Ok(())
}
async fn get_next_register_code(course_id: i32) -> Result<i32, sqlx::Error> {
let db = db();
let course_name = Course::get_course_name(course_id).await;
// All matpel certs share codes
let new_max = if course_name.contains("Matpel") {
let res = sqlx::query!(
"SELECT MAX(register_code) AS max FROM register
WHERE register_course_id IN
(SELECT course_id FROM course WHERE course_name LIKE 'Matpel%')",
)
.fetch_one(db)
.await?;
// If there are no registers, the new code will start from 50, as per the requirements
res.max.unwrap_or(50) + 1
} else {
let res = sqlx::query!(
"SELECT MAX(register_code) AS max FROM register
WHERE register_course_id = ?",
course_id
)
.fetch_one(db)
.await?;
// If there are no registers, the new code will start from 50, as per the requirements
res.max.unwrap_or(50) + 1
};
Ok(new_max)
}
}