refactor(client/ws): divorce WS data handler from the class constructor
This commit is contained in:
parent
5de11428a8
commit
9f892e8fb1
1 changed files with 45 additions and 28 deletions
|
|
@ -38,6 +38,7 @@ import javafx.scene.control.ListView;
|
||||||
import javafx.scene.control.ToggleButton;
|
import javafx.scene.control.ToggleButton;
|
||||||
import org.apache.commons.lang3.NotImplementedException;
|
import org.apache.commons.lang3.NotImplementedException;
|
||||||
import org.apache.commons.lang3.tuple.ImmutablePair;
|
import org.apache.commons.lang3.tuple.ImmutablePair;
|
||||||
|
import org.apache.commons.lang3.tuple.Pair;
|
||||||
|
|
||||||
public class FoodpalApplicationCtrl implements LocaleAware {
|
public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
private final ServerUtils server;
|
private final ServerUtils server;
|
||||||
|
|
@ -91,46 +92,62 @@ public class FoodpalApplicationCtrl implements LocaleAware {
|
||||||
|
|
||||||
this.configService = configService;
|
this.configService = configService;
|
||||||
this.dataService = recipeDataService;
|
this.dataService = recipeDataService;
|
||||||
recipeDataService.setMessageParser((msg) -> switch (msg) {
|
setupDataService();
|
||||||
|
initializeWebSocket();
|
||||||
|
}
|
||||||
|
private void setupDataService() {
|
||||||
|
dataService.setMessageParser((msg) -> switch (msg) {
|
||||||
case CreateRecipeMessage _ -> (m) -> {
|
case CreateRecipeMessage _ -> (m) -> {
|
||||||
CreateRecipeMessage crm = (CreateRecipeMessage) m;
|
CreateRecipeMessage crm = (CreateRecipeMessage) m;
|
||||||
this.recipeList.getItems().add(crm.getRecipe());
|
return handleCreateRecipeMessage(crm);
|
||||||
dataService.add(crm.getRecipe().getId());
|
|
||||||
return new ImmutablePair<>(crm.getRecipe().getId(), crm.getRecipe());
|
|
||||||
};
|
};
|
||||||
case UpdateRecipeMessage _ -> (m) -> {
|
case UpdateRecipeMessage _ -> (m) -> {
|
||||||
UpdateRecipeMessage upm = (UpdateRecipeMessage) m;
|
UpdateRecipeMessage urm = (UpdateRecipeMessage) m;
|
||||||
Recipe recipe = upm.getRecipe();
|
return handleUpdateRecipeMessage(urm);
|
||||||
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);
|
|
||||||
};
|
};
|
||||||
case DeleteRecipeMessage _ -> (m) -> {
|
case DeleteRecipeMessage _ -> (m) -> {
|
||||||
DeleteRecipeMessage drm = (DeleteRecipeMessage) m;
|
DeleteRecipeMessage drm = (DeleteRecipeMessage) m;
|
||||||
this.recipeList.getItems().remove(findRecipeById(drm.getRecipeId()).orElseThrow(
|
return handleDeleteRecipeMessage(drm);
|
||||||
() -> new InvalidModificationException("Invalid recipe id during delete: " + drm.getRecipeId())
|
|
||||||
));
|
|
||||||
dataService.add(drm.getRecipeId());
|
|
||||||
// TODO Make it an Optional<Recipe> so that we don't need to touch Null?
|
|
||||||
return new ImmutablePair<>(drm.getRecipeId(), null);
|
|
||||||
};
|
};
|
||||||
case FavouriteRecipeMessage _ -> (m) -> {
|
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);
|
default -> throw new IllegalStateException("Unexpected value: " + msg);
|
||||||
});
|
});
|
||||||
initializeWebSocket();
|
}
|
||||||
|
|
||||||
|
private Pair<Long, Recipe> handleCreateRecipeMessage(CreateRecipeMessage crm) {
|
||||||
|
this.recipeList.getItems().add(crm.getRecipe());
|
||||||
|
dataService.add(crm.getRecipe().getId());
|
||||||
|
return new ImmutablePair<>(crm.getRecipe().getId(), crm.getRecipe());
|
||||||
|
}
|
||||||
|
private Pair<Long, Recipe> 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<Long, Recipe> 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<Recipe> so that we don't need to touch Null?
|
||||||
|
return new ImmutablePair<>(drm.getRecipeId(), null);
|
||||||
|
}
|
||||||
|
// TODO Implementation
|
||||||
|
private Pair<Long, Recipe> handleFavouriteRecipeMessage(FavouriteRecipeMessage frm) {
|
||||||
|
throw new NotImplementedException("TODO:: implement favourited recipe deletion warning");
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initRecipeList() {
|
private void initRecipeList() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue