[BE] Restore working state
This commit is contained in:
parent
9007994fc1
commit
b7684e193b
@ -17,13 +17,19 @@ pub fn options_delete(_r: i32) -> Status {
|
|||||||
|
|
||||||
#[post("/register/batch", format = "json", data = "<data>")]
|
#[post("/register/batch", format = "json", data = "<data>")]
|
||||||
pub async fn insert_all(data: Json<Vec<RegisterCreate>>) -> (Status, Json<JsonResult<()>>) {
|
pub async fn insert_all(data: Json<Vec<RegisterCreate>>) -> (Status, Json<JsonResult<()>>) {
|
||||||
match RegisterCreate::batch_create(data.0).await {
|
for register_create in data.iter() {
|
||||||
Ok(_) => (Status::Ok, JsonResult::ok(())),
|
let res = register_create.create().await;
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Error creating registers: {}", err);
|
if let Err(err) = res {
|
||||||
(Status::InternalServerError, JsonResult::err(err))
|
eprintln!("Error creating register: {:?}", err);
|
||||||
|
return (
|
||||||
|
Status::InternalServerError,
|
||||||
|
JsonResult::err("Error creando registro".into()),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
(Status::Ok, JsonResult::ok(()))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/register/<dni>")]
|
#[get("/register/<dni>")]
|
||||||
|
@ -16,40 +16,30 @@ pub struct RegisterCreate {
|
|||||||
date: String,
|
date: String,
|
||||||
/// Foreign key to the custom_label table
|
/// Foreign key to the custom_label table
|
||||||
custom_label: String,
|
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,
|
is_preview: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl RegisterCreate {
|
impl RegisterCreate {
|
||||||
pub async fn batch_create(registers: Vec<RegisterCreate>) -> Result<(), String> {
|
pub async fn create(&self) -> Result<(), sqlx::Error> {
|
||||||
let mut transaction = match db().begin().await {
|
let db = db();
|
||||||
Ok(t) => t,
|
|
||||||
Err(err) => {
|
|
||||||
eprintln!("Error starting transaction: {:?}", err);
|
|
||||||
return Err(format!("Error iniciando transaccion."));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
for register in registers.iter() {
|
|
||||||
// Get custom_label_id from db based of self.custom_label
|
// Get custom_label_id from db based of self.custom_label
|
||||||
let custom_label_id = {
|
let custom_label_id = {
|
||||||
if register.custom_label_id > 0 {
|
if self.custom_label.is_empty() {
|
||||||
register.custom_label_id
|
|
||||||
}
|
|
||||||
else if register.custom_label.is_empty() {
|
|
||||||
1
|
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?
|
||||||
}
|
}
|
||||||
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?;
|
let next_register_code = Self::get_next_register_code(self.course_id).await?;
|
||||||
|
|
||||||
// Current date in YYYY-MM-DD format
|
// Current date in YYYY-MM-DD format
|
||||||
let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();
|
let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();
|
||||||
@ -66,15 +56,14 @@ impl RegisterCreate {
|
|||||||
) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||||
next_register_code,
|
next_register_code,
|
||||||
current_date,
|
current_date,
|
||||||
register.date,
|
self.date,
|
||||||
custom_label_id,
|
custom_label_id,
|
||||||
register.is_preview,
|
self.is_preview,
|
||||||
register.person_id,
|
self.person_id,
|
||||||
register.course_id
|
self.course_id
|
||||||
)
|
)
|
||||||
.execute(&mut *transaction)
|
.execute(db)
|
||||||
.await;
|
.await?;
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user