server/services/queries/get_game.rs
1use crate::{
2 domain::{Game, GameId},
3 error::{ServerError, bug},
4 server_state::ServerState,
5};
6
7/// Retrieves a game by its ID.
8///
9/// This function fetches the specified game from the server state. If the
10/// game exists, it is returned; otherwise, `None` is returned.
11///
12/// # Parameters
13///
14/// - `server_state`: The shared server state containing the games database.
15/// - `game_id`: The ID of the game to retrieve.
16///
17/// # Returns
18///
19/// Returns `Ok(Some(Game))` if the game exists.
20/// Returns `Ok(None)` if no game with the given ID is found.
21/// Returns a `ServerError` if there is a problem accessing the database
22/// or processing the request.
23pub async fn get_game(
24 server_state: ServerState,
25 game_id: GameId,
26) -> Result<Option<Game>, ServerError> {
27 use cqrs_es::persist::ViewRepository;
28
29 let game = server_state
30 .game_view_repo
31 .load(&game_id.value().to_string())
32 .await
33 .map_err(bug!())?;
34
35 let game = game.map(|g| g.instance().clone());
36
37 Ok(game)
38}