Skip to content

Commit

Permalink
Merge branch 'main' into board-stream
Browse files Browse the repository at this point in the history
  • Loading branch information
amiller68 authored Jan 29, 2024
2 parents 6661b30 + d09f0d9 commit adf9ea1
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/api/games/read_game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub async fn handler(
let game_board = GameBoard::latest(&mut conn, game_id).await?;

let api_game_board = ApiGameBoard::from(game_board);

Ok(GameIndexTemplate { api_game_board })
}

Expand Down
59 changes: 59 additions & 0 deletions src/api/games/read_game_board.rs
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()
}
}
}
}
2 changes: 0 additions & 2 deletions src/api/games/watch_game_sse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use crate::api::templates::GameBoardTemplate;

// TODO: generalize and use the read_game_board handler
pub type GameUpdateStream = Sender<GameBoardTemplate>;

// TODO: proper error handling
pub async fn handler(
Path(game_id): Path<Uuid>,
Extension(tx): Extension<GameUpdateStream>,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl AppState {
#[shuttle_runtime::main]
async fn main(
#[shuttle_shared_db::Postgres(
local_uri = &std::env::var("DATABASE_URL").expect("DATABASE_URL must be set")
local_uri = &std::env::var("DATABASE_URL").unwrap_or_else(|_| "postgres://localhost:5432".to_string()),
)]
db: PgPool,
) -> shuttle_axum::ShuttleAxum {
Expand Down
1 change: 1 addition & 0 deletions static/js/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ initBoard = function() {
clickedSquare.classList.remove('selected');
selectedPiece = null;
return;

}

toSquare = clickedSquare;
Expand Down

0 comments on commit adf9ea1

Please sign in to comment.