forked from Roflin/gamenight
Replace ApiResponseVariant with Result
This commit is contained in:
parent
aab60dcc11
commit
5fca980af9
@ -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<Redirect>)
|
||||
}
|
||||
|
||||
#[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 = "<gamenight_json>")]
|
||||
@ -119,12 +111,12 @@ pub async fn gamenight_post_json(
|
||||
conn: DbConn,
|
||||
user: Option<schema::User>,
|
||||
gamenight_json: Json<schema::GameNightNoId>,
|
||||
) -> ApiResponseVariant {
|
||||
) -> Result<json::Value, Status> {
|
||||
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<schema::Register>,
|
||||
) -> ApiResponseVariant {
|
||||
) -> Result<json::Value, Status> {
|
||||
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<AppConfig>,
|
||||
login_json: Json<schema::Login>,
|
||||
) -> ApiResponseVariant {
|
||||
) -> Result<json::Value, Status> {
|
||||
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())))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user