feat: add details endpoint

This commit is contained in:
Sebastian Pravda 2022-11-06 13:01:39 +01:00 committed by Sebastian Pravda
parent 43fe565fa2
commit 46dfbddad6
2 changed files with 29 additions and 12 deletions

View file

@ -4,7 +4,7 @@ extern crate rocket;
use std::net::SocketAddr;
use guards::request::auth::{CandidateAuth, AdminAuth};
use portfolio_core::services::candidate_service::CandidateService;
use portfolio_core::services::candidate_service::{CandidateService, AddUserDetailsForm};
use requests::{LoginRequest, RegisterRequest};
use rocket::http::Status;
use rocket::{Rocket, Build};
@ -57,6 +57,23 @@ async fn admin(session: AdminAuth) -> Result<String, Custom<String>> {
Ok("Hello admin".to_string())
}
#[put("/details", data = "<details>")]
async fn fill_details(conn: Connection<'_, Db>, details: Json<AddUserDetailsForm>, session: CandidateAuth) -> Result<String, Custom<String>> {
let db = conn.into_inner();
let form = details.into_inner();
let candidate: entity::candidate::Model = session.into();
let candidate = CandidateService::add_user_details(db, candidate, form)
.await;
if candidate.is_err() { // TODO cleanup
let e = candidate.err().unwrap();
return Err(Custom(Status::from_code(e.code()).unwrap_or_default(), e.message()));
}
Ok("Details added".to_string())
}
#[post("/login", data = "<login_form>")]
async fn login(conn: Connection<'_, Db>, login_form: Json<LoginRequest>, ip_addr: SocketAddr) -> Result<String, Custom<String>> {
let db = conn.into_inner();
@ -90,7 +107,7 @@ async fn start() -> Result<(), rocket::Error> {
.attach(Db::init())
.attach(AdHoc::try_on_ignite("Migrations", run_migrations))
//.mount("/", FileServer::from(relative!("/static")))
.mount("/", routes![create, login, hello, validate, admin])
.mount("/", routes![create, login, hello, validate, fill_details, admin])
.register("/", catchers![])
.launch()
.await

View file

@ -17,7 +17,7 @@ pub(crate) struct EncryptedAddUserData {
pub name: String,
pub surname: String,
pub birthplace: String,
pub birthdate: NaiveDate,
// pub birthdate: NaiveDate,
pub address: String,
pub telephone: String,
pub citizenship: String,
@ -58,7 +58,7 @@ impl EncryptedAddUserData {
name,
surname,
birthplace,
birthdate: NaiveDate::from_ymd(2000, 1, 1),
// birthdate: NaiveDate::from_ymd(2000, 1, 1),
address,
telephone,
citizenship,
@ -74,7 +74,7 @@ pub struct AddUserDetailsForm {
pub name: String,
pub surname: String,
pub birthplace: String,
pub birthdate: NaiveDate,
// pub birthdate: NaiveDate,
pub address: String,
pub telephone: String,
pub citizenship: String,
@ -143,16 +143,16 @@ impl CandidateService {
pub async fn add_user_details(
db: &DbConn,
application_id: i32,
user: candidate::Model,
form: AddUserDetailsForm,
) -> Result<entity::candidate::Model, ServiceError> {
let Ok(user) = Query::find_candidate_by_id(db, application_id).await else {
/* let Ok(user) = Query::find_candidate_by_id(db, application_id).await else {
return Err(ServiceError::DbError);
};
let Some(user_unwrapped) = user else {
return Err(ServiceError::UserNotFound);
};
}; */
let Ok(admin_public_keys) = Query::get_all_admin_public_keys(db).await else {
return Err(ServiceError::DbError);
@ -161,7 +161,7 @@ impl CandidateService {
let mut admin_public_keys_refrence: Vec<&str> =
admin_public_keys.iter().map(|s| &**s).collect();
let mut recipients = vec![&*user_unwrapped.public_key];
let mut recipients = vec![&*user.public_key];
recipients.append(&mut admin_public_keys_refrence);
@ -169,7 +169,7 @@ impl CandidateService {
Mutation::add_candidate_details(
db,
user_unwrapped,
user,
enc_details,
)
.await
@ -291,7 +291,7 @@ mod tests {
name: "test".to_string(),
surname: "a".to_string(),
birthplace: "b".to_string(),
birthdate: NaiveDate::from_ymd(1999, 1, 1),
// birthdate: NaiveDate::from_ymd(1999, 1, 1),
address: "test".to_string(),
telephone: "test".to_string(),
citizenship: "test".to_string(),
@ -299,7 +299,7 @@ mod tests {
sex: "test".to_string(),
study: "test".to_string(),
};
let candidate = CandidateService::add_user_details(&db, candidate.application, form).await.ok().unwrap();
let candidate = CandidateService::add_user_details(&db, candidate, form).await.ok().unwrap();