From a587459a1817c0fc57b46df3f9c69567e1e775b7 Mon Sep 17 00:00:00 2001 From: cel 🌸 Date: Tue, 12 Dec 2023 18:20:56 +0000 Subject: refactor: separate model and controller --- src/routes/signup.rs | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'src/routes/signup.rs') 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, form: web::Form) -> Result { - 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, form: web::Form) -> 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!( -- cgit