2023-09-03 00:42:43 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2023-08-31 03:34:27 +00:00
|
|
|
|
|
|
|
use crate::db;
|
2023-08-25 16:52:03 +00:00
|
|
|
|
2023-08-25 22:31:46 +00:00
|
|
|
#[derive(Serialize, Clone)]
|
2023-08-25 16:52:03 +00:00
|
|
|
pub struct Person {
|
2023-08-25 22:31:46 +00:00
|
|
|
/// Internal id
|
2023-08-25 16:52:03 +00:00
|
|
|
pub person_id: i32,
|
2023-08-25 22:31:46 +00:00
|
|
|
/// Country-specific id. For now only supports Peru's DNI.
|
|
|
|
///
|
|
|
|
/// Example: `74185293`
|
2023-08-25 16:52:03 +00:00
|
|
|
pub person_dni: String,
|
2023-08-25 22:31:46 +00:00
|
|
|
/// Names
|
|
|
|
///
|
|
|
|
/// Example: `Juan Carlos`
|
2023-08-25 16:52:03 +00:00
|
|
|
pub person_names: String,
|
2023-08-25 22:31:46 +00:00
|
|
|
/// First surname
|
|
|
|
///
|
|
|
|
/// Example: `Perez`
|
2023-08-25 16:52:03 +00:00
|
|
|
pub person_paternal_surname: String,
|
2023-08-25 22:31:46 +00:00
|
|
|
/// Second surname
|
|
|
|
///
|
|
|
|
/// Example: `Gomez`
|
2023-08-25 16:52:03 +00:00
|
|
|
pub person_maternal_surname: String,
|
2023-09-27 15:57:51 +00:00
|
|
|
/// Id of the online classroom user id linked to this user
|
2023-09-29 22:12:58 +00:00
|
|
|
pub person_classroom_id: Option<i32>,
|
2023-10-05 16:25:23 +00:00
|
|
|
pub person_classroom_username: Option<String>,
|
2023-08-25 16:52:03 +00:00
|
|
|
}
|
2023-08-29 21:04:47 +00:00
|
|
|
|
|
|
|
impl Person {
|
2023-09-07 22:18:21 +00:00
|
|
|
pub async fn get_by_dni(dni: i32) -> Result<Person, sqlx::Error> {
|
|
|
|
let db = db();
|
|
|
|
|
|
|
|
let result = sqlx::query_as!(Person, "SELECT * FROM person WHERE person_dni = ?", dni)
|
|
|
|
.fetch_one(db)
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
Ok(result)
|
|
|
|
}
|
2023-08-29 21:04:47 +00:00
|
|
|
}
|
2023-08-31 03:34:27 +00:00
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
pub struct PersonCreate {
|
|
|
|
pub person_dni: String,
|
|
|
|
pub person_names: String,
|
|
|
|
pub person_paternal_surname: String,
|
|
|
|
pub person_maternal_surname: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PersonCreate {
|
|
|
|
pub async fn create(&self) -> Result<(), sqlx::Error> {
|
|
|
|
let db = db();
|
|
|
|
|
2023-09-03 00:42:43 +00:00
|
|
|
sqlx::query!(
|
|
|
|
"INSERT INTO person (person_dni, person_names, person_paternal_surname, person_maternal_surname) VALUES (?, ?, ?, ?)",
|
|
|
|
self.person_dni,
|
|
|
|
self.person_names,
|
|
|
|
self.person_paternal_surname,
|
2023-09-27 15:57:51 +00:00
|
|
|
self.person_maternal_surname,
|
2023-09-03 00:42:43 +00:00
|
|
|
)
|
2023-08-31 03:34:27 +00:00
|
|
|
.execute(db)
|
|
|
|
.await?;
|
2023-09-03 00:42:43 +00:00
|
|
|
|
2023-08-31 03:34:27 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
2023-10-03 16:43:30 +00:00
|
|
|
|
2023-10-04 22:49:37 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2023-10-03 16:43:30 +00:00
|
|
|
pub struct PersonLink {
|
|
|
|
pub person_id: i32,
|
|
|
|
pub person_classroom_id: i32,
|
2023-10-05 16:25:23 +00:00
|
|
|
pub person_classroom_username: String,
|
2023-10-03 16:43:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl PersonLink {
|
|
|
|
pub async fn insert(&self) -> Result<(), String> {
|
|
|
|
let db = db();
|
|
|
|
|
|
|
|
let res = sqlx::query!(
|
2023-10-05 16:25:23 +00:00
|
|
|
"UPDATE person SET person_classroom_id = ?, person_classroom_username = ? WHERE person_id = ?",
|
2023-10-03 16:43:30 +00:00
|
|
|
self.person_classroom_id,
|
2023-10-05 16:25:23 +00:00
|
|
|
self.person_classroom_username,
|
2023-10-03 16:43:30 +00:00
|
|
|
self.person_id,
|
|
|
|
)
|
2023-10-04 16:29:03 +00:00
|
|
|
.execute(db)
|
|
|
|
.await;
|
2023-10-03 16:43:30 +00:00
|
|
|
|
|
|
|
match res {
|
|
|
|
Ok(_) => Ok(()),
|
|
|
|
Err(error) => {
|
|
|
|
eprintln!("Error linking person to classroom: {:?}", error);
|
|
|
|
Err(format!("Error vinculando persona."))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|