From 3734c490845b4bcd5e49f84b9c064d94cea930e1 Mon Sep 17 00:00:00 2001 From: Araozu Date: Wed, 30 Aug 2023 15:56:24 -0500 Subject: [PATCH] [BE] Fix bug in register fetch --- backend/Cargo.lock | 5 +++ backend/Cargo.toml | 2 +- backend/src/controller/register/mod.rs | 31 ++++++------------- backend/src/model/register.rs | 25 +++++++++++++++ .../src/certs/NewRegister/RegisterPreview.tsx | 2 -- frontend/src/certs/index.tsx | 2 +- 6 files changed, 41 insertions(+), 26 deletions(-) diff --git a/backend/Cargo.lock b/backend/Cargo.lock index 9432a5c..1022bdd 100644 --- a/backend/Cargo.lock +++ b/backend/Cargo.lock @@ -1970,6 +1970,7 @@ dependencies = [ "atoi", "byteorder", "bytes", + "chrono", "crc", "crossbeam-queue", "dotenvy", @@ -2034,6 +2035,7 @@ dependencies = [ "sha2", "sqlx-core", "sqlx-mysql", + "sqlx-postgres", "sqlx-sqlite", "syn 1.0.109", "tempfile", @@ -2052,6 +2054,7 @@ dependencies = [ "bitflags 2.4.0", "byteorder", "bytes", + "chrono", "crc", "digest", "dotenvy", @@ -2093,6 +2096,7 @@ dependencies = [ "base64", "bitflags 2.4.0", "byteorder", + "chrono", "crc", "dotenvy", "etcetera", @@ -2129,6 +2133,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "be4c21bf34c7cae5b283efb3ac1bcc7670df7561124dc2f8bdc0b59be40f79a2" dependencies = [ "atoi", + "chrono", "flume", "futures-channel", "futures-core", diff --git a/backend/Cargo.toml b/backend/Cargo.toml index d32e31d..ba7169a 100644 --- a/backend/Cargo.toml +++ b/backend/Cargo.toml @@ -8,7 +8,7 @@ edition = "2021" [dependencies] reqwest = { version = "0.11", features = ["json"] } rocket = { version = "=0.5.0-rc.3" , features = ["json", "msgpack", "uuid"] } -sqlx = { version = "0.7.1", features = [ "runtime-tokio", "tls-rustls", "mysql", "macros" ] } +sqlx = { version = "0.7.1", features = [ "runtime-tokio", "tls-rustls", "mysql", "macros", "chrono" ] } once_cell = "1.18.0" dotenvy = "0.15.7" serde = "1.0.188" diff --git a/backend/src/controller/register/mod.rs b/backend/src/controller/register/mod.rs index af85167..03eaaa1 100644 --- a/backend/src/controller/register/mod.rs +++ b/backend/src/controller/register/mod.rs @@ -22,28 +22,15 @@ pub async fn insert_all(data: Json>) -> Status { } #[get("/register/")] -pub async fn get_by_dni(dni: i32) -> Json> { - let r1 = Register { - register_id: 1, - register_code: 322, - register_creation_date: "2021-01-01".to_string(), - register_display_date: "2021-01-01".to_string(), - register_custom_label: "".to_string(), - register_person_id: 1, - register_course_id: 1, - register_is_preview: false, - }; +pub async fn get_by_dni(dni: i32) -> (Status, Json>) { + let registers = Register::get_by_dni(dni).await; - let r2 = Register { - register_id: 2, - register_code: 323, - register_creation_date: "2021-01-01".to_string(), - register_display_date: "2021-01-03".to_string(), - register_custom_label: "".to_string(), - register_person_id: 1, - register_course_id: 2, - register_is_preview: false, - }; + match registers { + Ok(data) => (Status::Ok, Json(data)), + Err(err) => { + eprintln!("{:?}", err); - Json(vec![r1, r2]) + (Status::InternalServerError, Json(vec![])) + } + } } diff --git a/backend/src/model/register.rs b/backend/src/model/register.rs index 21e18bc..5621b87 100644 --- a/backend/src/model/register.rs +++ b/backend/src/model/register.rs @@ -109,3 +109,28 @@ impl RegisterCreate { Ok(new_max) } } + +impl Register { + pub async fn get_by_dni(dni: i32) -> Result, sqlx::Error> { + let db = db(); + + let res = sqlx::query!("SELECT * FROM register + WHERE register_person_id = (SELECT person_id FROM person WHERE person_dni = ?)", dni) + .fetch_all(db) + .await? + .into_iter() + .map(|r| Register { + register_id: r.register_id, + register_code: r.register_code, + register_creation_date: r.register_creation_date.format("%Y-%m-%d").to_string(), + register_display_date: r.register_display_date.format("%Y-%m-%d").to_string(), + register_custom_label: r.register_custom_label, + register_is_preview: r.register_is_preview == 1, + register_person_id: r.register_person_id, + register_course_id: r.register_course_id, + }) + .collect(); + + Ok(res) + } +} diff --git a/frontend/src/certs/NewRegister/RegisterPreview.tsx b/frontend/src/certs/NewRegister/RegisterPreview.tsx index 8b0f439..c32066d 100644 --- a/frontend/src/certs/NewRegister/RegisterPreview.tsx +++ b/frontend/src/certs/NewRegister/RegisterPreview.tsx @@ -12,8 +12,6 @@ function isoDateToLocalDate(date: string): string { export function RegisterPreview(props: {selections: Array<[number, string]>, personId: number | null, onDelete: (v: number) => void, onRegister: () => void}) { const submit = async() => { - console.log("Submit..."); - const registers: RegisterBatchCreate = props.selections.map(([courseId, date]) => ({ person_id: props.personId!, course_id: courseId, diff --git a/frontend/src/certs/index.tsx b/frontend/src/certs/index.tsx index b9410fe..f867b0b 100644 --- a/frontend/src/certs/index.tsx +++ b/frontend/src/certs/index.tsx @@ -10,7 +10,7 @@ export function Certs() { return (
- {}} /> + {}} />
);