-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into board-stream
- Loading branch information
Showing
5 changed files
with
62 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use askama::Template; | ||
use axum::{ | ||
extract::{Path, State}, | ||
response::{IntoResponse, Response}, | ||
}; | ||
use sqlx::types::Uuid; | ||
|
||
use crate::api::models::ApiGameBoard; | ||
use crate::database::models::{Game, GameBoard, GameError}; | ||
use crate::AppState; | ||
|
||
pub async fn handler( | ||
State(state): State<AppState>, | ||
Path(game_id): Path<Uuid>, | ||
) -> Result<impl IntoResponse, ReadBoardError> { | ||
let mut conn = state.database().acquire().await?; | ||
if !Game::exists(&mut conn, game_id).await? { | ||
return Err(ReadBoardError::NotFound); | ||
} | ||
|
||
let game_board = GameBoard::latest(&mut conn, game_id).await?; | ||
|
||
tracing::info!("read game board: {:?}", game_board); | ||
|
||
let api_game_board = ApiGameBoard::from(game_board); | ||
|
||
Ok(TemplateApiGameBoard { api_game_board }) | ||
} | ||
|
||
#[derive(Template)] | ||
#[template(path = "game_board.html")] | ||
struct TemplateApiGameBoard { | ||
api_game_board: ApiGameBoard, | ||
} | ||
|
||
#[derive(Debug, thiserror::Error)] | ||
pub enum ReadBoardError { | ||
#[error("sqlx error: {0}")] | ||
Sqlx(#[from] sqlx::Error), | ||
#[error("game error: {0}")] | ||
Game(#[from] GameError), | ||
#[error("game not found")] | ||
NotFound, | ||
} | ||
|
||
impl IntoResponse for ReadBoardError { | ||
fn into_response(self) -> Response { | ||
match self { | ||
ReadBoardError::NotFound => { | ||
let body = format!("{}", self); | ||
(axum::http::StatusCode::NOT_FOUND, body).into_response() | ||
} | ||
_ => { | ||
let body = format!("{}", self); | ||
(axum::http::StatusCode::INTERNAL_SERVER_ERROR, body).into_response() | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters