Add axios. Refactor search person
This commit is contained in:
parent
3d60f11f43
commit
943875bd0d
1
backend/Cargo.lock
generated
1
backend/Cargo.lock
generated
@ -132,6 +132,7 @@ dependencies = [
|
|||||||
"dotenvy",
|
"dotenvy",
|
||||||
"isahc",
|
"isahc",
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
|
"once_cell",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"rocket",
|
"rocket",
|
||||||
"scraper",
|
"scraper",
|
||||||
|
@ -16,5 +16,6 @@ scraper = "0.17.1"
|
|||||||
isahc = { version = "1.7.2", features = ["cookies"] }
|
isahc = { version = "1.7.2", features = ["cookies"] }
|
||||||
urlencoding = "2.1.3"
|
urlencoding = "2.1.3"
|
||||||
lazy_static = "1.4.0"
|
lazy_static = "1.4.0"
|
||||||
|
once_cell = "1.18.0"
|
||||||
|
|
||||||
|
|
||||||
|
@ -1,13 +1,16 @@
|
|||||||
|
use std::fmt::format;
|
||||||
|
|
||||||
use reqwest::Client;
|
use reqwest::Client;
|
||||||
use rocket::http::Status;
|
use rocket::http::Status;
|
||||||
use rocket::serde::json::Json;
|
use rocket::serde::json::Json;
|
||||||
|
|
||||||
|
use crate::json_result::JsonResult;
|
||||||
use crate::model::person::PersonCreate;
|
use crate::model::person::PersonCreate;
|
||||||
use crate::model::reniec_person::ReniecPerson;
|
use crate::model::reniec_person::ReniecPerson;
|
||||||
use crate::{db, model::person::Person};
|
use crate::{db, model::person::Person};
|
||||||
|
|
||||||
#[get("/person/<dni>")]
|
#[get("/person/<dni>")]
|
||||||
pub async fn get_by_dni(dni: i32) -> (Status, Json<Person>) {
|
pub async fn get_by_dni(dni: i32) -> (Status, Json<JsonResult<Person>>) {
|
||||||
let db = db();
|
let db = db();
|
||||||
|
|
||||||
// TODO: move db logic to model
|
// TODO: move db logic to model
|
||||||
@ -29,7 +32,7 @@ pub async fn get_by_dni(dni: i32) -> (Status, Json<Person>) {
|
|||||||
person_classroom_id: None,
|
person_classroom_id: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
return (Status::Ok, Json(person));
|
return (Status::Ok, JsonResult::ok(person));
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -67,33 +70,80 @@ pub async fn get_by_dni(dni: i32) -> (Status, Json<Person>) {
|
|||||||
|
|
||||||
// If person is found in fake RENIEC API, insert to DB and return
|
// If person is found in fake RENIEC API, insert to DB and return
|
||||||
if let Some(p) = reniec_person {
|
if let Some(p) = reniec_person {
|
||||||
let query = sqlx::query!(
|
// Insert person into DB and get last inserted id
|
||||||
|
let mut tx = match db.begin().await {
|
||||||
|
Ok(t) => t,
|
||||||
|
Err(err) => {
|
||||||
|
eprintln!("Error starting transaction: {:?}", err);
|
||||||
|
return (
|
||||||
|
Status::InternalServerError,
|
||||||
|
JsonResult::err(format!("Error iniciando transaccion.")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let query_1 = sqlx::query!(
|
||||||
"INSERT INTO person (person_dni, person_names, person_paternal_surname, person_maternal_surname) VALUES (?, ?, ?, ?)",
|
"INSERT INTO person (person_dni, person_names, person_paternal_surname, person_maternal_surname) VALUES (?, ?, ?, ?)",
|
||||||
dni,
|
dni,
|
||||||
p.nombres,
|
p.nombres,
|
||||||
p.apellidoPaterno,
|
p.apellidoPaterno,
|
||||||
p.apellidoMaterno,
|
p.apellidoMaterno,
|
||||||
)
|
)
|
||||||
.execute(db)
|
.execute(&mut *tx)
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
if let Err(reason) = query {
|
let query_2 = sqlx::query!("SELECT LAST_INSERT_ID() AS person_id")
|
||||||
eprintln!("Error inserting person into DB: {:?}", reason);
|
.fetch_one(&mut *tx)
|
||||||
return (Status::InternalServerError, Json(Person::default()));
|
.await;
|
||||||
}
|
|
||||||
|
|
||||||
match Person::get_by_dni(dni).await {
|
match tx.commit().await {
|
||||||
Ok(p) => return (Status::Ok, Json(p)),
|
Ok(_) => (),
|
||||||
Err(error) => {
|
Err(err) => {
|
||||||
eprintln!("Error fetching person from DB: {:?}", error);
|
eprintln!("Error commiting transaction: {:?}", err);
|
||||||
|
return (
|
||||||
return (Status::InternalServerError, Json(Person::default()));
|
Status::InternalServerError,
|
||||||
|
JsonResult::err(format!("Error confirmando transaccion.")),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let Err(reason) = query_1 {
|
||||||
|
eprintln!("Error inserting person into DB: {:?}", reason);
|
||||||
|
return (
|
||||||
|
Status::InternalServerError,
|
||||||
|
JsonResult::err(format!("Error insertando a DB.")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
let inserted_id = match query_2 {
|
||||||
|
Ok(value) => value.person_id,
|
||||||
|
Err(error) => {
|
||||||
|
println!("Error getting last inserted id: {:?}", error);
|
||||||
|
return (
|
||||||
|
Status::InternalServerError,
|
||||||
|
JsonResult::err(format!("Error recuperando ID insertado.")),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
Status::Ok,
|
||||||
|
JsonResult::ok(Person {
|
||||||
|
person_id: inserted_id as i32,
|
||||||
|
person_dni: format!("{}", dni),
|
||||||
|
person_names: p.nombres,
|
||||||
|
person_paternal_surname: p.apellidoPaterno,
|
||||||
|
person_maternal_surname: p.apellidoMaterno,
|
||||||
|
person_classroom_id: None,
|
||||||
|
}),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return error
|
// Return error
|
||||||
(Status::NotFound, Json(Person::default()))
|
(
|
||||||
|
Status::NotFound,
|
||||||
|
JsonResult::err(format!("Persona no encontrada.")),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
#[options("/person")]
|
#[options("/person")]
|
||||||
|
@ -12,6 +12,8 @@ mod cors;
|
|||||||
mod model;
|
mod model;
|
||||||
mod online_classroom;
|
mod online_classroom;
|
||||||
|
|
||||||
|
pub mod json_result;
|
||||||
|
|
||||||
static DB: OnceCell<Pool<MySql>> = OnceCell::new();
|
static DB: OnceCell<Pool<MySql>> = OnceCell::new();
|
||||||
|
|
||||||
pub fn db() -> &'static Pool<MySql> {
|
pub fn db() -> &'static Pool<MySql> {
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
use rocket::{http::Status, serde::json::Json};
|
use rocket::{http::Status, serde::json::Json};
|
||||||
|
|
||||||
use self::{json_result::JsonResult, session::ensure_session};
|
use crate::json_result::JsonResult;
|
||||||
|
|
||||||
|
use self::session::ensure_session;
|
||||||
|
|
||||||
pub mod json_result;
|
|
||||||
mod session;
|
mod session;
|
||||||
pub mod users;
|
pub mod users;
|
||||||
|
|
||||||
|
@ -8,7 +8,6 @@ struct CookieHold {
|
|||||||
pub jar: CookieJar,
|
pub jar: CookieJar,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
lazy_static! {
|
lazy_static! {
|
||||||
/// Stores a client with a persistent cookie store
|
/// Stores a client with a persistent cookie store
|
||||||
static ref SESSION_COOKIE: RwLock<CookieHold> = RwLock::new(CookieHold { jar: CookieJar::new()});
|
static ref SESSION_COOKIE: RwLock<CookieHold> = RwLock::new(CookieHold { jar: CookieJar::new()});
|
||||||
@ -16,7 +15,6 @@ lazy_static! {
|
|||||||
static ref SESSION_TIME: RwLock<u64> = RwLock::new(0);
|
static ref SESSION_TIME: RwLock<u64> = RwLock::new(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Makes a request to the online classroom, and returns the html string
|
/// Makes a request to the online classroom, and returns the html string
|
||||||
pub async fn request(url: String) -> Result<String, String> {
|
pub async fn request(url: String) -> Result<String, String> {
|
||||||
let classroom_url = std::env::var("CLASSROOM_URL").expect("CLASSROOM_URL env var is not set!");
|
let classroom_url = std::env::var("CLASSROOM_URL").expect("CLASSROOM_URL env var is not set!");
|
||||||
|
@ -1,4 +1,6 @@
|
|||||||
use super::{json_result::JsonResult, session::request};
|
use crate::json_result::JsonResult;
|
||||||
|
|
||||||
|
use super::session::request;
|
||||||
use rocket::{http::Status, serde::json::Json};
|
use rocket::{http::Status, serde::json::Json};
|
||||||
use scraper::{ElementRef, Html, Selector};
|
use scraper::{ElementRef, Html, Selector};
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
"@solidjs/router": "^0.8.3",
|
"@solidjs/router": "^0.8.3",
|
||||||
"@types/file-saver": "^2.0.5",
|
"@types/file-saver": "^2.0.5",
|
||||||
"@types/qrcode": "^1.5.1",
|
"@types/qrcode": "^1.5.1",
|
||||||
|
"axios": "^1.5.1",
|
||||||
"file-saver": "^2.0.5",
|
"file-saver": "^2.0.5",
|
||||||
"qrcode": "^1.5.3",
|
"qrcode": "^1.5.3",
|
||||||
"solid-js": "^1.7.6"
|
"solid-js": "^1.7.6"
|
||||||
|
@ -6,6 +6,8 @@ import { XIcon } from "../../icons/XIcon";
|
|||||||
import { Person } from "../../types/Person";
|
import { Person } from "../../types/Person";
|
||||||
import { PersonDisplay } from "./PersonDisplay";
|
import { PersonDisplay } from "./PersonDisplay";
|
||||||
import { PersonRegister } from "./PersonRegister";
|
import { PersonRegister } from "./PersonRegister";
|
||||||
|
import { backend } from "../../utils/functions";
|
||||||
|
import { JsonResult } from "../../types/JsonResult";
|
||||||
|
|
||||||
type HTMLButtonEvent = JSX.EventHandlerUnion<HTMLButtonElement, MouseEvent>;
|
type HTMLButtonEvent = JSX.EventHandlerUnion<HTMLButtonElement, MouseEvent>;
|
||||||
|
|
||||||
@ -44,37 +46,38 @@ export function Search(props: {setPerson: (p: Person | null) => void}) {
|
|||||||
/*
|
/*
|
||||||
Get the user data from the DB
|
Get the user data from the DB
|
||||||
*/
|
*/
|
||||||
const search = async() => {
|
const search = () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
setError("");
|
setError("");
|
||||||
|
|
||||||
try {
|
backend.get<JsonResult<Person>>(`/api/person/${dni()}`)
|
||||||
const response = await fetch(`${import.meta.env.VITE_BACKEND_URL}/api/person/${dni()}`);
|
.then((response) => {
|
||||||
const body = await response.json();
|
if (response.status === 200) {
|
||||||
|
setPerson(response.data.Ok);
|
||||||
|
props.setPerson(response.data.Ok);
|
||||||
|
setManualCreate(false);
|
||||||
|
} else if (response.status === 404) {
|
||||||
|
console.error(response.data);
|
||||||
|
|
||||||
if (response.ok) {
|
setError("No encontrado. Ingresar datos manualmente.");
|
||||||
setPerson(body);
|
setManualCreate(true);
|
||||||
props.setPerson(body);
|
props.setPerson(null);
|
||||||
setManualCreate(false);
|
} else {
|
||||||
} else if (response.status === 404) {
|
setError(response.data.Err.reason);
|
||||||
console.error(body);
|
setManualCreate(false);
|
||||||
|
}
|
||||||
setError("No encontrado. Ingresar datos manualmente.");
|
})
|
||||||
setManualCreate(true);
|
.catch((err) => {
|
||||||
props.setPerson(null);
|
setError(`Error inesperado: ${err.message}`);
|
||||||
} else {
|
console.error(err);
|
||||||
setError(body);
|
})
|
||||||
setManualCreate(false);
|
.finally(() => {
|
||||||
}
|
setLoading(false);
|
||||||
} catch (e) {
|
})
|
||||||
setError(JSON.stringify(e));
|
;
|
||||||
}
|
|
||||||
|
|
||||||
setLoading(false);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div class="text-center">
|
<div class="text-center">
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import axios from "axios";
|
||||||
|
|
||||||
|
|
||||||
// YYYY-MM-DD to DD/MM/YYYY
|
// YYYY-MM-DD to DD/MM/YYYY
|
||||||
@ -5,3 +6,7 @@ export function formatDate(date: string): string {
|
|||||||
const [year, month, day] = date.split("-");
|
const [year, month, day] = date.split("-");
|
||||||
return `${day}/${month}/${year}`;
|
return `${day}/${month}/${year}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const backend = axios.create({
|
||||||
|
baseURL: import.meta.env.VITE_BACKEND_URL,
|
||||||
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user