Updateable pantry status

This commit is contained in:
Tim Holloway 2022-01-16 17:33:20 -05:00
parent ec823ff784
commit 95641cf00b
4 changed files with 83 additions and 6 deletions

View File

@ -16,16 +16,19 @@ import org.primefaces.model.StreamedContent;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.mousetech.gourmetj.persistence.dao.PantryRepository;
import com.mousetech.gourmetj.persistence.dao.ShopcatRepository;
import com.mousetech.gourmetj.persistence.model.Ingredient;
import com.mousetech.gourmetj.persistence.model.Pantry;
import com.mousetech.gourmetj.persistence.model.Recipe;
import com.mousetech.gourmetj.persistence.model.Shopcat;
import com.mousetech.gourmetj.persistence.service.RecipeService;
import com.mousetech.gourmetj.utils.YamlShoppingList;
/** Backing bean for the Shopping List tab of the "More..."
* page.
/**
* Backing bean for the Shopping List tab of the "More..." page.
* _TestedBy ShoppingListBeanTest
*
* @author timh
* @since Jan 16, 2022
*/
@ -104,6 +107,11 @@ public class ShoppingListBean implements Serializable {
buildMaps();
}
public void clearRecipeList() {
this.userSession.getShoppingList().clear();
getRecipeList().clear();
}
public List<RecipeReference> getRecipeList() {
if (this.recipeList == null) {
this.recipeList = loadRecipeList();
@ -111,14 +119,29 @@ public class ShoppingListBean implements Serializable {
return this.recipeList;
}
@Inject
RecipeService recipeService;
private List<RecipeReference> loadRecipeList() {
List<RecipeReference> list =
userSession.getShoppingList().stream()
.map(r -> new RecipeReference(r))
.map(r -> new RecipeReference(fetchRecipe(r)))
.collect(Collectors.toList());
return list;
}
/**
* Fetch the current version of a recipe. The reference
* version may be out of date if the recipe was edited or
* the Pantry status of one or more Ingredients changed.
* @param r reference recipe
* @return current version of reference recipe
*/
private Recipe fetchRecipe(Recipe r) {
Recipe rnew = this.recipeService.findDetails(r.getId());
return rnew;
}
public List<ShopIngredient> getIngredientList() {
return this.siList;
}
@ -242,6 +265,28 @@ public class ShoppingListBean implements Serializable {
.createDownload(getIngredientList());
}
@Inject
PantryRepository pantryRepository;
/**
* Listener for when Pantry checkbox is checked/unchecked
* @param ing Ingredient to update
*/
public void updatePantry(ShopIngredient ing) {
String ingKey = ing.getIngkey();
boolean checked = ing.isInPantry();
Pantry pantry = pantryRepository.findPantryByIngkey(ingKey);
if ( pantry == null ) {
pantry = new Pantry(ingKey, checked);
} else {
pantry.setPantry(checked);
}
pantryRepository.save(pantry);
// Reset recipes to cover ingredient change
this.recipeList = null;
}
// =============================================
private List<String> shopcatList;

View File

@ -13,6 +13,7 @@ public class Pantry implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;
@ -28,6 +29,11 @@ public class Pantry implements Serializable {
public Pantry() {
}
public Pantry(String ingKey, boolean checked) {
this.ingkey = ingKey;
this.pantry = checked;
}
public Long getId() {
return this.id;
}

View File

@ -49,6 +49,10 @@ public class YamlShoppingList {
if (StringUtils.isBlank(newShopcat)) {
newShopcat = "Unassigned";
}
if ( ing.isInPantry()) {
// Don't put pantry items in list.
continue;
}
if (!StringUtils.equals(newShopcat, oldShopcat)) {
wtr.println(newShopcat + ":");
oldShopcat = newShopcat;

View File

@ -47,6 +47,12 @@
>
<f:facet name="header">
<h:outputText value="Recipes" />
<p:commandButton
update="@parent:tblRecipes"
value="Clear Recipes"
immediate="true"
action="#{shoppingListBean.clearRecipeList}"
/>
</f:facet>
<p:column style="width: 4em">
<p:spinner required="true" min="0"
@ -113,11 +119,23 @@
<p:column label="Item"
style="width: 20em"
>
<h:outputText
<h:outputText id="outItem"
value="#{item.item}"
styleClass="#{(item.inPantry) ? 'noRecipe' :'plusRecipe' }"
/>
</p:column>
<p:column style="width: 2em">
<f:facet name="header">
<h:outputText value="Pantry" />
</f:facet>
<p:selectBooleanCheckbox
value="#{item.inPantry}"
>
<p:ajax update="outItem"
listener="#{shoppingListBean.updatePantry(item)}"
/>
</p:selectBooleanCheckbox>
</p:column>
</p:dataTable>
</p:column>
</h:form>
@ -131,6 +149,10 @@
/>
</p:tab>
<!-- -->
<p:tab id="tabPantry" title="Pantry">
<h:outputText value="For future implementation" />
</p:tab>
<!-- -->
<p:tab id="tabImportExport" title="Import/Export">
<h:outputText value="For future implementation" />
</p:tab>