Skip to content

Commit

Permalink
update ingredient
Browse files Browse the repository at this point in the history
  • Loading branch information
IusanMihai committed Mar 4, 2021
1 parent b47d7ca commit 83909e7
Show file tree
Hide file tree
Showing 8 changed files with 425 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package guru.springframework.controllers;

import guru.springframework.commands.IngredientCommand;
import guru.springframework.commands.RecipeCommand;
import guru.springframework.commands.UnitOfMeasureCommand;
import guru.springframework.services.IngredientService;
import guru.springframework.services.RecipeService;
import guru.springframework.services.UnitOfMeasureService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;

@Slf4j
@AllArgsConstructor
Expand All @@ -17,6 +19,7 @@ public class IngredientController {

private final RecipeService recipeService;
private final IngredientService ingredientService;
private final UnitOfMeasureService unitOfMeasureService;

@GetMapping
@RequestMapping("/recipe/{recipeId}/ingredients")
Expand All @@ -36,4 +39,56 @@ public String showRecipeIngredient(@PathVariable String recipeId,
model.addAttribute("ingredient", ingredientService.findByRecipeIdAndIngredientId(Long.valueOf(recipeId), Long.valueOf(id)));
return "recipe/ingredient/show";
}

@GetMapping
@RequestMapping("recipe/{recipeId}/ingredient/new")
public String newRecipe(@PathVariable String recipeId, Model model){

//make sure we have a good id value
RecipeCommand recipeCommand = recipeService.findCommandById(Long.valueOf(recipeId));
//todo raise exception if null

//need to return back parent id for hidden form property
IngredientCommand ingredientCommand = new IngredientCommand();
ingredientCommand.setRecipeId(Long.valueOf(recipeId));
model.addAttribute("ingredient", ingredientCommand);

//init uom
ingredientCommand.setUom(new UnitOfMeasureCommand());

model.addAttribute("uomList", unitOfMeasureService.listAllUoms());

return "recipe/ingredient/ingredientform";
}

@GetMapping
@RequestMapping("recipe/{recipeId}/ingredient/{id}/update")
public String updateRecipeIngredient(@PathVariable String recipeId,
@PathVariable String id, Model model){
model.addAttribute("ingredient", ingredientService.findByRecipeIdAndIngredientId(Long.valueOf(recipeId), Long.valueOf(id)));

model.addAttribute("uomList", unitOfMeasureService.listAllUoms());
return "recipe/ingredient/ingredientform";
}

@PostMapping("recipe/{recipeId}/ingredient")
public String saveOrUpdate(@ModelAttribute IngredientCommand command){
IngredientCommand savedCommand = ingredientService.saveIngredientCommand(command);

log.debug("saved receipe id:" + savedCommand.getRecipeId());
log.debug("saved ingredient id:" + savedCommand.getId());

return "redirect:/recipe/" + savedCommand.getRecipeId() + "/ingredient/" + savedCommand.getId() + "/show";
}

@GetMapping
@RequestMapping("recipe/{recipeId}/ingredient/{id}/delete")
public String deleteIngredient(@PathVariable String recipeId,
@PathVariable String id){

log.debug("deleting ingredient id:" + id);
ingredientService.deleteById(Long.valueOf(recipeId), Long.valueOf(id));

return "redirect:/recipe/" + recipeId + "/ingredients";
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package guru.springframework.services;

import guru.springframework.commands.IngredientCommand;
import guru.springframework.converters.IngredientCommandToIngredient;
import guru.springframework.converters.IngredientToIngredientCommand;
import guru.springframework.domain.Ingredient;
import guru.springframework.domain.Recipe;
import guru.springframework.repositories.RecipeRepository;
import guru.springframework.repositories.UnitOfMeasureRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

Expand All @@ -16,7 +20,9 @@
public class IngredientServiceImpl implements IngredientService {

private final IngredientToIngredientCommand ingredientToIngredientCommand;
private final IngredientCommandToIngredient ingredientCommandToIngredient;
private final RecipeRepository recipeRepository;
private final UnitOfMeasureRepository unitOfMeasureRepository;

@Override
public IngredientCommand findByRecipeIdAndIngredientId(Long recipeId, Long ingredientId) {
Expand All @@ -43,12 +49,86 @@ public IngredientCommand findByRecipeIdAndIngredientId(Long recipeId, Long ingre
}

@Override
@Transactional
public IngredientCommand saveIngredientCommand(IngredientCommand command) {
return null;
Optional<Recipe> recipeOptional = recipeRepository.findById(command.getRecipeId());

if(!recipeOptional.isPresent()){

//todo toss error if not found!
log.error("Recipe not found for id: " + command.getRecipeId());
return new IngredientCommand();
} else {
Recipe recipe = recipeOptional.get();

Optional<Ingredient> ingredientOptional = recipe
.getIngredients()
.stream()
.filter(ingredient -> ingredient.getId().equals(command.getId()))
.findFirst();

if(ingredientOptional.isPresent()){
Ingredient ingredientFound = ingredientOptional.get();
ingredientFound.setDescription(command.getDescription());
ingredientFound.setAmount(command.getAmount());
ingredientFound.setUom(unitOfMeasureRepository
.findById(command.getUom().getId())
.orElseThrow(() -> new RuntimeException("UOM NOT FOUND"))); //todo address this
} else {
//add new Ingredient
Ingredient ingredient = ingredientCommandToIngredient.convert(command);
ingredient.setRecipe(recipe);
recipe.addIngredient(ingredient);
}

Recipe savedRecipe = recipeRepository.save(recipe);

Optional<Ingredient> savedIngredientOptional = savedRecipe.getIngredients().stream()
.filter(recipeIngredients -> recipeIngredients.getId().equals(command.getId()))
.findFirst();

//check by description
if(!savedIngredientOptional.isPresent()){
//not totally safe... But best guess
savedIngredientOptional = savedRecipe.getIngredients().stream()
.filter(recipeIngredients -> recipeIngredients.getDescription().equals(command.getDescription()))
.filter(recipeIngredients -> recipeIngredients.getAmount().equals(command.getAmount()))
.filter(recipeIngredients -> recipeIngredients.getUom().getId().equals(command.getUom().getId()))
.findFirst();
}

//to do check for fail
return ingredientToIngredientCommand.convert(savedIngredientOptional.get());
}

}

@Override
public void deleteById(Long recipeId, Long idToDelete) {

log.debug("Deleting ingredient: " + recipeId + ":" + idToDelete);

Optional<Recipe> recipeOptional = recipeRepository.findById(recipeId);

if(recipeOptional.isPresent()){
Recipe recipe = recipeOptional.get();
log.debug("found recipe");

Optional<Ingredient> ingredientOptional = recipe
.getIngredients()
.stream()
.filter(ingredient -> ingredient.getId().equals(idToDelete))
.findFirst();

if(ingredientOptional.isPresent()){
log.debug("found Ingredient");
Ingredient ingredientToDelete = ingredientOptional.get();
ingredientToDelete.setRecipe(null);
recipe.getIngredients().remove(ingredientOptional.get());
recipeRepository.save(recipe);
}
} else {
log.debug("Recipe Id Not found. Id:" + recipeId);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package guru.springframework.services;

import guru.springframework.commands.UnitOfMeasureCommand;

import java.util.Set;

public interface UnitOfMeasureService {
Set<UnitOfMeasureCommand> listAllUoms();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package guru.springframework.services;

import guru.springframework.commands.UnitOfMeasureCommand;
import guru.springframework.converters.UnitOfMeasureToUnitOfMeasureCommand;
import guru.springframework.repositories.UnitOfMeasureRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@Slf4j
@AllArgsConstructor
@Service
public class UnitOfMeasureServiceImpl implements UnitOfMeasureService {
private final UnitOfMeasureRepository unitOfMeasureRepository;
private final UnitOfMeasureToUnitOfMeasureCommand unitOfMeasureToUnitOfMeasureCommand;

@Override
public Set<UnitOfMeasureCommand> listAllUoms() {
return StreamSupport.stream(unitOfMeasureRepository.findAll().spliterator(),false)
.map(unitOfMeasureToUnitOfMeasureCommand::convert)
.collect(Collectors.toSet());
}
}
64 changes: 64 additions & 0 deletions src/main/resources/templates/recipe/ingredient/ingredientform.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>Edit Ingredient</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"
integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"
th:href="@{/webjars/bootstrap/3.3.7-1/css/bootstrap.min.css}">

<script src="/webjars/jquery/1.11.1/jquery.min.js"></script>

<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"
integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa"
crossorigin="anonymous" th:src="@{/webjars/bootstrap/3.3.7-1/js/bootstrap.min.js}"></script>
</head>
<body>

<div class="container-fluid" style="margin-top: 20px">
<div class="row">
<div class="col-md-6 col-md-offset-3">
<!--/*@thymesVar id="ingredient" type="guru.springframework.commands.IngredientCommand"*/-->
<form th:object="${ingredient}" th:action="@{'/recipe/' + ${ingredient.getRecipeId()} + '/ingredient'} " method="post">
<input type="hidden" th:field="*{id}"/>
<div class="pannel-group">
<div class="panel panel-primary">
<div class="panel-heading">
<h1 class="panel-title">Edit Ingredient Information</h1>
</div>
<div class="panel-body">


<div class="row">
<div class="col-md-3 form-group">
<label>Description:</label>
<input type="text" class="form-control" th:field="*{description}"/>
</div>

<div class="col-md-3 form-group">
<label>Amount:</label>
<input type="number" class="form-control" th:field="*{amount}"/>
</div>

<div class="col-md-3 form-group">
<label>UOM:</label>
<select class="form-control" name="uom.id">
<option th:each="uomEach : ${uomList}"
th:value="${uomEach.id}"
th:selected="${uomEach.id.equals(ingredient.uom.id)}"
th:text="${uomEach.description}">Each</option>
</select>
</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Loading

0 comments on commit 83909e7

Please sign in to comment.