[BE] Fix bug in register fetch

This commit is contained in:
Araozu 2023-08-30 15:56:24 -05:00
parent a0e15e5bfd
commit 3734c49084
6 changed files with 41 additions and 26 deletions

5
backend/Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -22,28 +22,15 @@ pub async fn insert_all(data: Json<Vec<RegisterCreate>>) -> Status {
}
#[get("/register/<dni>")]
pub async fn get_by_dni(dni: i32) -> Json<Vec<Register>> {
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<Vec<Register>>) {
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![]))
}
}
}

View File

@ -109,3 +109,28 @@ impl RegisterCreate {
Ok(new_max)
}
}
impl Register {
pub async fn get_by_dni(dni: i32) -> Result<Vec<Register>, 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)
}
}

View File

@ -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,

View File

@ -10,7 +10,7 @@ export function Certs() {
return (
<div class="grid grid-cols-[16rem_25rem_1fr]">
<Search setPerson={setPerson} />
<NewRegister personId={0} onSuccess={() => {}} />
<NewRegister personId={person()?.person_id ?? -1} onSuccess={() => {}} />
<Registers person={person()} />
</div>
);