From 9f892e8fb13e8b204561754f665a4557e89b75a2 Mon Sep 17 00:00:00 2001 From: Steven Liu Date: Tue, 6 Jan 2026 21:32:07 +0100 Subject: [PATCH] refactor(client/ws): divorce WS data handler from the class constructor --- .../client/scenes/FoodpalApplicationCtrl.java | 73 ++++++++++++------- 1 file changed, 45 insertions(+), 28 deletions(-) diff --git a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java index 502706d..756c349 100644 --- a/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java +++ b/client/src/main/java/client/scenes/FoodpalApplicationCtrl.java @@ -38,6 +38,7 @@ import javafx.scene.control.ListView; import javafx.scene.control.ToggleButton; import org.apache.commons.lang3.NotImplementedException; import org.apache.commons.lang3.tuple.ImmutablePair; +import org.apache.commons.lang3.tuple.Pair; public class FoodpalApplicationCtrl implements LocaleAware { private final ServerUtils server; @@ -91,46 +92,62 @@ public class FoodpalApplicationCtrl implements LocaleAware { this.configService = configService; this.dataService = recipeDataService; - recipeDataService.setMessageParser((msg) -> switch (msg) { + setupDataService(); + initializeWebSocket(); + } + private void setupDataService() { + dataService.setMessageParser((msg) -> switch (msg) { case CreateRecipeMessage _ -> (m) -> { CreateRecipeMessage crm = (CreateRecipeMessage) m; - this.recipeList.getItems().add(crm.getRecipe()); - dataService.add(crm.getRecipe().getId()); - return new ImmutablePair<>(crm.getRecipe().getId(), crm.getRecipe()); + return handleCreateRecipeMessage(crm); }; case UpdateRecipeMessage _ -> (m) -> { - UpdateRecipeMessage upm = (UpdateRecipeMessage) m; - Recipe recipe = upm.getRecipe(); - System.out.println("recipe: " + recipe.getId() + " of content: " + recipe); - // Discover the first (and should be ONLY) instance of the recipe with the specified ID. - // if not found, throw new InvalidModificationException with a message. - this.recipeList.getItems().set( - this.recipeList.getItems().indexOf( - findRecipeById(recipe.getId()) - .orElseThrow( - () -> new InvalidModificationException("Invalid recipe id during update: " + recipe.getId())) - ), - recipe - ); - System.out.println(recipe.getId()); - dataService.add(recipe.getId()); - return new ImmutablePair<>(recipe.getId(), recipe); + UpdateRecipeMessage urm = (UpdateRecipeMessage) m; + return handleUpdateRecipeMessage(urm); }; case DeleteRecipeMessage _ -> (m) -> { DeleteRecipeMessage drm = (DeleteRecipeMessage) m; - this.recipeList.getItems().remove(findRecipeById(drm.getRecipeId()).orElseThrow( - () -> new InvalidModificationException("Invalid recipe id during delete: " + drm.getRecipeId()) - )); - dataService.add(drm.getRecipeId()); - // TODO Make it an Optional so that we don't need to touch Null? - return new ImmutablePair<>(drm.getRecipeId(), null); + return handleDeleteRecipeMessage(drm); }; case FavouriteRecipeMessage _ -> (m) -> { - throw new NotImplementedException("TODO:: implement favourited recipe deletion warning"); + FavouriteRecipeMessage frm = (FavouriteRecipeMessage) m; + return handleFavouriteRecipeMessage(frm); }; default -> throw new IllegalStateException("Unexpected value: " + msg); }); - initializeWebSocket(); + } + + private Pair handleCreateRecipeMessage(CreateRecipeMessage crm) { + this.recipeList.getItems().add(crm.getRecipe()); + dataService.add(crm.getRecipe().getId()); + return new ImmutablePair<>(crm.getRecipe().getId(), crm.getRecipe()); + } + private Pair handleUpdateRecipeMessage(UpdateRecipeMessage urm) { + Recipe recipe = urm.getRecipe(); + // Discover the first (and should be ONLY) instance of the recipe with the specified ID. + // if not found, throw new InvalidModificationException with a message. + this.recipeList.getItems().set( + this.recipeList.getItems().indexOf( + findRecipeById(recipe.getId()) + .orElseThrow( + () -> new InvalidModificationException("Invalid recipe id during update: " + recipe.getId())) + ), + recipe + ); + dataService.add(recipe.getId()); + return new ImmutablePair<>(recipe.getId(), recipe); + } + private Pair handleDeleteRecipeMessage(DeleteRecipeMessage drm) { + this.recipeList.getItems().remove(findRecipeById(drm.getRecipeId()).orElseThrow( + () -> new InvalidModificationException("Invalid recipe id during delete: " + drm.getRecipeId()) + )); + dataService.add(drm.getRecipeId()); + // TODO Make it an Optional so that we don't need to touch Null? + return new ImmutablePair<>(drm.getRecipeId(), null); + } + // TODO Implementation + private Pair handleFavouriteRecipeMessage(FavouriteRecipeMessage frm) { + throw new NotImplementedException("TODO:: implement favourited recipe deletion warning"); } private void initRecipeList() {