From ac2028d37385bdf0e99fad5bc8374262d6c828f3 Mon Sep 17 00:00:00 2001 From: ATechnoHazard Date: Sat, 26 Sep 2020 19:59:32 +0530 Subject: [PATCH] pastes: don't 404 when user does not exist Signed-off-by: ATechnoHazard --- src/api/routes/paste.rs | 11 ++++++----- src/core/users/entity.rs | 11 +++++++++++ 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/api/routes/paste.rs b/src/api/routes/paste.rs index 5276ce4..59056d0 100644 --- a/src/api/routes/paste.rs +++ b/src/api/routes/paste.rs @@ -8,10 +8,11 @@ use serde_json::Value; use crate::api::catchers::{forbidden, internal_server_error, not_found, unprocessable_entity}; use crate::api::guards::db::DbConn; -use crate::core::paste::{entity::Paste, service::{create_paste, fetch_paste, update_paste};}; +use crate::core::paste::{entity::Paste, service::{create_paste, fetch_paste, update_paste}}; use crate::core::users::service::{create_or_fetch_user, fetch_user}; use crate::utils::phonetic_key; use crate::utils::users::get_session_id; +use crate::core::users::entity::User; #[post("/", data = "")] fn create(mut paste: Json, conn: DbConn, mut ck: Cookies) -> Custom> { @@ -53,10 +54,10 @@ fn fetch(id: String, conn: DbConn, mut ck: Cookies) -> Custom> { let user = match r_user { Ok(user) => user, Err(e) => { - return match e.downcast_ref::() { - Some(Error::NotFound) => not_found(), - _ => internal_server_error(), - }; + match e.downcast_ref::() { + Some(Error::NotFound) => User::new(), + _ => return internal_server_error(), + } } }; diff --git a/src/core/users/entity.rs b/src/core/users/entity.rs index 2b5c3f3..484ae38 100644 --- a/src/core/users/entity.rs +++ b/src/core/users/entity.rs @@ -8,3 +8,14 @@ pub struct User { pub password: Option, pub activated: Option, } + +impl User { + pub fn new() -> Self { + User { + id: "".to_string(), + username: None, + password: None, + activated: None, + } + } +}