eeg_certs/backend/src/model/register.rs

165 lines
5.1 KiB
Rust
Raw Normal View History

2023-08-30 23:28:11 +00:00
use rocket::form::validate::Contains;
use serde::{Deserialize, Serialize};
2023-08-28 22:28:58 +00:00
2023-08-30 13:55:05 +00:00
use crate::db;
2023-09-03 00:42:43 +00:00
use super::{course::Course, custom_label::CustomLabel};
2023-08-30 13:55:05 +00:00
#[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-09-03 00:42:43 +00:00
/// Foreign key to the custom_label table
custom_label: String,
is_preview: bool,
2023-08-28 22:28:58 +00:00
}
2023-08-30 13:55:05 +00:00
impl RegisterCreate {
2023-10-03 15:07:38 +00:00
pub async fn create(&self) -> Result<(), sqlx::Error> {
let db = db();
2023-09-03 00:42:43 +00:00
2023-10-03 15:07:38 +00:00
// 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?
}
2023-10-03 15:07:38 +00:00
}
};
2023-10-03 15:07:38 +00:00
let next_register_code = Self::get_next_register_code(self.course_id).await?;
2023-08-30 13:55:05 +00:00
2023-10-03 15:07:38 +00:00
// Current date in YYYY-MM-DD format
let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();
2023-08-30 13:55:05 +00:00
2023-10-03 15:07:38 +00:00
let _ = sqlx::query!(
"INSERT INTO register (
2023-08-30 13:55:05 +00:00
register_code,
register_creation_date,
register_display_date,
register_custom_label,
register_is_preview,
register_person_id,
register_course_id
) VALUES (?, ?, ?, ?, ?, ?, ?)",
2023-10-03 15:07:38 +00:00
next_register_code,
current_date,
self.date,
custom_label_id,
self.is_preview,
self.person_id,
self.course_id
)
.execute(db)
.await?;
2023-08-30 13:55:05 +00:00
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=?",
2023-08-30 13:55:05 +00:00
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)
}
}
2023-08-30 20:56:24 +00:00
2023-09-03 00:42:43 +00:00
#[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,
}
2023-08-30 20:56:24 +00:00
impl Register {
pub async fn get_by_dni(dni: String) -> Result<Vec<Register>, sqlx::Error> {
2023-08-30 20:56:24 +00:00
let db = db();
2023-08-30 23:28:11 +00:00
let res = sqlx::query!(
"SELECT * FROM register
WHERE register_person_id = (SELECT person_id FROM person WHERE person_dni = ?)",
dni
)
.fetch_all(db)
.await?
.into_iter()
.map(|r| Register {
register_id: r.register_id,
register_code: r.register_code,
register_creation_date: r.register_creation_date.format("%Y-%m-%d").to_string(),
register_display_date: r.register_display_date.format("%Y-%m-%d").to_string(),
register_custom_label: r.register_custom_label,
register_is_preview: r.register_is_preview == 1,
register_person_id: r.register_person_id,
register_course_id: r.register_course_id,
})
.collect();
2023-08-30 20:56:24 +00:00
Ok(res)
}
2023-09-01 18:01:00 +00:00
pub async fn delete(register_id: i32) -> Result<(), sqlx::Error> {
let db = db();
let _ = sqlx::query!("DELETE FROM register WHERE register_id = ?", register_id)
.execute(db)
.await?;
Ok(())
}
2023-08-30 20:56:24 +00:00
}