Adds an add gamenight control and fixes the fetch gamenight Effect,

Introduces an infinite fetch gamenights loop
This commit is contained in:
2022-04-29 22:40:10 +02:00
parent bf796201bf
commit 2cfaf2b4cc
10 changed files with 189 additions and 20 deletions

View File

@@ -31,6 +31,8 @@ struct ApiResponse {
message: Option<Cow<'static, str>>,
#[serde(skip_serializing_if = "Option::is_none")]
jwt: Option<Cow<'static, str>>,
#[serde(skip_serializing_if = "Option::is_none")]
gamenights: Option<Vec<schema::GameNight>>,
}
impl ApiResponse {
@@ -41,6 +43,7 @@ impl ApiResponse {
result: Self::SUCCES_RESULT,
message: None,
jwt: None,
gamenights: None,
};
fn error(message: String) -> Self {
@@ -48,6 +51,7 @@ impl ApiResponse {
result: Self::FAILURE_RESULT,
message: Some(Cow::Owned(message)),
jwt: None,
gamenights: None,
}
}
@@ -56,6 +60,16 @@ impl ApiResponse {
result: Self::SUCCES_RESULT,
message: None,
jwt: Some(Cow::Owned(jwt)),
gamenights: None,
}
}
fn gamenight_response(gamenights: Vec<schema::GameNight>) -> Self {
Self {
result: Self::SUCCES_RESULT,
message: None,
jwt: None,
gamenights: Some(gamenights),
}
}
}
@@ -106,7 +120,7 @@ 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))
ApiResponseVariant::Value(json!(ApiResponse::gamenight_response(gamenights)))
}
#[get("/gamenights", rank = 2)]
@@ -117,17 +131,20 @@ pub async fn gamenights_unauthorized() -> ApiResponseVariant {
#[post("/gamenight", format = "application/json", data = "<gamenight_json>")]
pub async fn gamenight_post_json(
conn: DbConn,
user: Option<schema::User>,
_user: schema::User,
gamenight_json: Json<schema::GameNightNoId>,
) -> ApiResponseVariant {
if user.is_some() {
schema::insert_gamenight(conn, gamenight_json.into_inner()).await;
ApiResponseVariant::Value(json!(ApiResponse::SUCCES))
} else {
ApiResponseVariant::Status(Status::Unauthorized)
match schema::insert_gamenight(conn, gamenight_json.into_inner()).await {
Ok(_) => ApiResponseVariant::Value(json!(ApiResponse::SUCCES)),
Err(err) => ApiResponseVariant::Value(json!(ApiResponse::error(err.to_string()))),
}
}
#[post("/gamenight", rank = 2)]
pub async fn gamenight_post_json_unauthorized() -> ApiResponseVariant {
ApiResponseVariant::Status(Status::Unauthorized)
}
#[post("/register", format = "application/json", data = "<register_json>")]
pub async fn register_post_json(
conn: DbConn,

View File

@@ -59,6 +59,7 @@ fn rocket() -> _ {
api::gamenights,
api::gamenights_unauthorized,
api::gamenight_post_json,
api::gamenight_post_json_unauthorized,
api::register_post_json,
api::login_post_json
],

View File

@@ -84,14 +84,18 @@ pub async fn get_all_gamenights(conn: DbConn) -> Vec<GameNight> {
.await
}
pub async fn insert_gamenight(conn: DbConn, new_gamenight: GameNightNoId) -> () {
conn.run(|c| {
pub async fn insert_gamenight(conn: DbConn, new_gamenight: GameNightNoId) -> Result<(), DatabaseError> {
let insert_result = conn.run(|c| {
diesel::insert_into(gamenight::table)
.values(new_gamenight)
.execute(c)
.unwrap()
})
.await;
match insert_result {
Err(e) => Err(DatabaseError::Query(e.to_string())),
_ => Ok(()),
}
}
pub async fn insert_user(conn: DbConn, new_user: Register) -> Result<(), DatabaseError> {