Merge branch 'mourning-recipes' into 'main'

Mourning recipes

See merge request cse1105/2025-2026/teams/csep-team-76!65
This commit is contained in:
Oskar Rasieński 2026-01-16 22:10:57 +01:00
commit f6d5551408
5 changed files with 61 additions and 20 deletions

View file

@ -127,7 +127,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) -> {
@ -160,12 +160,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

@ -58,7 +58,12 @@ public class WebSocketDataService<ID, Value> {
logger.info("Item " + id + " pending propagation. Adding to pending register.");
return pendingRegister.putIfAbsent(id, future) == null;
}
public boolean add(ID id) {
return add(id, (_) -> {});
}
public boolean isPending(ID id) {
return pendingRegister.containsKey(id);
}
}