Skip to content

Commit

Permalink
Adjusting to no relational model
Browse files Browse the repository at this point in the history
  • Loading branch information
mephisto2120 committed Nov 16, 2021
1 parent ae7c614 commit 66aa944
Show file tree
Hide file tree
Showing 11 changed files with 23 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

/**
* Created by jt on 6/28/17.
*/
@Slf4j
@Controller
public class IngredientController {
Expand Down Expand Up @@ -57,7 +54,6 @@ public String newRecipe(@PathVariable String recipeId, Model model){

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

//init uom
Expand All @@ -81,7 +77,6 @@ public String updateRecipeIngredient(@PathVariable String recipeId,
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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

/**
* Created by jt on 6/21/17.
*/
@Component
public class IngredientCommandToIngredient implements Converter<IngredientCommand, Ingredient> {

Expand All @@ -32,7 +29,6 @@ public Ingredient convert(IngredientCommand source) {
if(source.getRecipeId() != null){
Recipe recipe = new Recipe();
recipe.setId(source.getRecipeId());
ingredient.setRecipe(recipe);
recipe.addIngredient(ingredient);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Component;

/**
* Created by jt on 6/21/17.
*/
@Component
public class IngredientToIngredientCommand implements Converter<Ingredient, IngredientCommand> {

Expand All @@ -29,9 +26,6 @@ public IngredientCommand convert(Ingredient ingredient) {

IngredientCommand ingredientCommand = new IngredientCommand();
ingredientCommand.setId(ingredient.getId());
if (ingredient.getRecipe() != null) {
ingredientCommand.setRecipeId(ingredient.getRecipe().getId());
}
ingredientCommand.setAmount(ingredient.getAmount());
ingredientCommand.setDescription(ingredient.getDescription());
ingredientCommand.setUom(uomConverter.convert(ingredient.getUom()));
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/guru/springframework/domain/Category.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.Set;

/**
* Created by jt on 6/13/17.
*/
@Getter
@Setter
@Document
public class Category {
private String id;
private String description;
@DBRef
private Set<Recipe> recipes;
}
4 changes: 0 additions & 4 deletions src/main/java/guru/springframework/domain/Difficulty.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package guru.springframework.domain;

/**
* Created by jt on 6/13/17.
*/
public enum Difficulty {

EASY, MODERATE, KIND_OF_HARD, HARD
}
7 changes: 4 additions & 3 deletions src/main/java/guru/springframework/domain/Ingredient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;

import java.math.BigDecimal;

/**
* Created by jt on 6/13/17.
*/
@Getter
@Setter
public class Ingredient {

@Id
private String id;
private String description;
private BigDecimal amount;

@DBRef
private UnitOfMeasure uom;
private Recipe recipe;

Expand Down
8 changes: 4 additions & 4 deletions src/main/java/guru/springframework/domain/Notes.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;


/**
* Created by jt on 6/13/17.
*/
@Getter
@Setter
@Document
public class Notes {

@Id
private String id;
private Recipe recipe;
private String recipeNotes;
Expand Down
13 changes: 7 additions & 6 deletions src/main/java/guru/springframework/domain/Recipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

import java.util.HashSet;
import java.util.Set;

/**
* Created by jt on 6/13/17.
*/
@Getter
@Setter
@Document
public class Recipe {

@Id
private String id;
private String description;
private Integer prepTime;
Expand All @@ -26,17 +28,16 @@ public class Recipe {
private Difficulty difficulty;
private Notes notes;

@DBRef
private Set<Category> categories = new HashSet<>();

public void setNotes(Notes notes) {
if (notes != null) {
this.notes = notes;
notes.setRecipe(this);
}
}

public Recipe addIngredient(Ingredient ingredient){
ingredient.setRecipe(this);
public Recipe addIngredient(Ingredient ingredient) {
this.ingredients.add(ingredient);
return this;
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/guru/springframework/domain/UnitOfMeasure.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

import lombok.Getter;
import lombok.Setter;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;


/**
* Created by jt on 6/13/17.
*/
@Getter
@Setter
@Document
public class UnitOfMeasure {

@Id
private String id;
private String description;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@

import static org.junit.Assert.*;

/**
* Created by jt on 6/21/17.
*/
public class IngredientToIngredientCommandTest {

public static final Recipe RECIPE = new Recipe();
Expand Down Expand Up @@ -45,7 +42,6 @@ public void testConvertNullUOM() throws Exception {
//given
Ingredient ingredient = new Ingredient();
ingredient.setId(ID_VALUE);
ingredient.setRecipe(RECIPE);
ingredient.setAmount(AMOUNT);
ingredient.setDescription(DESCRIPTION);
ingredient.setUom(null);
Expand All @@ -63,7 +59,6 @@ public void testConvertWithUom() throws Exception {
//given
Ingredient ingredient = new Ingredient();
ingredient.setId(ID_VALUE);
ingredient.setRecipe(RECIPE);
ingredient.setAmount(AMOUNT);
ingredient.setDescription(DESCRIPTION);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ public void findByRecipeIdAndReceipeIdHappyPath() throws Exception {

//when
assertEquals("3", ingredientCommand.getId());
assertEquals("1", ingredientCommand.getRecipeId());
verify(recipeRepository, times(1)).findById(anyString());
}

Expand Down Expand Up @@ -116,7 +115,6 @@ public void testDeleteById() throws Exception {
Ingredient ingredient = new Ingredient();
ingredient.setId("3");
recipe.addIngredient(ingredient);
ingredient.setRecipe(recipe);
Optional<Recipe> recipeOptional = Optional.of(recipe);

when(recipeRepository.findById(anyString())).thenReturn(recipeOptional);
Expand Down

0 comments on commit 66aa944

Please sign in to comment.