Skip to content

Commit

Permalink
new endpoint to get individual game
Browse files Browse the repository at this point in the history
  • Loading branch information
Timtam committed Mar 26, 2024
1 parent 8911780 commit ef28b7a
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
1 change: 1 addition & 0 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ fn rocket_from_config(figment: Figment) -> Rocket<Build> {
users_routes::signup,
games_routes::create_game,
games_routes::get_all_games,
games_routes::get_game,
games_routes::join_game,
games_routes::leave_game,
games_routes::start_game,
Expand Down
60 changes: 59 additions & 1 deletion server/src/routes/games.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ use crate::{
services::{GameService, UserService},
users::User,
};
use rocket::{response::status::Created, serde::json::Json, State};
use rocket::{
response::status::{Created, NotFound},
serde::json::Json,
State,
};
use rocket_okapi::openapi;

/// Create a new game
Expand Down Expand Up @@ -108,6 +112,37 @@ pub async fn start_game(
})
}

/// Get all info about a certain game
///
/// Retrieve all known info about a specific game. game_id must be identical to a game's id, either returned by POST /games, or by GET /games.
/// The info here is currently identical with what you get with GET /games, but that might change later.
///
/// This call will return a 404 error if the game_id provided doesn't exist.
#[openapi(tag = "Games")]
#[get("/games/<game_id>")]
pub fn get_game(
game_id: u32,
games: &State<GameService>,
users: &State<UserService>,
) -> Result<Json<GameResponse>, NotFound<Json<MessageResponse>>> {
match games.get(game_id) {
Some(g) => Ok(Json(GameResponse {
id: g.id,
creator: users.get_by_id(g.creator).unwrap(),
players: g
.players
.into_iter()
.map(|p| users.get_by_id(p).unwrap())
.collect::<Vec<_>>(),
})),
None => Err(NotFound(Json(MessageResponse {
message: "game id not found".into(),
r#type: "error".into(),
}))),
}
}

#[cfg(test)]
mod tests {
use crate::{
Expand All @@ -131,6 +166,29 @@ mod tests {
assert!(game.into_json::<GameResponse>().await.is_some());
}

#[sqlx::test]
async fn can_read_single_game() {
let client = mocked_client().await;

let cookie = create_test_users(&client, 1).await.get(0).cloned().unwrap();

let game = client
.post(uri!("/api", super::create_game))
.private_cookie(cookie.clone())
.dispatch()
.await
.into_json::<GameResponse>()
.await
.unwrap();

let response = client
.get(uri!("/api", super::get_game(game_id = game.id)))
.dispatch()
.await;

assert_eq!(response.status(), Status::Ok);
}

#[sqlx::test]
async fn each_game_gets_individual_ids() {
let client = mocked_client().await;
Expand Down

0 comments on commit ef28b7a

Please sign in to comment.