diff options
author | cel 🌸 <cel@blos.sm> | 2023-12-12 18:20:56 +0000 |
---|---|---|
committer | cel 🌸 <cel@blos.sm> | 2023-12-12 18:20:56 +0000 |
commit | a587459a1817c0fc57b46df3f9c69567e1e775b7 (patch) | |
tree | ab99c6aaa7c2b3245c83db6332e4ca54006831b3 /src/routes/signup.rs | |
parent | 370a25e5a0cbb95e2aa1cec55305b22aeaf99aa0 (diff) | |
download | pinussy-a587459a1817c0fc57b46df3f9c69567e1e775b7.tar.gz pinussy-a587459a1817c0fc57b46df3f9c69567e1e775b7.tar.bz2 pinussy-a587459a1817c0fc57b46df3f9c69567e1e775b7.zip |
refactor: separate model and controller
Diffstat (limited to 'src/routes/signup.rs')
-rw-r--r-- | src/routes/signup.rs | 16 |
1 files changed, 5 insertions, 11 deletions
diff --git a/src/routes/signup.rs b/src/routes/signup.rs index ae10201..bb1f714 100644 --- a/src/routes/signup.rs +++ b/src/routes/signup.rs @@ -1,11 +1,12 @@ use actix_web::{get, post, web, HttpResponse}; use serde::Deserialize; +use crate::error::PinussyError; use crate::notification::{Kind as NotificationKind, Notification}; use crate::templates; +use crate::users::User; use crate::Pinussy; use crate::Result; -use bcrypt::{hash, DEFAULT_COST}; #[get("/signup")] async fn get() -> HttpResponse { @@ -20,15 +21,8 @@ struct SignupForm { #[post("/signup")] async fn post(state: web::Data<Pinussy>, form: web::Form<SignupForm>) -> Result<HttpResponse> { - let password_hash = hash(&form.password, DEFAULT_COST)?; - match sqlx::query!( - "insert into users(username, password) values ($1, $2)", - &form.username, - password_hash - ) - .execute(&state.db) - .await - { + let new_user = User::new(form.username.clone(), form.password.clone())?; + match state.db.users().create(new_user).await { Ok(_) => { return Ok(HttpResponse::Ok().body( render!( @@ -43,7 +37,7 @@ async fn post(state: web::Data<Pinussy>, form: web::Form<SignupForm>) -> Result< } Err(e) => { match e { - sqlx::Error::Database(e) => { + PinussyError::Database(sqlx::Error::Database(e)) => { if e.is_unique_violation() { return Ok(HttpResponse::Conflict().body( render!( |