refactor(server): implement request handlers as methods

moved request handling logic into dedicated methods on the request handler struct for better organization and readability. this includes handlers for ping, list games, get game info, and invalid requests. game file data handler is added but not yet implemented.
This commit is contained in:
ddidderr committed 2025-08-12 08:39:23 +02:00
1 parent 13d97de515
commit 70548d2041
1 file changed
+21 -7
+21 -7
View File
@@ -44,14 +44,16 @@ impl RequestHandler {
Ok(()) Ok(())
} }
pub(crate) async fn process_request(&self, request: Request, games_folder: &Path) -> Response { fn handle_ping() -> Response {
match request { Response::Pong
Request::Ping => Response::Pong, }
Request::ListGames => {
async fn handle_list_games(&self) -> Response {
let db = self.db.read().await; let db = self.db.read().await;
Response::ListGames(db.all_games().into_iter().cloned().collect()) Response::ListGames(db.all_games().into_iter().cloned().collect())
} }
Request::GetGame { id } => {
async fn handle_get_game(&self, id: String, games_folder: &Path) -> Response {
if self.db.read().await.get_game_by_id(&id).is_none() { if self.db.read().await.get_game_by_id(&id).is_none() {
tracing::error!("Game not found in DB: {id}"); tracing::error!("Game not found in DB: {id}");
return Response::GameNotFound(id); return Response::GameNotFound(id);
@@ -97,10 +99,22 @@ impl RequestHandler {
file_descriptions: game_files_descs, file_descriptions: game_files_descs,
} }
} }
Request::GetGameFileData(_) => {
fn handle_get_game_file_data(&self) -> Response {
Response::InvalidRequest(Bytes::new(), "Not implemented".to_string()) Response::InvalidRequest(Bytes::new(), "Not implemented".to_string())
} }
Request::Invalid(data, err_msg) => Response::InvalidRequest(data, err_msg),
fn handle_invalid(&self, data: Bytes, err_msg: String) -> Response {
Response::InvalidRequest(data, err_msg)
}
pub(crate) async fn process_request(&self, request: Request, games_folder: &Path) -> Response {
match request {
Request::Ping => RequestHandler::handle_ping(),
Request::ListGames => self.handle_list_games().await,
Request::GetGame { id } => self.handle_get_game(id, games_folder).await,
Request::GetGameFileData(_) => self.handle_get_game_file_data(),
Request::Invalid(data, err_msg) => self.handle_invalid(data, err_msg),
} }
} }
} }