[BE] Restore working state

master
Araozu 2023-10-03 10:07:38 -05:00
parent 9007994fc1
commit b7684e193b
2 changed files with 42 additions and 47 deletions

View File

@ -17,13 +17,19 @@ pub fn options_delete(_r: i32) -> Status {
#[post("/register/batch", format = "json", data = "<data>")]
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))
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/<dni>")]

View File

@ -16,40 +16,30 @@ 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<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."));
}
};
pub async fn create(&self) -> Result<(), sqlx::Error> {
let db = db();
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() {
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?
}
else {
// Create a new label
CustomLabel::create(&register.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
let current_date = chrono::Local::now().format("%Y-%m-%d").to_string();
@ -66,15 +56,14 @@ impl RegisterCreate {
) VALUES (?, ?, ?, ?, ?, ?, ?)",
next_register_code,
current_date,
register.date,
self.date,
custom_label_id,
register.is_preview,
register.person_id,
register.course_id
self.is_preview,
self.person_id,
self.course_id
)
.execute(&mut *transaction)
.await;
}
.execute(db)
.await?;
Ok(())
}