From 9007994fc1280af86c5d13f29b318f1330d509f5 Mon Sep 17 00:00:00 2001 From: Araozu Date: Mon, 2 Oct 2023 22:17:51 -0500 Subject: [PATCH] [Broken][BE] Attempt to refactor register creation --- backend/src/controller/register/mod.rs | 25 ++++--- backend/src/model/register.rs | 73 +++++++++++-------- .../certs/NewRegister/CustomLabelSelect.tsx | 6 +- frontend/src/types/Register.ts | 5 ++ 4 files changed, 64 insertions(+), 45 deletions(-) diff --git a/backend/src/controller/register/mod.rs b/backend/src/controller/register/mod.rs index acbb013..b182983 100644 --- a/backend/src/controller/register/mod.rs +++ b/backend/src/controller/register/mod.rs @@ -1,6 +1,9 @@ use rocket::{http::Status, serde::json::Json}; -use crate::{model::register::{Register, RegisterCreate}, json_result::JsonResult}; +use crate::{ + json_result::JsonResult, + model::register::{Register, RegisterCreate}, +}; #[options("/register/batch")] pub fn options() -> Status { @@ -13,17 +16,14 @@ pub fn options_delete(_r: i32) -> Status { } #[post("/register/batch", format = "json", data = "")] -pub async fn insert_all(data: Json>) -> Status { - for register_create in data.iter() { - let res = register_create.create().await; - - if let Err(err) = res { - eprintln!("Error creating register: {}", err); - return Status::InternalServerError; +pub async fn insert_all(data: Json>) -> (Status, Json>) { + match RegisterCreate::batch_create(data.0).await { + Ok(_) => (Status::Ok, JsonResult::ok(())), + Err(err) => { + eprintln!("Error creating registers: {}", err); + (Status::InternalServerError, JsonResult::err(err)) } } - - Status::Ok } #[get("/register/")] @@ -35,7 +35,10 @@ pub async fn get_by_dni(dni: i32) -> (Status, Json>>) { Err(err) => { eprintln!("{:?}", err); - (Status::InternalServerError, JsonResult::err(format!("Error recuperando certs de DB"))) + ( + Status::InternalServerError, + JsonResult::err(format!("Error recuperando certs de DB")), + ) } } } diff --git a/backend/src/model/register.rs b/backend/src/model/register.rs index 4ec2634..3a44cec 100644 --- a/backend/src/model/register.rs +++ b/backend/src/model/register.rs @@ -16,36 +16,46 @@ pub struct RegisterCreate { date: String, /// Foreign key to the custom_label table custom_label: String, + /// Id of the (optional) custom_label. If -1, a new custom_label will be created + /// with the value of self.custom_label + custom_label_id: i32, is_preview: bool, } 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? - } + pub async fn batch_create(registers: Vec) -> Result<(), String> { + let mut transaction = match db().begin().await { + Ok(t) => t, + Err(err) => { + eprintln!("Error starting transaction: {:?}", err); + return Err(format!("Error iniciando transaccion.")); } }; - let next_register_code = Self::get_next_register_code(self.course_id).await?; + for register in registers.iter() { + // Get custom_label_id from db based of self.custom_label + let custom_label_id = { + if register.custom_label_id > 0 { + register.custom_label_id + } + else if register.custom_label.is_empty() { + 1 + } + else { + // Create a new label + CustomLabel::create(®ister.custom_label) + .await + .or_else(|_| Err("Error creando nueva denominacion"))? + } + }; - // Current date in YYYY-MM-DD format - let current_date = chrono::Local::now().format("%Y-%m-%d").to_string(); + let next_register_code = Self::get_next_register_code(register.course_id).await?; - let _ = sqlx::query!( - "INSERT INTO register ( + // 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, @@ -54,16 +64,17 @@ impl RegisterCreate { register_person_id, register_course_id ) VALUES (?, ?, ?, ?, ?, ?, ?)", - next_register_code, - current_date, - self.date, - custom_label_id, - self.is_preview, - self.person_id, - self.course_id - ) - .execute(db) - .await?; + next_register_code, + current_date, + register.date, + custom_label_id, + register.is_preview, + register.person_id, + register.course_id + ) + .execute(&mut *transaction) + .await; + } Ok(()) } diff --git a/frontend/src/certs/NewRegister/CustomLabelSelect.tsx b/frontend/src/certs/NewRegister/CustomLabelSelect.tsx index 9e05272..d3e4cba 100644 --- a/frontend/src/certs/NewRegister/CustomLabelSelect.tsx +++ b/frontend/src/certs/NewRegister/CustomLabelSelect.tsx @@ -70,7 +70,7 @@ export function CustomLabelSelect(props: { ref={inputElement} class={`bg-c-background text-c-on-background border-c-outline border-2 rounded-tl rounded-tr px-2 py-1 - w-full + w-full text-sm invalid:border-c-error invalid:text-c-error focus:border-c-primary outline-none disabled:opacity-50 disabled:cursor-not-allowed`} @@ -85,13 +85,13 @@ export function CustomLabelSelect(props: {
{([, label]) => (