From 5fca980af9cfd9102953052dd310b9ab199e5c29 Mon Sep 17 00:00:00 2001 From: Lars Jellema Date: Thu, 21 Apr 2022 21:39:25 +0200 Subject: [PATCH] Replace ApiResponseVariant with Result --- backend/src/api.rs | 44 ++++++++++++++++++-------------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/backend/src/api.rs b/backend/src/api.rs index c04cb66..5a6ca54 100644 --- a/backend/src/api.rs +++ b/backend/src/api.rs @@ -10,20 +10,13 @@ use jsonwebtoken::{EncodingKey, Header}; use rocket::http::Status; use rocket::request::Outcome; use rocket::request::{FromRequest, Request}; -use rocket::serde::json::{json, Json, Value}; +use rocket::serde::json; +use rocket::serde::json::{json, Json}; use rocket::State; use serde::{Deserialize, Serialize}; use std::borrow::Cow; use validator::ValidateArgs; -#[derive(Debug, Responder)] -pub enum ApiResponseVariant { - Status(Status), - // Redirect(Redirect), - Value(Value), - // Flash(Flash) -} - #[derive(Serialize, Deserialize, Debug)] struct ApiResponse { result: Cow<'static, str>, @@ -104,14 +97,13 @@ impl<'r> FromRequest<'r> for schema::User { } #[get("/gamenights")] -pub async fn gamenights(conn: DbConn, _user: schema::User) -> ApiResponseVariant { - let gamenights = schema::get_all_gamenights(conn).await; - ApiResponseVariant::Value(json!(gamenights)) +pub async fn gamenights(conn: DbConn, _user: schema::User) -> json::Value { + json!(schema::get_all_gamenights(conn).await) } #[get("/gamenights", rank = 2)] -pub async fn gamenights_unauthorized() -> ApiResponseVariant { - ApiResponseVariant::Status(Status::Unauthorized) +pub async fn gamenights_unauthorized() -> Status { + Status::Unauthorized } #[post("/gamenight", format = "application/json", data = "")] @@ -119,12 +111,12 @@ pub async fn gamenight_post_json( conn: DbConn, user: Option, gamenight_json: Json, -) -> ApiResponseVariant { +) -> Result { if user.is_some() { schema::insert_gamenight(conn, gamenight_json.into_inner()).await; - ApiResponseVariant::Value(json!(ApiResponse::SUCCES)) + Ok(json!(ApiResponse::SUCCES)) } else { - ApiResponseVariant::Status(Status::Unauthorized) + Err(Status::Unauthorized) } } @@ -132,7 +124,7 @@ pub async fn gamenight_post_json( pub async fn register_post_json( conn: DbConn, register_json: Json, -) -> ApiResponseVariant { +) -> Result { let register = register_json.into_inner(); let register_clone = register.clone(); match conn @@ -141,13 +133,13 @@ pub async fn register_post_json( { Ok(()) => (), Err(error) => { - return ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))) + return Ok(json!(ApiResponse::error(error.to_string()))) } } match schema::insert_user(conn, register).await { - Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)), - Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))), + Ok(_) => Ok(json!(ApiResponse::SUCCES)), + Err(err) => Ok(json!(ApiResponse::error(err.to_string()))), } } @@ -163,12 +155,12 @@ pub async fn login_post_json( conn: DbConn, config: &State, login_json: Json, -) -> ApiResponseVariant { +) -> Result { match schema::login(conn, login_json.into_inner()).await { - Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))), + Err(err) => Ok(json!(ApiResponse::error(err.to_string()))), Ok(login_result) => { if !login_result.result { - return ApiResponseVariant::Value(json!(ApiResponse::error(String::from( + return Ok(json!(ApiResponse::error(String::from( "username and password didn't match" )))); } @@ -185,9 +177,9 @@ pub async fn login_post_json( &my_claims, &EncodingKey::from_secret(secret.as_bytes()), ) { - Ok(token) => ApiResponseVariant::Value(json!(ApiResponse::login_response(token))), + Ok(token) => Ok(json!(ApiResponse::login_response(token))), Err(error) => { - ApiResponseVariant::Value(json!(ApiResponse::error(error.to_string()))) + Ok(json!(ApiResponse::error(error.to_string()))) } } }