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::http::Status;
|
||||||
use rocket::request::Outcome;
|
use rocket::request::Outcome;
|
||||||
use rocket::request::{FromRequest, Request};
|
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 rocket::State;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::borrow::Cow;
|
use std::borrow::Cow;
|
||||||
use validator::ValidateArgs;
|
use validator::ValidateArgs;
|
||||||
|
|
||||||
#[derive(Debug, Responder)]
|
|
||||||
pub enum ApiResponseVariant {
|
|
||||||
Status(Status),
|
|
||||||
// Redirect(Redirect),
|
|
||||||
Value(Value),
|
|
||||||
// Flash(Flash<Redirect>)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug)]
|
#[derive(Serialize, Deserialize, Debug)]
|
||||||
struct ApiResponse {
|
struct ApiResponse {
|
||||||
result: Cow<'static, str>,
|
result: Cow<'static, str>,
|
||||||
@ -104,14 +97,13 @@ impl<'r> FromRequest<'r> for schema::User {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[get("/gamenights")]
|
#[get("/gamenights")]
|
||||||
pub async fn gamenights(conn: DbConn, _user: schema::User) -> ApiResponseVariant {
|
pub async fn gamenights(conn: DbConn, _user: schema::User) -> json::Value {
|
||||||
let gamenights = schema::get_all_gamenights(conn).await;
|
json!(schema::get_all_gamenights(conn).await)
|
||||||
ApiResponseVariant::Value(json!(gamenights))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[get("/gamenights", rank = 2)]
|
#[get("/gamenights", rank = 2)]
|
||||||
pub async fn gamenights_unauthorized() -> ApiResponseVariant {
|
pub async fn gamenights_unauthorized() -> Status {
|
||||||
ApiResponseVariant::Status(Status::Unauthorized)
|
Status::Unauthorized
|
||||||
}
|
}
|
||||||
|
|
||||||
#[post("/gamenight", format = "application/json", data = "<gamenight_json>")]
|
#[post("/gamenight", format = "application/json", data = "<gamenight_json>")]
|
||||||
@ -119,12 +111,12 @@ pub async fn gamenight_post_json(
|
|||||||
conn: DbConn,
|
conn: DbConn,
|
||||||
user: Option<schema::User>,
|
user: Option<schema::User>,
|
||||||
gamenight_json: Json<schema::GameNightNoId>,
|
gamenight_json: Json<schema::GameNightNoId>,
|
||||||
) -> ApiResponseVariant {
|
) -> Result<json::Value, Status> {
|
||||||
if user.is_some() {
|
if user.is_some() {
|
||||||
schema::insert_gamenight(conn, gamenight_json.into_inner()).await;
|
schema::insert_gamenight(conn, gamenight_json.into_inner()).await;
|
||||||
ApiResponseVariant::Value(json!(ApiResponse::SUCCES))
|
Ok(json!(ApiResponse::SUCCES))
|
||||||
} else {
|
} else {
|
||||||
ApiResponseVariant::Status(Status::Unauthorized)
|
Err(Status::Unauthorized)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -132,7 +124,7 @@ pub async fn gamenight_post_json(
|
|||||||
pub async fn register_post_json(
|
pub async fn register_post_json(
|
||||||
conn: DbConn,
|
conn: DbConn,
|
||||||
register_json: Json<schema::Register>,
|
register_json: Json<schema::Register>,
|
||||||
) -> ApiResponseVariant {
|
) -> Result<json::Value, Status> {
|
||||||
let register = register_json.into_inner();
|
let register = register_json.into_inner();
|
||||||
let register_clone = register.clone();
|
let register_clone = register.clone();
|
||||||
match conn
|
match conn
|
||||||
@ -141,13 +133,13 @@ pub async fn register_post_json(
|
|||||||
{
|
{
|
||||||
Ok(()) => (),
|
Ok(()) => (),
|
||||||
Err(error) => {
|
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 {
|
match schema::insert_user(conn, register).await {
|
||||||
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
|
Ok(_) => Ok(json!(ApiResponse::SUCCES)),
|
||||||
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
|
Err(err) => Ok(json!(ApiResponse::error(err.to_string()))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -163,12 +155,12 @@ pub async fn login_post_json(
|
|||||||
conn: DbConn,
|
conn: DbConn,
|
||||||
config: &State<AppConfig>,
|
config: &State<AppConfig>,
|
||||||
login_json: Json<schema::Login>,
|
login_json: Json<schema::Login>,
|
||||||
) -> ApiResponseVariant {
|
) -> Result<json::Value, Status> {
|
||||||
match schema::login(conn, login_json.into_inner()).await {
|
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) => {
|
Ok(login_result) => {
|
||||||
if !login_result.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"
|
"username and password didn't match"
|
||||||
))));
|
))));
|
||||||
}
|
}
|
||||||
@ -185,9 +177,9 @@ pub async fn login_post_json(
|
|||||||
&my_claims,
|
&my_claims,
|
||||||
&EncodingKey::from_secret(secret.as_bytes()),
|
&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) => {
|
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