Made Delete msg take Recipe instead of id and made alert for recipe mourning.

This commit is contained in:
Oskar Rasieński 2026-01-16 19:13:35 +01:00
commit caeacf86e0
2 changed files with 33 additions and 15 deletions

View file

@ -122,7 +122,7 @@ public class FoodpalApplicationCtrl implements LocaleAware {
};
case DeleteRecipeMessage _ -> (m) -> {
DeleteRecipeMessage drm = (DeleteRecipeMessage) m;
logger.info("Server informs us of the deletion of recipe with ID: " + drm.getRecipeId());
logger.info("Server informs us of the deletion of recipe: " + drm.getRecipe());
return handleDeleteRecipeMessage(drm);
};
case FavouriteRecipeMessage _ -> (m) -> {
@ -155,12 +155,28 @@ public class FoodpalApplicationCtrl implements LocaleAware {
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())
Recipe recipe = drm.getRecipe();
// If it's not pending means other client deleted.
boolean externalDelete = !dataService.isPending(recipe.getId());
this.recipeList.getItems().remove(findRecipeById(recipe.getId()).orElseThrow(
() -> new InvalidModificationException("Invalid recipe id during delete: " + recipe.getId())
));
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);
// Show an alert to mourn a lost comrade (recipe).
if (externalDelete && config.isFavourite(recipe.getId())) {
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("Mourn your loss!!!");
alert.setHeaderText(null);
alert.setContentText("Your most beloved recipe by the name of \"" +
recipe.getName() + "\" has been removed.");
alert.showAndWait();
}
dataService.add(recipe.getId());
return new ImmutablePair<>(recipe.getId(), recipe);
}
// TODO Implementation
private Pair<Long, Recipe> handleFavouriteRecipeMessage(FavouriteRecipeMessage frm) {

View file

@ -1,17 +1,19 @@
package commons.ws.messages;
import commons.Recipe;
/**
* Message sent when a recipe is deleted.
*
* @see commons.ws.messages.Message.Type#RECIPE_DELETE
*/
public class DeleteRecipeMessage implements Message {
private Long recipeId;
private Recipe recipe;
public DeleteRecipeMessage() {} // for jackson
public DeleteRecipeMessage(Long recipeId) {
this.recipeId = recipeId;
public DeleteRecipeMessage(Recipe recipe) {
this.recipe = recipe;
}
@Override
@ -20,16 +22,16 @@ public class DeleteRecipeMessage implements Message {
}
/**
* Get the ID of the deleted recipe.
* Get the deleted recipe.
*
* @return The ID of the deleted recipe.
* @return The deleted recipe.
*/
public Long getRecipeId() {
return recipeId;
public Recipe getRecipe() {
return recipe;
}
// for jackson
public void setRecipeId(Long recipeId) {
this.recipeId = recipeId;
public void setRecipe(Recipe recipe) {
this.recipe = recipe;
}
}