summaryrefslogtreecommitdiffstats
path: root/src/routes
diff options
context:
space:
mode:
authorLibravatar cel 🌸 <cel@blos.sm>2023-12-12 18:20:56 +0000
committerLibravatar cel 🌸 <cel@blos.sm>2023-12-12 18:20:56 +0000
commita587459a1817c0fc57b46df3f9c69567e1e775b7 (patch)
treeab99c6aaa7c2b3245c83db6332e4ca54006831b3 /src/routes
parent370a25e5a0cbb95e2aa1cec55305b22aeaf99aa0 (diff)
downloadpinussy-a587459a1817c0fc57b46df3f9c69567e1e775b7.tar.gz
pinussy-a587459a1817c0fc57b46df3f9c69567e1e775b7.tar.bz2
pinussy-a587459a1817c0fc57b46df3f9c69567e1e775b7.zip
refactor: separate model and controller
Diffstat (limited to 'src/routes')
-rw-r--r--src/routes/home.rs7
-rw-r--r--src/routes/login.rs18
-rw-r--r--src/routes/signup.rs16
-rw-r--r--src/routes/users.rs7
4 files changed, 13 insertions, 35 deletions
diff --git a/src/routes/home.rs b/src/routes/home.rs
index 11d3a72..a43eabc 100644
--- a/src/routes/home.rs
+++ b/src/routes/home.rs
@@ -8,12 +8,7 @@ use crate::{Pinussy, Result};
async fn get(session: Session, state: web::Data<Pinussy>) -> Result<HttpResponse> {
let username: Option<String>;
if let Some(user_id) = session.get::<i32>("user_id")? {
- username = Some(
- sqlx::query!("select username from users where id = $1", user_id)
- .fetch_one(&state.db)
- .await?
- .username,
- )
+ username = Some(state.db.users().read(user_id).await?.username)
} else {
username = None
}
diff --git a/src/routes/login.rs b/src/routes/login.rs
index 33f7f69..c6cf077 100644
--- a/src/routes/login.rs
+++ b/src/routes/login.rs
@@ -1,9 +1,9 @@
use actix_session::Session;
use actix_web::http::header::LOCATION;
use actix_web::{get, post, web, HttpResponse};
-use bcrypt::verify;
use serde::Deserialize;
+use crate::error::PinussyError;
use crate::notification::{Kind, Notification};
use crate::templates;
use crate::Pinussy;
@@ -27,17 +27,11 @@ async fn post(
session: Session,
form: web::Form<LoginForm>,
) -> Result<HttpResponse> {
- match sqlx::query!(
- "select id, password from users where username = $1",
- &form.username
- )
- .fetch_one(&state.db)
- .await
- {
+ match state.db.users().read_username(&form.username).await {
Ok(user) => {
- let password_hash: String = user.password;
- if verify(&form.password, &password_hash)? {
- session.insert("user_id", user.id)?;
+ if user.verify_password(&form.password)? {
+ let user_id = state.db.users().get_id(&form.username).await?;
+ session.insert("user_id", user_id)?;
return Ok(HttpResponse::SeeOther()
.insert_header((LOCATION, "/"))
.finish());
@@ -54,7 +48,7 @@ async fn post(
));
}
}
- Err(sqlx::Error::RowNotFound) => {
+ Err(PinussyError::Database(sqlx::Error::RowNotFound)) => {
return Ok(HttpResponse::NotFound().body(
render!(
templates::login_html,
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!(
diff --git a/src/routes/users.rs b/src/routes/users.rs
index 2ad9ede..eb08ade 100644
--- a/src/routes/users.rs
+++ b/src/routes/users.rs
@@ -6,11 +6,6 @@ use crate::{Pinussy, Result};
#[get("/users")]
async fn get(state: web::Data<Pinussy>) -> Result<HttpResponse> {
- let users: Vec<User> = sqlx::query_as("select * from users")
- .fetch_all(&state.db)
- .await
- // TODO: no unwrap
- .unwrap();
- println!("lol");
+ let users: Vec<User> = state.db.users().read_all().await?;
Ok(HttpResponse::Ok().body(render!(templates::users_html, users).unwrap()))
}