diff --git a/backend/src/controller/register/mod.rs b/backend/src/controller/register/mod.rs index b182983..844ab78 100644 --- a/backend/src/controller/register/mod.rs +++ b/backend/src/controller/register/mod.rs @@ -17,13 +17,19 @@ pub fn options_delete(_r: i32) -> Status { #[post("/register/batch", format = "json", data = "")] 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)) + 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, + JsonResult::err("Error creando registro".into()), + ); } } + + (Status::Ok, JsonResult::ok(())) } #[get("/register/")] diff --git a/backend/src/model/register.rs b/backend/src/model/register.rs index 3a44cec..4ec2634 100644 --- a/backend/src/model/register.rs +++ b/backend/src/model/register.rs @@ -16,46 +16,36 @@ 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 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.")); + 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? + } } }; - 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"))? - } - }; + let next_register_code = Self::get_next_register_code(self.course_id).await?; - let next_register_code = Self::get_next_register_code(register.course_id).await?; + // Current date in YYYY-MM-DD format + let current_date = chrono::Local::now().format("%Y-%m-%d").to_string(); - // 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 ( + let _ = sqlx::query!( + "INSERT INTO register ( register_code, register_creation_date, register_display_date, @@ -64,17 +54,16 @@ impl RegisterCreate { register_person_id, register_course_id ) VALUES (?, ?, ?, ?, ?, ?, ?)", - next_register_code, - current_date, - register.date, - custom_label_id, - register.is_preview, - register.person_id, - register.course_id - ) - .execute(&mut *transaction) - .await; - } + next_register_code, + current_date, + self.date, + custom_label_id, + self.is_preview, + self.person_id, + self.course_id + ) + .execute(db) + .await?; Ok(()) }