[Broken][BE] Attempt to refactor register creation
This commit is contained in:
parent
659f2483fb
commit
9007994fc1
@ -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 = "<data>")]
|
||||
pub async fn insert_all(data: Json<Vec<RegisterCreate>>) -> 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<Vec<RegisterCreate>>) -> (Status, Json<JsonResult<()>>) {
|
||||
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/<dni>")]
|
||||
@ -35,7 +35,10 @@ pub async fn get_by_dni(dni: i32) -> (Status, Json<JsonResult<Vec<Register>>>) {
|
||||
Err(err) => {
|
||||
eprintln!("{:?}", err);
|
||||
|
||||
(Status::InternalServerError, JsonResult::err(format!("Error recuperando certs de DB")))
|
||||
(
|
||||
Status::InternalServerError,
|
||||
JsonResult::err(format!("Error recuperando certs de DB")),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -16,30 +16,40 @@ 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<RegisterCreate>) -> 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"))?
|
||||
}
|
||||
};
|
||||
|
||||
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();
|
||||
@ -56,14 +66,15 @@ impl RegisterCreate {
|
||||
) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
next_register_code,
|
||||
current_date,
|
||||
self.date,
|
||||
register.date,
|
||||
custom_label_id,
|
||||
self.is_preview,
|
||||
self.person_id,
|
||||
self.course_id
|
||||
register.is_preview,
|
||||
register.person_id,
|
||||
register.course_id
|
||||
)
|
||||
.execute(db)
|
||||
.await?;
|
||||
.execute(&mut *transaction)
|
||||
.await;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
@ -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: {
|
||||
<br />
|
||||
<div
|
||||
class="border-c-outline border-l-2 border-b-2 border-r-2
|
||||
rounded-bl rounded-br overflow-y-scroll h-[4rem]
|
||||
rounded-bl rounded-br overflow-y-scroll h-[6rem]
|
||||
absolute w-full"
|
||||
>
|
||||
<For each={filteredOptions()}>
|
||||
{([, label]) => (
|
||||
<button
|
||||
class="w-full text-left py-1 px-2
|
||||
class="w-full text-left py-1 px-2 text-sm
|
||||
hover:bg-c-primary-container hover:text-c-on-primary-container"
|
||||
onclick={(ev) => {
|
||||
ev.preventDefault();
|
||||
|
@ -9,6 +9,11 @@ export type RegisterBatchCreate = Array<{
|
||||
* Value of the custom label
|
||||
*/
|
||||
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: number,
|
||||
is_preview: boolean,
|
||||
}>
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user