From 81157d53de1eba2fdc98cdbc6cb440f2ee25d2d7 Mon Sep 17 00:00:00 2001 From: Araozu Date: Mon, 28 Aug 2023 17:28:58 -0500 Subject: [PATCH] [Certs] FE & BE Mock for registers --- backend/src/controller/course/mod.rs | 11 +++++- backend/src/controller/mod.rs | 1 + backend/src/controller/register/mod.rs | 38 +++++++++++++++++++ backend/src/main.rs | 3 ++ backend/src/model/course.rs | 5 --- backend/src/model/mod.rs | 1 + backend/src/model/register.rs | 36 ++++++++++++++++++ frontend/.env.local | 2 + frontend/src/App.tsx | 3 ++ .../certs/NewRegister/ManualRegistration.tsx | 33 ++++++++-------- .../src/certs/NewRegister/RegisterPreview.tsx | 34 ++++++++--------- frontend/src/certs/Registers.tsx | 25 ------------ frontend/src/certs/Registers/index.tsx | 31 +++++++++++++++ frontend/src/types/Register.ts | 29 ++++++++++++++ 14 files changed, 186 insertions(+), 66 deletions(-) create mode 100644 backend/src/controller/register/mod.rs create mode 100644 backend/src/model/register.rs create mode 100644 frontend/.env.local delete mode 100644 frontend/src/certs/Registers.tsx create mode 100644 frontend/src/certs/Registers/index.tsx create mode 100644 frontend/src/types/Register.ts diff --git a/backend/src/controller/course/mod.rs b/backend/src/controller/course/mod.rs index 0c4dc7a..b98846a 100644 --- a/backend/src/controller/course/mod.rs +++ b/backend/src/controller/course/mod.rs @@ -7,12 +7,19 @@ pub async fn get_all() -> Json> { // TODO: get from database let c = Course { course_id: 1, - course_code: 322, course_name: "4x4".to_string(), course_display_name: "Manejo en 4x4 road danger".to_string(), course_days_amount: 2, course_has_custom_label: false, }; - Json(vec![c.clone(), c]) + let c2 = Course { + course_id: 2, + course_name: "Mec. Basica".to_string(), + course_display_name: "Mecánica Básica".to_string(), + course_days_amount: 2, + course_has_custom_label: false, + }; + + Json(vec![c, c2]) } diff --git a/backend/src/controller/mod.rs b/backend/src/controller/mod.rs index 7f554cd..256ec51 100644 --- a/backend/src/controller/mod.rs +++ b/backend/src/controller/mod.rs @@ -1,2 +1,3 @@ pub mod course; pub mod person; +pub mod register; diff --git a/backend/src/controller/register/mod.rs b/backend/src/controller/register/mod.rs new file mode 100644 index 0000000..98c15b1 --- /dev/null +++ b/backend/src/controller/register/mod.rs @@ -0,0 +1,38 @@ +use rocket::{serde::json::Json, http::Status}; + +use crate::model::register::{RegisterCreate, Register}; + +#[options("/register/batch")] +pub fn options() -> Status { + Status::Ok +} + +#[post("/register/batch", format = "json", data = "")] +pub async fn insert_all(data: Json>) -> Status { + Status::Ok +} + +#[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, + }; + + 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, + }; + + Json(vec![r1, r2]) +} diff --git a/backend/src/main.rs b/backend/src/main.rs index c1a8a9a..0c72325 100644 --- a/backend/src/main.rs +++ b/backend/src/main.rs @@ -40,5 +40,8 @@ fn rocket() -> _ { .mount("/api", routes![ controller::person::get_by_dni, controller::course::get_all, + controller::register::insert_all, + controller::register::options, + controller::register::get_by_dni, ]) } diff --git a/backend/src/model/course.rs b/backend/src/model/course.rs index bc5261b..bed1504 100644 --- a/backend/src/model/course.rs +++ b/backend/src/model/course.rs @@ -4,11 +4,6 @@ use rocket::serde::Serialize; #[serde(crate = "rocket::serde")] pub struct Course { pub course_id: i32, - /// Display code, shown on the certificate. - /// Currently a 4-digit number. After 9999, it will be a 4-digit hex number. - /// - /// Example: `0322` - pub course_code: i32, /// Internal name, shown in the admin panel /// /// Example: `4x4` diff --git a/backend/src/model/mod.rs b/backend/src/model/mod.rs index 7f554cd..256ec51 100644 --- a/backend/src/model/mod.rs +++ b/backend/src/model/mod.rs @@ -1,2 +1,3 @@ pub mod course; pub mod person; +pub mod register; diff --git a/backend/src/model/register.rs b/backend/src/model/register.rs new file mode 100644 index 0000000..93a372b --- /dev/null +++ b/backend/src/model/register.rs @@ -0,0 +1,36 @@ +use rocket::serde::{Serialize, Deserialize}; + +#[derive(Serialize, Deserialize, Clone)] +#[serde(crate = "rocket::serde")] +/// Represents a single register send by the frontend +/// to create a new register in the database +pub struct RegisterCreate { + person_id: i32, + course_id: i32, + /// YYYY-MM-DD + date: String +} + + +#[derive(Serialize, Deserialize, Clone)] +#[serde(crate = "rocket::serde")] +pub struct Register { + pub register_id: i32, + /// Display code, shown on the certificate. + /// Currently a 4-digit number. After 9999, it will be a 4-digit hex number. + /// + /// Example: `0322`` + pub register_code: i32, + /// Date of creation of the register, in YYYY-MM-DD format + pub register_creation_date: String, + /// Date shown on the certificate, in YYYY-MM-DD format + pub register_display_date: String, + /// Some course's names can be extended with a label. + /// Used in machinery courses, where the course name is the type of machinery, + /// and the custom label the manufacturer and series (i.e. `320D CAT`). + pub register_custom_label: String, + /// Foreign key to the person table + pub register_person_id: i32, + /// Foreign key to the course table + pub register_course_id: i32, +} diff --git a/frontend/.env.local b/frontend/.env.local new file mode 100644 index 0000000..03ceebb --- /dev/null +++ b/frontend/.env.local @@ -0,0 +1,2 @@ +VITE_BACKEND_URL=http://localhost:8000 + diff --git a/frontend/src/App.tsx b/frontend/src/App.tsx index b82579f..84e8728 100644 --- a/frontend/src/App.tsx +++ b/frontend/src/App.tsx @@ -7,7 +7,10 @@ const App: Component = () => (
+

En construccion

} /> +

En construccion

} /> +

En construccion

} />
); diff --git a/frontend/src/certs/NewRegister/ManualRegistration.tsx b/frontend/src/certs/NewRegister/ManualRegistration.tsx index 8b7ef7b..469ba80 100644 --- a/frontend/src/certs/NewRegister/ManualRegistration.tsx +++ b/frontend/src/certs/NewRegister/ManualRegistration.tsx @@ -67,7 +67,6 @@ export function ManualRegistration(props: {personId: number | null, onAdd: (v: [
- - +
-

- {error()}  -

+ + + +

+ {error()}  +

+
+ ); } diff --git a/frontend/src/certs/NewRegister/RegisterPreview.tsx b/frontend/src/certs/NewRegister/RegisterPreview.tsx index 7212af5..8b0f439 100644 --- a/frontend/src/certs/NewRegister/RegisterPreview.tsx +++ b/frontend/src/certs/NewRegister/RegisterPreview.tsx @@ -2,6 +2,7 @@ import { FilledCard } from "../../components/FilledCard"; import { For } from "solid-js"; import { XIcon } from "../../icons/XIcon"; import { allCourses } from "../../utils/allCourses"; +import { RegisterBatchCreate } from "../../types/Register"; function isoDateToLocalDate(date: string): string { @@ -13,19 +14,18 @@ export function RegisterPreview(props: {selections: Array<[number, string]>, per const submit = async() => { console.log("Submit..."); - // TODO: Send all register requests at once - for (const [courseId, date] of props.selections) { - const result = await defaultNewRegisterFn( - props.personId ?? -1, - courseId, - date, - ); + const registers: RegisterBatchCreate = props.selections.map(([courseId, date]) => ({ + person_id: props.personId!, + course_id: courseId, + date, + })); - if (result === null) { - console.log("Success"); - } else { - console.log(`error. ${result}`); - } + const result = await createRegisters(registers); + + if (result === null) { + console.log("Success"); + } else { + console.log(`error. ${result}`); } props.onRegister(); @@ -74,17 +74,13 @@ function Register(props: {courseId: number, date: string, onDelete: (v: number) } -async function defaultNewRegisterFn(personId: number, subjectId: number, date: string): Promise { - const response = await fetch("/certificate", { +async function createRegisters(data: RegisterBatchCreate): Promise { + const response = await fetch(`${import.meta.env.VITE_BACKEND_URL}/api/register/batch`, { method: "POST", headers: { "Content-Type": "application/json", }, - body: JSON.stringify({ - personId, - subjectId, - date, - }), + body: JSON.stringify(data), }); if (response.ok) { diff --git a/frontend/src/certs/Registers.tsx b/frontend/src/certs/Registers.tsx deleted file mode 100644 index fbf2075..0000000 --- a/frontend/src/certs/Registers.tsx +++ /dev/null @@ -1,25 +0,0 @@ -import { DownloadIcon } from "../icons/DownloadIcon"; - -export function Registers() { - return ( -
-

- SUMA BERNAL, MAIKOL -

- -
-
- -
-

Matpel 2

-

12/08/2023 - 6486

-
-
- - -
-
- ); -} diff --git a/frontend/src/certs/Registers/index.tsx b/frontend/src/certs/Registers/index.tsx new file mode 100644 index 0000000..d0c292d --- /dev/null +++ b/frontend/src/certs/Registers/index.tsx @@ -0,0 +1,31 @@ +import { DownloadIcon } from "../../icons/DownloadIcon"; +import { Register } from "../../types/Register"; + +export function Registers() { + return ( +
+

+ SUMA BERNAL, MAIKOL +

+
+ + + +
+
+ ); +} + +function RegisterEl(props: {register: Register}) { + return ( +
+ +
+

Matpel 2

+

12/08/2023 - 6486

+
+
+ ); +} diff --git a/frontend/src/types/Register.ts b/frontend/src/types/Register.ts new file mode 100644 index 0000000..21846b3 --- /dev/null +++ b/frontend/src/types/Register.ts @@ -0,0 +1,29 @@ +export type RegisterBatchCreate = Array<{ + person_id: number, + course_id: number, + /** + * YYYY-MM-DD + */ + date: string, +}> + +export type Register = { + register_id: number, + /// Display code, shown on the certificate. + /// Currently a 4-digit number. After 9999, it will be a 4-digit hex number. + /// + /// Example: `0322`` + register_code: number, + /// Date of creation of the register, in YYYY-MM-DD format + register_creation_date: string, + /// Date shown on the certificate, in YYYY-MM-DD format + register_display_date: string, + /// Some course's names can be extended with a label. + /// Used in machinery courses, where the course name is the type of machinery, + /// and the custom label the manufacturer and series (i.e. `320D CAT`). + register_custom_label: string, + /// Foreign key to the person table + register_person_id: number, + /// Foreign key to the course table + register_course_id: number, +}