From ae662a3dbdbf0d96e83f054d6b89a26bd72ea02d Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Thu, 11 May 2023 12:59:11 -0500 Subject: [PATCH 01/23] add index page --- README.md | 8 +------- pom.xml | 2 +- .../controllers/IndexController.java | 16 ++++++++++++++++ src/main/resources/templates/index.html | 10 ++++++++++ 4 files changed, 28 insertions(+), 8 deletions(-) create mode 100644 src/main/java/guru/springframework/controllers/IndexController.java create mode 100644 src/main/resources/templates/index.html diff --git a/README.md b/README.md index 0f4f568095..b944ffdcef 100644 --- a/README.md +++ b/README.md @@ -1,7 +1 @@ -# Spring Boot Recipe Application - -[![CircleCI](https://circleci.com/gh/springframeworkguru/spring5-recipe-app.svg?style=svg)](https://circleci.com/gh/springframeworkguru/spring5-recipe-app) - -This repository is for an example application built in my Spring Framework 5 - Beginner to Guru - -You can learn about my Spring Framework 5 Online course [here.](https://go.springframework.guru/spring-framework-5-online-course) \ No newline at end of file +# Spring Boot Recipe Application \ No newline at end of file diff --git a/pom.xml b/pom.xml index 25bb466d7f..222faceb67 100644 --- a/pom.xml +++ b/pom.xml @@ -2,9 +2,9 @@ 4.0.0 + guru.springframework spring5-recipe-app - 0.0.1-SNAPSHOT jar diff --git a/src/main/java/guru/springframework/controllers/IndexController.java b/src/main/java/guru/springframework/controllers/IndexController.java new file mode 100644 index 0000000000..e65fa36e70 --- /dev/null +++ b/src/main/java/guru/springframework/controllers/IndexController.java @@ -0,0 +1,16 @@ +package guru.springframework.controllers; + +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.RequestMapping; + +/** + * Created by jt on 6/1/17. + */ +@Controller +public class IndexController { + + @RequestMapping({"", "/", "/index"}) + public String getIndexPage(){ + return "index"; + } +} diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html new file mode 100644 index 0000000000..bf0f934562 --- /dev/null +++ b/src/main/resources/templates/index.html @@ -0,0 +1,10 @@ + + + + + Recipe Home + + +

My Recipes!

+ + \ No newline at end of file From a851b6fd307f6e13d7239c89423862ad58ff415c Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Thu, 11 May 2023 16:24:09 -0500 Subject: [PATCH 02/23] One To One JPA Relationships --- .../guru/springframework/domain/Notes.java | 44 +++++++ .../guru/springframework/domain/Recipe.java | 110 ++++++++++++++++++ 2 files changed, 154 insertions(+) create mode 100644 src/main/java/guru/springframework/domain/Notes.java create mode 100644 src/main/java/guru/springframework/domain/Recipe.java diff --git a/src/main/java/guru/springframework/domain/Notes.java b/src/main/java/guru/springframework/domain/Notes.java new file mode 100644 index 0000000000..6e247b46bb --- /dev/null +++ b/src/main/java/guru/springframework/domain/Notes.java @@ -0,0 +1,44 @@ +package guru.springframework.domain; + +import javax.persistence.*; + +/** + * Created by jt on 6/13/17. + */ +@Entity +public class Notes { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @OneToOne + private Recipe recipe; + + @Lob + private String recipeNotes; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Recipe getRecipe() { + return recipe; + } + + public void setRecipe(Recipe recipe) { + this.recipe = recipe; + } + + public String getRecipeNotes() { + return recipeNotes; + } + + public void setRecipeNotes(String recipeNotes) { + this.recipeNotes = recipeNotes; + } +} diff --git a/src/main/java/guru/springframework/domain/Recipe.java b/src/main/java/guru/springframework/domain/Recipe.java new file mode 100644 index 0000000000..1d609385bf --- /dev/null +++ b/src/main/java/guru/springframework/domain/Recipe.java @@ -0,0 +1,110 @@ +package guru.springframework.domain; + +import javax.persistence.*; + +/** + * Created by jt on 6/13/17. + */ +@Entity +public class Recipe { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String description; + private Integer prepTime; + private Integer cookTime; + private Integer servings; + private String source; + private String url; + private String directions; + //todo add + //private Difficulty difficulty; + + @Lob + private Byte[] image; + + @OneToOne(cascade = CascadeType.ALL) + private Notes notes; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Integer getPrepTime() { + return prepTime; + } + + public void setPrepTime(Integer prepTime) { + this.prepTime = prepTime; + } + + public Integer getCookTime() { + return cookTime; + } + + public void setCookTime(Integer cookTime) { + this.cookTime = cookTime; + } + + public Integer getServings() { + return servings; + } + + public void setServings(Integer servings) { + this.servings = servings; + } + + public String getSource() { + return source; + } + + public void setSource(String source) { + this.source = source; + } + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getDirections() { + return directions; + } + + public void setDirections(String directions) { + this.directions = directions; + } + + public Byte[] getImage() { + return image; + } + + public void setImage(Byte[] image) { + this.image = image; + } + + public Notes getNotes() { + return notes; + } + + public void setNotes(Notes notes) { + this.notes = notes; + } +} From 0a22621fc938dd69a48a092f2fcd299409aed818 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Thu, 11 May 2023 16:46:25 -0500 Subject: [PATCH 03/23] One To Many JPA Relationships --- .../springframework/domain/Ingredient.java | 52 +++++++++++++++++++ .../guru/springframework/domain/Recipe.java | 12 +++++ 2 files changed, 64 insertions(+) create mode 100644 src/main/java/guru/springframework/domain/Ingredient.java diff --git a/src/main/java/guru/springframework/domain/Ingredient.java b/src/main/java/guru/springframework/domain/Ingredient.java new file mode 100644 index 0000000000..29091ac397 --- /dev/null +++ b/src/main/java/guru/springframework/domain/Ingredient.java @@ -0,0 +1,52 @@ +package guru.springframework.domain; + + +import javax.persistence.*; +import java.math.BigDecimal; + +@Entity +public class Ingredient { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String description; + private BigDecimal amount; + + //private UnitOfMeasure uom; + + @ManyToOne + private Recipe recipe; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public BigDecimal getAmount() { + return amount; + } + + public void setAmount(BigDecimal amount) { + this.amount = amount; + } + + public Recipe getRecipe() { + return recipe; + } + + public void setRecipe(Recipe recipe) { + this.recipe = recipe; + } +} diff --git a/src/main/java/guru/springframework/domain/Recipe.java b/src/main/java/guru/springframework/domain/Recipe.java index 1d609385bf..cc626483d2 100644 --- a/src/main/java/guru/springframework/domain/Recipe.java +++ b/src/main/java/guru/springframework/domain/Recipe.java @@ -1,6 +1,7 @@ package guru.springframework.domain; import javax.persistence.*; +import java.util.Set; /** * Created by jt on 6/13/17. @@ -22,6 +23,9 @@ public class Recipe { //todo add //private Difficulty difficulty; + @OneToMany(cascade = CascadeType.ALL, mappedBy = "recipe") + private Set ingredients; + @Lob private Byte[] image; @@ -92,6 +96,14 @@ public void setDirections(String directions) { this.directions = directions; } + public Set getIngredients() { + return ingredients; + } + + public void setIngredients(Set ingredients) { + this.ingredients = ingredients; + } + public Byte[] getImage() { return image; } From f1b29c6f456175cc646a912f290dba250ae89f56 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Thu, 11 May 2023 16:58:43 -0500 Subject: [PATCH 04/23] added UnitOfMeasure and relation --- .../springframework/domain/Ingredient.java | 11 ++++- .../springframework/domain/UnitOfMeasure.java | 40 +++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 src/main/java/guru/springframework/domain/UnitOfMeasure.java diff --git a/src/main/java/guru/springframework/domain/Ingredient.java b/src/main/java/guru/springframework/domain/Ingredient.java index 29091ac397..f0cb078e54 100644 --- a/src/main/java/guru/springframework/domain/Ingredient.java +++ b/src/main/java/guru/springframework/domain/Ingredient.java @@ -13,7 +13,8 @@ public class Ingredient { private String description; private BigDecimal amount; - //private UnitOfMeasure uom; + @OneToOne(fetch = FetchType.EAGER) + private UnitOfMeasure uom; @ManyToOne private Recipe recipe; @@ -42,6 +43,14 @@ public void setAmount(BigDecimal amount) { this.amount = amount; } + public UnitOfMeasure getUom() { + return uom; + } + + public void setUom(UnitOfMeasure uom) { + this.uom = uom; + } + public Recipe getRecipe() { return recipe; } diff --git a/src/main/java/guru/springframework/domain/UnitOfMeasure.java b/src/main/java/guru/springframework/domain/UnitOfMeasure.java new file mode 100644 index 0000000000..a82c776dc2 --- /dev/null +++ b/src/main/java/guru/springframework/domain/UnitOfMeasure.java @@ -0,0 +1,40 @@ +package guru.springframework.domain; + +import javax.persistence.*; + +@Entity +public class UnitOfMeasure { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String description; + + @OneToOne + private Ingredient ingredient; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Ingredient getIngredient() { + return ingredient; + } + + public void setIngredient(Ingredient ingredient) { + this.ingredient = ingredient; + } + +} From 82ddd4b345140b4ae6e68eeccd11767d31fe9900 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Thu, 11 May 2023 17:02:53 -0500 Subject: [PATCH 05/23] . --- .../guru/springframework/domain/UnitOfMeasure.java | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/src/main/java/guru/springframework/domain/UnitOfMeasure.java b/src/main/java/guru/springframework/domain/UnitOfMeasure.java index a82c776dc2..3c55919977 100644 --- a/src/main/java/guru/springframework/domain/UnitOfMeasure.java +++ b/src/main/java/guru/springframework/domain/UnitOfMeasure.java @@ -9,10 +9,7 @@ public class UnitOfMeasure { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String description; - - @OneToOne - private Ingredient ingredient; - + public Long getId() { return id; } @@ -29,12 +26,5 @@ public void setDescription(String description) { this.description = description; } - public Ingredient getIngredient() { - return ingredient; - } - - public void setIngredient(Ingredient ingredient) { - this.ingredient = ingredient; - } } From 3ef1fee0658075cdb6eac00655fb19826e6eb853 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Fri, 12 May 2023 11:47:34 -0500 Subject: [PATCH 06/23] added difficulty enum --- .../java/guru/springframework/domain/Difficulty.java | 9 +++++++++ src/main/java/guru/springframework/domain/Recipe.java | 11 +++++++++++ 2 files changed, 20 insertions(+) create mode 100644 src/main/java/guru/springframework/domain/Difficulty.java diff --git a/src/main/java/guru/springframework/domain/Difficulty.java b/src/main/java/guru/springframework/domain/Difficulty.java new file mode 100644 index 0000000000..f375554a10 --- /dev/null +++ b/src/main/java/guru/springframework/domain/Difficulty.java @@ -0,0 +1,9 @@ +package guru.springframework.domain; + +public enum Difficulty { + + EASY, + MODERATE, + IND_OF_HARD, + HARD +} diff --git a/src/main/java/guru/springframework/domain/Recipe.java b/src/main/java/guru/springframework/domain/Recipe.java index cc626483d2..398cd84e84 100644 --- a/src/main/java/guru/springframework/domain/Recipe.java +++ b/src/main/java/guru/springframework/domain/Recipe.java @@ -29,6 +29,9 @@ public class Recipe { @Lob private Byte[] image; + @Enumerated(value = EnumType.STRING) + private Difficulty difficulty; + @OneToOne(cascade = CascadeType.ALL) private Notes notes; @@ -112,6 +115,14 @@ public void setImage(Byte[] image) { this.image = image; } + public Difficulty getDifficulty() { + return difficulty; + } + + public void setDifficulty(Difficulty difficulty) { + this.difficulty = difficulty; + } + public Notes getNotes() { return notes; } From cd3b313840d1bb8f9a83240a9d8e01d386d0fc80 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Fri, 12 May 2023 15:16:57 -0500 Subject: [PATCH 07/23] Many To Many JPA Relationships --- .../guru/springframework/domain/Category.java | 40 +++++++++++++++++++ .../guru/springframework/domain/Recipe.java | 14 +++++++ 2 files changed, 54 insertions(+) create mode 100644 src/main/java/guru/springframework/domain/Category.java diff --git a/src/main/java/guru/springframework/domain/Category.java b/src/main/java/guru/springframework/domain/Category.java new file mode 100644 index 0000000000..2214f87aea --- /dev/null +++ b/src/main/java/guru/springframework/domain/Category.java @@ -0,0 +1,40 @@ +package guru.springframework.domain; + +import javax.persistence.*; +import java.util.Set; + +@Entity +public class Category { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + private String description; + + @ManyToMany(mappedBy = "categories") + private Set recipes; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + public Set getRecipes() { + return recipes; + } + + public void setRecipes(Set recipes) { + this.recipes = recipes; + } +} diff --git a/src/main/java/guru/springframework/domain/Recipe.java b/src/main/java/guru/springframework/domain/Recipe.java index 398cd84e84..5f792f2ab8 100644 --- a/src/main/java/guru/springframework/domain/Recipe.java +++ b/src/main/java/guru/springframework/domain/Recipe.java @@ -35,6 +35,12 @@ public class Recipe { @OneToOne(cascade = CascadeType.ALL) private Notes notes; + @ManyToMany + @JoinTable(name = "recipe_category ", + joinColumns = @JoinColumn(name = "recipe_id"), + inverseJoinColumns = @JoinColumn(name = "category_id")) + private Set categories; + public Long getId() { return id; } @@ -130,4 +136,12 @@ public Notes getNotes() { public void setNotes(Notes notes) { this.notes = notes; } + + public Set getCategories() { + return categories; + } + + public void setCategories(Set categories) { + this.categories = categories; + } } From 333b060c089dea8cb98aceb2741fa2dc82441a54 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Mon, 15 May 2023 15:23:05 -0500 Subject: [PATCH 08/23] create spring data repositories --- .../springframework/repositories/CategoryRepository.java | 7 +++++++ .../springframework/repositories/RecipeRepository.java | 9 +++++++++ .../repositories/UnitOfMeasureRepository.java | 7 +++++++ 3 files changed, 23 insertions(+) create mode 100644 src/main/java/guru/springframework/repositories/CategoryRepository.java create mode 100644 src/main/java/guru/springframework/repositories/RecipeRepository.java create mode 100644 src/main/java/guru/springframework/repositories/UnitOfMeasureRepository.java diff --git a/src/main/java/guru/springframework/repositories/CategoryRepository.java b/src/main/java/guru/springframework/repositories/CategoryRepository.java new file mode 100644 index 0000000000..df7dac3c9c --- /dev/null +++ b/src/main/java/guru/springframework/repositories/CategoryRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.repositories; + +import guru.springframework.domain.Category; +import org.springframework.data.repository.CrudRepository; + +public interface CategoryRepository extends CrudRepository { +} diff --git a/src/main/java/guru/springframework/repositories/RecipeRepository.java b/src/main/java/guru/springframework/repositories/RecipeRepository.java new file mode 100644 index 0000000000..c825c7b2b0 --- /dev/null +++ b/src/main/java/guru/springframework/repositories/RecipeRepository.java @@ -0,0 +1,9 @@ +package guru.springframework.repositories; + +import guru.springframework.domain.Recipe; +import org.springframework.data.repository.CrudRepository; + +public interface RecipeRepository extends CrudRepository{ + + +} diff --git a/src/main/java/guru/springframework/repositories/UnitOfMeasureRepository.java b/src/main/java/guru/springframework/repositories/UnitOfMeasureRepository.java new file mode 100644 index 0000000000..aca01fcdbb --- /dev/null +++ b/src/main/java/guru/springframework/repositories/UnitOfMeasureRepository.java @@ -0,0 +1,7 @@ +package guru.springframework.repositories; + +import guru.springframework.domain.UnitOfMeasure; +import org.springframework.data.repository.CrudRepository; + +public interface UnitOfMeasureRepository extends CrudRepository { +} From f2e326852181fffbaec279d1dd5e620d7783506e Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Tue, 30 May 2023 14:15:27 -0500 Subject: [PATCH 09/23] adding database initialization script --- src/main/resources/data.sql | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/main/resources/data.sql diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql new file mode 100644 index 0000000000..e69de29bb2 From 9db7035f8d313f89457427b592e8095162fb4a8d Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Tue, 30 May 2023 14:15:35 -0500 Subject: [PATCH 10/23] adding database initialization script. --- src/main/resources/data.sql | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index e69de29bb2..b628116c80 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -0,0 +1,9 @@ +INSERT INTO category (description) VALUES ('American'); +INSERT INTO category (description) VALUES ('Italian'); +INSERT INTO category (description) VALUES ('Mexican'); +INSERT INTO category (description) VALUES ('Fast Food'); +INSERT INTO unit_of_measure (description) VALUES ('Teaspoon'); +INSERT INTO unit_of_measure (description) VALUES ('Tablespoon'); +INSERT INTO unit_of_measure (description) VALUES ('Cup'); +INSERT INTO unit_of_measure (description) VALUES ('Pinch'); +INSERT INTO unit_of_measure (description) VALUES ('Ounce'); \ No newline at end of file From 0d18ec19014f44b02fd09071fe496887d332302a Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Tue, 30 May 2023 14:33:03 -0500 Subject: [PATCH 11/23] Adding Query Method Examples --- .../controllers/IndexController.java | 23 ++++++++++++++++++- .../repositories/CategoryRepository.java | 4 ++++ .../repositories/UnitOfMeasureRepository.java | 4 ++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/guru/springframework/controllers/IndexController.java b/src/main/java/guru/springframework/controllers/IndexController.java index e65fa36e70..694b7b1a31 100644 --- a/src/main/java/guru/springframework/controllers/IndexController.java +++ b/src/main/java/guru/springframework/controllers/IndexController.java @@ -1,16 +1,37 @@ package guru.springframework.controllers; +import guru.springframework.domain.Category; +import guru.springframework.domain.UnitOfMeasure; +import guru.springframework.repositories.CategoryRepository; +import guru.springframework.repositories.UnitOfMeasureRepository; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; +import java.util.Optional; + /** * Created by jt on 6/1/17. */ @Controller -public class IndexController { +public class IndexController { + + private final CategoryRepository categoryRepository; + private final UnitOfMeasureRepository unitOfMeasureRepository; + + public IndexController(CategoryRepository categoryRepository, UnitOfMeasureRepository unitOfMeasureRepository) { + this.categoryRepository = categoryRepository; + this.unitOfMeasureRepository = unitOfMeasureRepository; + } @RequestMapping({"", "/", "/index"}) public String getIndexPage(){ + + Optional categoryOptional = categoryRepository.findByDescription("American"); + Optional unitOfMeasureOptional = unitOfMeasureRepository.findByDescription("Teaspoon"); + + System.out.println("Cat Id is: " + categoryOptional.get().getId()); + System.out.println("UOM ID is: " + unitOfMeasureOptional.get().getId()); + return "index"; } } diff --git a/src/main/java/guru/springframework/repositories/CategoryRepository.java b/src/main/java/guru/springframework/repositories/CategoryRepository.java index df7dac3c9c..2fca6df4e7 100644 --- a/src/main/java/guru/springframework/repositories/CategoryRepository.java +++ b/src/main/java/guru/springframework/repositories/CategoryRepository.java @@ -3,5 +3,9 @@ import guru.springframework.domain.Category; import org.springframework.data.repository.CrudRepository; +import java.util.Optional; + public interface CategoryRepository extends CrudRepository { + + Optional findByDescription(String description); } diff --git a/src/main/java/guru/springframework/repositories/UnitOfMeasureRepository.java b/src/main/java/guru/springframework/repositories/UnitOfMeasureRepository.java index aca01fcdbb..76ca864681 100644 --- a/src/main/java/guru/springframework/repositories/UnitOfMeasureRepository.java +++ b/src/main/java/guru/springframework/repositories/UnitOfMeasureRepository.java @@ -3,5 +3,9 @@ import guru.springframework.domain.UnitOfMeasure; import org.springframework.data.repository.CrudRepository; +import java.util.Optional; + public interface UnitOfMeasureRepository extends CrudRepository { + + Optional findByDescription(String description); } From 141dd07f7be63839b90161b6c026b1b6dad26757 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Tue, 30 May 2023 15:21:56 -0500 Subject: [PATCH 12/23] Display List of Recipes --- .../bootstrap/RecipeBootstrap.java | 207 ++++++++++++++++++ .../controllers/IndexController.java | 29 +-- .../springframework/domain/Ingredient.java | 10 + .../guru/springframework/domain/Recipe.java | 13 +- .../services/RecipeService.java | 10 + .../services/RecipeServiceImpl.java | 27 +++ src/main/resources/data.sql | 5 +- src/main/resources/templates/index.html | 11 + 8 files changed, 284 insertions(+), 28 deletions(-) create mode 100644 src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java create mode 100644 src/main/java/guru/springframework/services/RecipeService.java create mode 100644 src/main/java/guru/springframework/services/RecipeServiceImpl.java diff --git a/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java b/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java new file mode 100644 index 0000000000..ee0fd461aa --- /dev/null +++ b/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java @@ -0,0 +1,207 @@ +package guru.springframework.bootstrap; + +import guru.springframework.domain.*; +import guru.springframework.repositories.CategoryRepository; +import guru.springframework.repositories.RecipeRepository; +import guru.springframework.repositories.UnitOfMeasureRepository; +import org.springframework.context.ApplicationListener; +import org.springframework.context.event.ContextRefreshedEvent; +import org.springframework.stereotype.Component; + +import java.math.BigDecimal; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +/** + * Created by jt on 6/13/17. + */ +@Component +public class RecipeBootstrap implements ApplicationListener { + + private final CategoryRepository categoryRepository; + private final RecipeRepository recipeRepository; + private final UnitOfMeasureRepository unitOfMeasureRepository; + + public RecipeBootstrap(CategoryRepository categoryRepository, RecipeRepository recipeRepository, UnitOfMeasureRepository unitOfMeasureRepository) { + this.categoryRepository = categoryRepository; + this.recipeRepository = recipeRepository; + this.unitOfMeasureRepository = unitOfMeasureRepository; + } + + @Override + public void onApplicationEvent(ContextRefreshedEvent event) { + recipeRepository.saveAll(getRecipes()); + } + + private List getRecipes() { + + List recipes = new ArrayList<>(2); + + //get UOMs + Optional eachUomOptional = unitOfMeasureRepository.findByDescription("Each"); + + String uOMNotFound = "Expected UOM Not Found"; + if(!eachUomOptional.isPresent()){ + throw new RuntimeException(uOMNotFound); + } + + Optional tableSpoonUomOptional = unitOfMeasureRepository.findByDescription("Tablespoon"); + + if(!tableSpoonUomOptional.isPresent()){ + throw new RuntimeException(uOMNotFound); + } + + Optional teaSpoonUomOptional = unitOfMeasureRepository.findByDescription("Teaspoon"); + + if(!teaSpoonUomOptional.isPresent()){ + throw new RuntimeException(uOMNotFound); + } + + Optional dashUomOptional = unitOfMeasureRepository.findByDescription("Dash"); + + if(!dashUomOptional.isPresent()){ + throw new RuntimeException(uOMNotFound); + } + + Optional pintUomOptional = unitOfMeasureRepository.findByDescription("Pint"); + + if(!pintUomOptional.isPresent()){ + throw new RuntimeException(uOMNotFound); + } + + Optional cupsUomOptional = unitOfMeasureRepository.findByDescription("Cup"); + + if(!cupsUomOptional.isPresent()){ + throw new RuntimeException(uOMNotFound); + } + + //get optionals + UnitOfMeasure eachUom = eachUomOptional.get(); + UnitOfMeasure tableSpoonUom = tableSpoonUomOptional.get(); + UnitOfMeasure teapoonUom = tableSpoonUomOptional.get(); + UnitOfMeasure dashUom = dashUomOptional.get(); + UnitOfMeasure pintUom = pintUomOptional.get(); + UnitOfMeasure cupsUom = cupsUomOptional.get(); + + //get Categories + Optional americanCategoryOptional = categoryRepository.findByDescription("American"); + + if(!americanCategoryOptional.isPresent()){ + throw new RuntimeException("Expected Category Not Found"); + } + + Optional mexicanCategoryOptional = categoryRepository.findByDescription("Mexican"); + + if(!mexicanCategoryOptional.isPresent()){ + throw new RuntimeException("Expected Category Not Found"); + } + + Category americanCategory = americanCategoryOptional.get(); + Category mexicanCategory = mexicanCategoryOptional.get(); + + //Yummy Guac + Recipe guacRecipe = new Recipe(); + guacRecipe.setDescription("Perfect Guacamole"); + guacRecipe.setPrepTime(10); + guacRecipe.setCookTime(0); + guacRecipe.setDifficulty(Difficulty.EASY); + guacRecipe.setDirections("1 Cut avocado, remove flesh: Cut the avocados in half. Remove seed. Score the inside of the avocado with a blunt knife and scoop out the flesh with a spoon" + + "\n" + + "2 Mash with a fork: Using a fork, roughly mash the avocado. (Don't overdo it! The guacamole should be a little chunky.)" + + "\n" + + "3 Add salt, lime juice, and the rest: Sprinkle with salt and lime (or lemon) juice. The acid in the lime juice will provide some balance to the richness of the avocado and will help delay the avocados from turning brown.\n" + + "Add the chopped onion, cilantro, black pepper, and chiles. Chili peppers vary individually in their hotness. So, start with a half of one chili pepper and add to the guacamole to your desired degree of hotness.\n" + + "Remember that much of this is done to taste because of the variability in the fresh ingredients. Start with this recipe and adjust to your taste.\n" + + "4 Cover with plastic and chill to store: Place plastic wrap on the surface of the guacamole cover it and to prevent air reaching it. (The oxygen in the air causes oxidation which will turn the guacamole brown.) Refrigerate until ready to serve.\n" + + "Chilling tomatoes hurts their flavor, so if you want to add chopped tomato to your guacamole, add it just before serving.\n" + + "\n" + + "\n" + + "Read more: http://www.simplyrecipes.com/recipes/perfect_guacamole/#ixzz4jvpiV9Sd"); + + Notes guacNotes = new Notes(); + guacNotes.setRecipeNotes("For a very quick guacamole just take a 1/4 cup of salsa and mix it in with your mashed avocados.\n" + + "Feel free to experiment! One classic Mexican guacamole has pomegranate seeds and chunks of peaches in it (a Diana Kennedy favorite). Try guacamole with added pineapple, mango, or strawberries.\n" + + "The simplest version of guacamole is just mashed avocados with salt. Don't let the lack of availability of other ingredients stop you from making guacamole.\n" + + "To extend a limited supply of avocados, add either sour cream or cottage cheese to your guacamole dip. Purists may be horrified, but so what? It tastes great.\n" + + "\n" + + "\n" + + "Read more: http://www.simplyrecipes.com/recipes/perfect_guacamole/#ixzz4jvoun5ws"); + guacNotes.setRecipe(guacRecipe); + guacRecipe.setNotes(guacNotes); + + guacRecipe.getIngredients().add(new Ingredient("ripe avocados", new BigDecimal(2), eachUom, guacRecipe)); + guacRecipe.getIngredients().add(new Ingredient("Kosher salt", new BigDecimal(".5"), teapoonUom, guacRecipe)); + guacRecipe.getIngredients().add(new Ingredient("fresh lime juice or lemon juice", new BigDecimal(2), tableSpoonUom, guacRecipe)); + guacRecipe.getIngredients().add(new Ingredient("minced red onion or thinly sliced green onion", new BigDecimal(2), tableSpoonUom, guacRecipe)); + guacRecipe.getIngredients().add(new Ingredient("serrano chiles, stems and seeds removed, minced", new BigDecimal(2), eachUom, guacRecipe)); + guacRecipe.getIngredients().add(new Ingredient("Cilantro", new BigDecimal(2), tableSpoonUom, guacRecipe)); + guacRecipe.getIngredients().add(new Ingredient("freshly grated black pepper", new BigDecimal(2), dashUom, guacRecipe)); + guacRecipe.getIngredients().add(new Ingredient("ripe tomato, seeds and pulp removed, chopped", new BigDecimal(".5"), eachUom, guacRecipe)); + + guacRecipe.getCategories().add(americanCategory); + guacRecipe.getCategories().add(mexicanCategory); + + //add to return list + recipes.add(guacRecipe); + + //Yummy Tacos + Recipe tacosRecipe = new Recipe(); + tacosRecipe.setDescription("Spicy Grilled Chicken Taco"); + tacosRecipe.setCookTime(9); + tacosRecipe.setPrepTime(20); + tacosRecipe.setDifficulty(Difficulty.MODERATE); + + tacosRecipe.setDirections("1 Prepare a gas or charcoal grill for medium-high, direct heat.\n" + + "2 Make the marinade and coat the chicken: In a large bowl, stir together the chili powder, oregano, cumin, sugar, salt, garlic and orange zest. Stir in the orange juice and olive oil to make a loose paste. Add the chicken to the bowl and toss to coat all over.\n" + + "Set aside to marinate while the grill heats and you prepare the rest of the toppings.\n" + + "\n" + + "\n" + + "3 Grill the chicken: Grill the chicken for 3 to 4 minutes per side, or until a thermometer inserted into the thickest part of the meat registers 165F. Transfer to a plate and rest for 5 minutes.\n" + + "4 Warm the tortillas: Place each tortilla on the grill or on a hot, dry skillet over medium-high heat. As soon as you see pockets of the air start to puff up in the tortilla, turn it with tongs and heat for a few seconds on the other side.\n" + + "Wrap warmed tortillas in a tea towel to keep them warm until serving.\n" + + "5 Assemble the tacos: Slice the chicken into strips. On each tortilla, place a small handful of arugula. Top with chicken slices, sliced avocado, radishes, tomatoes, and onion slices. Drizzle with the thinned sour cream. Serve with lime wedges.\n" + + "\n" + + "\n" + + "Read more: http://www.simplyrecipes.com/recipes/spicy_grilled_chicken_tacos/#ixzz4jvtrAnNm"); + + Notes tacoNotes = new Notes(); + tacoNotes.setRecipeNotes("We have a family motto and it is this: Everything goes better in a tortilla.\n" + + "Any and every kind of leftover can go inside a warm tortilla, usually with a healthy dose of pickled jalapenos. I can always sniff out a late-night snacker when the aroma of tortillas heating in a hot pan on the stove comes wafting through the house.\n" + + "Today’s tacos are more purposeful – a deliberate meal instead of a secretive midnight snack!\n" + + "First, I marinate the chicken briefly in a spicy paste of ancho chile powder, oregano, cumin, and sweet orange juice while the grill is heating. You can also use this time to prepare the taco toppings.\n" + + "Grill the chicken, then let it rest while you warm the tortillas. Now you are ready to assemble the tacos and dig in. The whole meal comes together in about 30 minutes!\n" + + "\n" + + "\n" + + "Read more: http://www.simplyrecipes.com/recipes/spicy_grilled_chicken_tacos/#ixzz4jvu7Q0MJ"); + tacoNotes.setRecipe(tacosRecipe); + tacosRecipe.setNotes(tacoNotes); + + + tacosRecipe.getIngredients().add(new Ingredient("Ancho Chili Powder", new BigDecimal(2), tableSpoonUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("Dried Oregano", new BigDecimal(1), teapoonUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("Dried Cumin", new BigDecimal(1), teapoonUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("Sugar", new BigDecimal(1), teapoonUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("Salt", new BigDecimal(".5"), teapoonUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("Clove of Garlic, Choppedr", new BigDecimal(1), eachUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("finely grated orange zestr", new BigDecimal(1), tableSpoonUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("fresh-squeezed orange juice", new BigDecimal(3), tableSpoonUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("Olive Oil", new BigDecimal(2), tableSpoonUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("boneless chicken thighs", new BigDecimal(4), tableSpoonUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("small corn tortillasr", new BigDecimal(8), eachUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("packed baby arugula", new BigDecimal(3), cupsUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("medium ripe avocados, slic", new BigDecimal(2), eachUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("radishes, thinly sliced", new BigDecimal(4), eachUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("cherry tomatoes, halved", new BigDecimal(".5"), pintUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("red onion, thinly sliced", new BigDecimal(".25"), eachUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("Roughly chopped cilantro", new BigDecimal(4), eachUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("cup sour cream thinned with 1/4 cup milk", new BigDecimal(4), cupsUom, tacosRecipe)); + tacosRecipe.getIngredients().add(new Ingredient("lime, cut into wedges", new BigDecimal(4), eachUom, tacosRecipe)); + + tacosRecipe.getCategories().add(americanCategory); + tacosRecipe.getCategories().add(mexicanCategory); + + recipes.add(tacosRecipe); + return recipes; + } +} \ No newline at end of file diff --git a/src/main/java/guru/springframework/controllers/IndexController.java b/src/main/java/guru/springframework/controllers/IndexController.java index 694b7b1a31..92adfe9b85 100644 --- a/src/main/java/guru/springframework/controllers/IndexController.java +++ b/src/main/java/guru/springframework/controllers/IndexController.java @@ -1,36 +1,23 @@ package guru.springframework.controllers; -import guru.springframework.domain.Category; -import guru.springframework.domain.UnitOfMeasure; -import guru.springframework.repositories.CategoryRepository; -import guru.springframework.repositories.UnitOfMeasureRepository; +import guru.springframework.services.RecipeService; import org.springframework.stereotype.Controller; +import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; -import java.util.Optional; - -/** - * Created by jt on 6/1/17. - */ @Controller -public class IndexController { +public class IndexController { - private final CategoryRepository categoryRepository; - private final UnitOfMeasureRepository unitOfMeasureRepository; + private final RecipeService recipeService; - public IndexController(CategoryRepository categoryRepository, UnitOfMeasureRepository unitOfMeasureRepository) { - this.categoryRepository = categoryRepository; - this.unitOfMeasureRepository = unitOfMeasureRepository; + public IndexController(RecipeService recipeService) { + this.recipeService = recipeService; } @RequestMapping({"", "/", "/index"}) - public String getIndexPage(){ - - Optional categoryOptional = categoryRepository.findByDescription("American"); - Optional unitOfMeasureOptional = unitOfMeasureRepository.findByDescription("Teaspoon"); + public String getIndexPage(Model model) { - System.out.println("Cat Id is: " + categoryOptional.get().getId()); - System.out.println("UOM ID is: " + unitOfMeasureOptional.get().getId()); + model.addAttribute("recipes", recipeService.getRecipes()); return "index"; } diff --git a/src/main/java/guru/springframework/domain/Ingredient.java b/src/main/java/guru/springframework/domain/Ingredient.java index f0cb078e54..b2e9b5c3a2 100644 --- a/src/main/java/guru/springframework/domain/Ingredient.java +++ b/src/main/java/guru/springframework/domain/Ingredient.java @@ -19,6 +19,16 @@ public class Ingredient { @ManyToOne private Recipe recipe; + public Ingredient() { + } + + public Ingredient(String description, BigDecimal amount, UnitOfMeasure uom, Recipe recipe) { + this.description = description; + this.amount = amount; + this.uom = uom; + this.recipe = recipe; + } + public Long getId() { return id; } diff --git a/src/main/java/guru/springframework/domain/Recipe.java b/src/main/java/guru/springframework/domain/Recipe.java index 5f792f2ab8..82f35a6ff1 100644 --- a/src/main/java/guru/springframework/domain/Recipe.java +++ b/src/main/java/guru/springframework/domain/Recipe.java @@ -1,6 +1,7 @@ package guru.springframework.domain; import javax.persistence.*; +import java.util.HashSet; import java.util.Set; /** @@ -19,12 +20,12 @@ public class Recipe { private Integer servings; private String source; private String url; + + @Lob private String directions; - //todo add - //private Difficulty difficulty; @OneToMany(cascade = CascadeType.ALL, mappedBy = "recipe") - private Set ingredients; + private Set ingredients = new HashSet<>(); @Lob private Byte[] image; @@ -37,9 +38,9 @@ public class Recipe { @ManyToMany @JoinTable(name = "recipe_category ", - joinColumns = @JoinColumn(name = "recipe_id"), - inverseJoinColumns = @JoinColumn(name = "category_id")) - private Set categories; + joinColumns = @JoinColumn(name = "recipe_id"), + inverseJoinColumns = @JoinColumn(name = "category_id")) + private Set categories = new HashSet<>(); public Long getId() { return id; diff --git a/src/main/java/guru/springframework/services/RecipeService.java b/src/main/java/guru/springframework/services/RecipeService.java new file mode 100644 index 0000000000..fb5d986c6f --- /dev/null +++ b/src/main/java/guru/springframework/services/RecipeService.java @@ -0,0 +1,10 @@ +package guru.springframework.services; + +import guru.springframework.domain.Recipe; + +import java.util.Set; + +public interface RecipeService { + + Set getRecipes(); +} diff --git a/src/main/java/guru/springframework/services/RecipeServiceImpl.java b/src/main/java/guru/springframework/services/RecipeServiceImpl.java new file mode 100644 index 0000000000..2fbe73a832 --- /dev/null +++ b/src/main/java/guru/springframework/services/RecipeServiceImpl.java @@ -0,0 +1,27 @@ +package guru.springframework.services; + +import guru.springframework.domain.Recipe; +import guru.springframework.repositories.RecipeRepository; +import org.springframework.stereotype.Service; + +import java.util.HashSet; +import java.util.Set; + +@Service +public class RecipeServiceImpl implements RecipeService{ + + private final RecipeRepository recipeRepository; + + public RecipeServiceImpl(RecipeRepository recipeRepository) { + this.recipeRepository = recipeRepository; + } + + @Override + public Set getRecipes() { + Set recipes = new HashSet<>(); + + recipeRepository.findAll().iterator().forEachRemaining(recipes::add); + + return recipes; + } +} diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql index b628116c80..85ffa02ea5 100644 --- a/src/main/resources/data.sql +++ b/src/main/resources/data.sql @@ -6,4 +6,7 @@ INSERT INTO unit_of_measure (description) VALUES ('Teaspoon'); INSERT INTO unit_of_measure (description) VALUES ('Tablespoon'); INSERT INTO unit_of_measure (description) VALUES ('Cup'); INSERT INTO unit_of_measure (description) VALUES ('Pinch'); -INSERT INTO unit_of_measure (description) VALUES ('Ounce'); \ No newline at end of file +INSERT INTO unit_of_measure (description) VALUES ('Ounce'); +INSERT INTO unit_of_measure (description) VALUES ('Each'); +INSERT INTO unit_of_measure (description) VALUES ('Dash'); +INSERT INTO unit_of_measure (description) VALUES ('Pint'); \ No newline at end of file diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index bf0f934562..dd0f2f031e 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -6,5 +6,16 @@

My Recipes!

+ + + + + + + + + + +
IDDescription
123Tasty Goodnees
\ No newline at end of file From 726fdb91384932d9fe2643e9cbcb5a46867540b4 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Tue, 30 May 2023 16:26:47 -0500 Subject: [PATCH 13/23] refactoring logic into POJO setters --- .../bootstrap/RecipeBootstrap.java | 60 +++++++++---------- .../springframework/domain/Ingredient.java | 6 ++ .../guru/springframework/domain/Recipe.java | 7 +++ 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java b/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java index ee0fd461aa..28c7f65ead 100644 --- a/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java +++ b/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java @@ -127,17 +127,18 @@ private List getRecipes() { "\n" + "\n" + "Read more: http://www.simplyrecipes.com/recipes/perfect_guacamole/#ixzz4jvoun5ws"); - guacNotes.setRecipe(guacRecipe); + guacRecipe.setNotes(guacNotes); - guacRecipe.getIngredients().add(new Ingredient("ripe avocados", new BigDecimal(2), eachUom, guacRecipe)); - guacRecipe.getIngredients().add(new Ingredient("Kosher salt", new BigDecimal(".5"), teapoonUom, guacRecipe)); - guacRecipe.getIngredients().add(new Ingredient("fresh lime juice or lemon juice", new BigDecimal(2), tableSpoonUom, guacRecipe)); - guacRecipe.getIngredients().add(new Ingredient("minced red onion or thinly sliced green onion", new BigDecimal(2), tableSpoonUom, guacRecipe)); - guacRecipe.getIngredients().add(new Ingredient("serrano chiles, stems and seeds removed, minced", new BigDecimal(2), eachUom, guacRecipe)); - guacRecipe.getIngredients().add(new Ingredient("Cilantro", new BigDecimal(2), tableSpoonUom, guacRecipe)); - guacRecipe.getIngredients().add(new Ingredient("freshly grated black pepper", new BigDecimal(2), dashUom, guacRecipe)); - guacRecipe.getIngredients().add(new Ingredient("ripe tomato, seeds and pulp removed, chopped", new BigDecimal(".5"), eachUom, guacRecipe)); + //very redundent - could add helper method, and make this simpler + guacRecipe.addIngredient(new Ingredient("ripe avocados", new BigDecimal(2), eachUom)); + guacRecipe.addIngredient(new Ingredient("Kosher salt", new BigDecimal(".5"), teapoonUom)); + guacRecipe.addIngredient(new Ingredient("fresh lime juice or lemon juice", new BigDecimal(2), tableSpoonUom)); + guacRecipe.addIngredient(new Ingredient("minced red onion or thinly sliced green onion", new BigDecimal(2), tableSpoonUom)); + guacRecipe.addIngredient(new Ingredient("serrano chiles, stems and seeds removed, minced", new BigDecimal(2), eachUom)); + guacRecipe.addIngredient(new Ingredient("Cilantro", new BigDecimal(2), tableSpoonUom)); + guacRecipe.addIngredient(new Ingredient("freshly grated black pepper", new BigDecimal(2), dashUom)); + guacRecipe.addIngredient(new Ingredient("ripe tomato, seeds and pulp removed, chopped", new BigDecimal(".5"), eachUom)); guacRecipe.getCategories().add(americanCategory); guacRecipe.getCategories().add(mexicanCategory); @@ -174,29 +175,28 @@ private List getRecipes() { "\n" + "\n" + "Read more: http://www.simplyrecipes.com/recipes/spicy_grilled_chicken_tacos/#ixzz4jvu7Q0MJ"); - tacoNotes.setRecipe(tacosRecipe); - tacosRecipe.setNotes(tacoNotes); + tacosRecipe.setNotes(tacoNotes); - tacosRecipe.getIngredients().add(new Ingredient("Ancho Chili Powder", new BigDecimal(2), tableSpoonUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("Dried Oregano", new BigDecimal(1), teapoonUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("Dried Cumin", new BigDecimal(1), teapoonUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("Sugar", new BigDecimal(1), teapoonUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("Salt", new BigDecimal(".5"), teapoonUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("Clove of Garlic, Choppedr", new BigDecimal(1), eachUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("finely grated orange zestr", new BigDecimal(1), tableSpoonUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("fresh-squeezed orange juice", new BigDecimal(3), tableSpoonUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("Olive Oil", new BigDecimal(2), tableSpoonUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("boneless chicken thighs", new BigDecimal(4), tableSpoonUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("small corn tortillasr", new BigDecimal(8), eachUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("packed baby arugula", new BigDecimal(3), cupsUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("medium ripe avocados, slic", new BigDecimal(2), eachUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("radishes, thinly sliced", new BigDecimal(4), eachUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("cherry tomatoes, halved", new BigDecimal(".5"), pintUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("red onion, thinly sliced", new BigDecimal(".25"), eachUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("Roughly chopped cilantro", new BigDecimal(4), eachUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("cup sour cream thinned with 1/4 cup milk", new BigDecimal(4), cupsUom, tacosRecipe)); - tacosRecipe.getIngredients().add(new Ingredient("lime, cut into wedges", new BigDecimal(4), eachUom, tacosRecipe)); + tacosRecipe.addIngredient(new Ingredient("Ancho Chili Powder", new BigDecimal(2), tableSpoonUom)); + tacosRecipe.addIngredient(new Ingredient("Dried Oregano", new BigDecimal(1), teapoonUom)); + tacosRecipe.addIngredient(new Ingredient("Dried Cumin", new BigDecimal(1), teapoonUom)); + tacosRecipe.addIngredient(new Ingredient("Sugar", new BigDecimal(1), teapoonUom)); + tacosRecipe.addIngredient(new Ingredient("Salt", new BigDecimal(".5"), teapoonUom)); + tacosRecipe.addIngredient(new Ingredient("Clove of Garlic, Choppedr", new BigDecimal(1), eachUom)); + tacosRecipe.addIngredient(new Ingredient("finely grated orange zestr", new BigDecimal(1), tableSpoonUom)); + tacosRecipe.addIngredient(new Ingredient("fresh-squeezed orange juice", new BigDecimal(3), tableSpoonUom)); + tacosRecipe.addIngredient(new Ingredient("Olive Oil", new BigDecimal(2), tableSpoonUom)); + tacosRecipe.addIngredient(new Ingredient("boneless chicken thighs", new BigDecimal(4), tableSpoonUom)); + tacosRecipe.addIngredient(new Ingredient("small corn tortillasr", new BigDecimal(8), eachUom)); + tacosRecipe.addIngredient(new Ingredient("packed baby arugula", new BigDecimal(3), cupsUom)); + tacosRecipe.addIngredient(new Ingredient("medium ripe avocados, slic", new BigDecimal(2), eachUom)); + tacosRecipe.addIngredient(new Ingredient("radishes, thinly sliced", new BigDecimal(4), eachUom)); + tacosRecipe.addIngredient(new Ingredient("cherry tomatoes, halved", new BigDecimal(".5"), pintUom)); + tacosRecipe.addIngredient(new Ingredient("red onion, thinly sliced", new BigDecimal(".25"), eachUom)); + tacosRecipe.addIngredient(new Ingredient("Roughly chopped cilantro", new BigDecimal(4), eachUom)); + tacosRecipe.addIngredient(new Ingredient("cup sour cream thinned with 1/4 cup milk", new BigDecimal(4), cupsUom)); + tacosRecipe.addIngredient(new Ingredient("lime, cut into wedges", new BigDecimal(4), eachUom)); tacosRecipe.getCategories().add(americanCategory); tacosRecipe.getCategories().add(mexicanCategory); diff --git a/src/main/java/guru/springframework/domain/Ingredient.java b/src/main/java/guru/springframework/domain/Ingredient.java index b2e9b5c3a2..dad849c732 100644 --- a/src/main/java/guru/springframework/domain/Ingredient.java +++ b/src/main/java/guru/springframework/domain/Ingredient.java @@ -22,6 +22,12 @@ public class Ingredient { public Ingredient() { } + public Ingredient(String description, BigDecimal amount, UnitOfMeasure uom) { + this.description = description; + this.amount = amount; + this.uom = uom; + } + public Ingredient(String description, BigDecimal amount, UnitOfMeasure uom, Recipe recipe) { this.description = description; this.amount = amount; diff --git a/src/main/java/guru/springframework/domain/Recipe.java b/src/main/java/guru/springframework/domain/Recipe.java index 82f35a6ff1..fac63ad354 100644 --- a/src/main/java/guru/springframework/domain/Recipe.java +++ b/src/main/java/guru/springframework/domain/Recipe.java @@ -136,6 +136,13 @@ public Notes getNotes() { public void setNotes(Notes notes) { this.notes = notes; + notes.setRecipe(this); + } + + public Recipe addIngredient(Ingredient ingredient){ + ingredient.setRecipe(this); + this.ingredients.add(ingredient); + return this; } public Set getCategories() { From 8efc8a72b42b5e1528595bccf4cf5e5b60488d55 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Thu, 1 Jun 2023 16:37:07 -0500 Subject: [PATCH 14/23] adding projecto lombok --- pom.xml | 6 +- .../guru/springframework/domain/Category.java | 28 +---- .../springframework/domain/Ingredient.java | 3 +- .../guru/springframework/domain/Notes.java | 3 +- .../guru/springframework/domain/Recipe.java | 104 +----------------- .../springframework/domain/UnitOfMeasure.java | 2 +- .../services/RecipeServiceImpl.java | 5 +- 7 files changed, 21 insertions(+), 130 deletions(-) diff --git a/pom.xml b/pom.xml index 222faceb67..2c35ae31d6 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 2.1.0.RELEASE + 3.0.5 @@ -48,6 +48,10 @@ h2 runtime + + org.projectlombok + lombok + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/guru/springframework/domain/Category.java b/src/main/java/guru/springframework/domain/Category.java index 2214f87aea..9c6e5242d4 100644 --- a/src/main/java/guru/springframework/domain/Category.java +++ b/src/main/java/guru/springframework/domain/Category.java @@ -1,8 +1,11 @@ package guru.springframework.domain; -import javax.persistence.*; +import jakarta.persistence.*; +import lombok.*; + import java.util.Set; +@Data @Entity public class Category { @@ -14,27 +17,4 @@ public class Category { @ManyToMany(mappedBy = "categories") private Set recipes; - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Set getRecipes() { - return recipes; - } - - public void setRecipes(Set recipes) { - this.recipes = recipes; - } } diff --git a/src/main/java/guru/springframework/domain/Ingredient.java b/src/main/java/guru/springframework/domain/Ingredient.java index dad849c732..209e4e84b8 100644 --- a/src/main/java/guru/springframework/domain/Ingredient.java +++ b/src/main/java/guru/springframework/domain/Ingredient.java @@ -1,7 +1,6 @@ package guru.springframework.domain; - -import javax.persistence.*; +import jakarta.persistence.*; import java.math.BigDecimal; @Entity diff --git a/src/main/java/guru/springframework/domain/Notes.java b/src/main/java/guru/springframework/domain/Notes.java index 6e247b46bb..b7b90f2adc 100644 --- a/src/main/java/guru/springframework/domain/Notes.java +++ b/src/main/java/guru/springframework/domain/Notes.java @@ -1,6 +1,7 @@ package guru.springframework.domain; -import javax.persistence.*; +import jakarta.persistence.*; + /** * Created by jt on 6/13/17. diff --git a/src/main/java/guru/springframework/domain/Recipe.java b/src/main/java/guru/springframework/domain/Recipe.java index fac63ad354..55ba7b4e47 100644 --- a/src/main/java/guru/springframework/domain/Recipe.java +++ b/src/main/java/guru/springframework/domain/Recipe.java @@ -1,12 +1,15 @@ package guru.springframework.domain; -import javax.persistence.*; +import jakarta.persistence.*; +import lombok.Data; + import java.util.HashSet; import java.util.Set; /** * Created by jt on 6/13/17. */ +@Data @Entity public class Recipe { @@ -42,98 +45,6 @@ public class Recipe { inverseJoinColumns = @JoinColumn(name = "category_id")) private Set categories = new HashSet<>(); - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public Integer getPrepTime() { - return prepTime; - } - - public void setPrepTime(Integer prepTime) { - this.prepTime = prepTime; - } - - public Integer getCookTime() { - return cookTime; - } - - public void setCookTime(Integer cookTime) { - this.cookTime = cookTime; - } - - public Integer getServings() { - return servings; - } - - public void setServings(Integer servings) { - this.servings = servings; - } - - public String getSource() { - return source; - } - - public void setSource(String source) { - this.source = source; - } - - public String getUrl() { - return url; - } - - public void setUrl(String url) { - this.url = url; - } - - public String getDirections() { - return directions; - } - - public void setDirections(String directions) { - this.directions = directions; - } - - public Set getIngredients() { - return ingredients; - } - - public void setIngredients(Set ingredients) { - this.ingredients = ingredients; - } - - public Byte[] getImage() { - return image; - } - - public void setImage(Byte[] image) { - this.image = image; - } - - public Difficulty getDifficulty() { - return difficulty; - } - - public void setDifficulty(Difficulty difficulty) { - this.difficulty = difficulty; - } - - public Notes getNotes() { - return notes; - } - public void setNotes(Notes notes) { this.notes = notes; notes.setRecipe(this); @@ -145,11 +56,4 @@ public Recipe addIngredient(Ingredient ingredient){ return this; } - public Set getCategories() { - return categories; - } - - public void setCategories(Set categories) { - this.categories = categories; - } } diff --git a/src/main/java/guru/springframework/domain/UnitOfMeasure.java b/src/main/java/guru/springframework/domain/UnitOfMeasure.java index 3c55919977..4a59dd2454 100644 --- a/src/main/java/guru/springframework/domain/UnitOfMeasure.java +++ b/src/main/java/guru/springframework/domain/UnitOfMeasure.java @@ -1,6 +1,6 @@ package guru.springframework.domain; -import javax.persistence.*; +import jakarta.persistence.*; @Entity public class UnitOfMeasure { diff --git a/src/main/java/guru/springframework/services/RecipeServiceImpl.java b/src/main/java/guru/springframework/services/RecipeServiceImpl.java index 2fbe73a832..9e5ef9fe1c 100644 --- a/src/main/java/guru/springframework/services/RecipeServiceImpl.java +++ b/src/main/java/guru/springframework/services/RecipeServiceImpl.java @@ -2,11 +2,13 @@ import guru.springframework.domain.Recipe; import guru.springframework.repositories.RecipeRepository; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import java.util.HashSet; import java.util.Set; +@Slf4j @Service public class RecipeServiceImpl implements RecipeService{ @@ -18,8 +20,9 @@ public RecipeServiceImpl(RecipeRepository recipeRepository) { @Override public Set getRecipes() { - Set recipes = new HashSet<>(); + log.debug("I'm in the service"); + Set recipes = new HashSet<>(); recipeRepository.findAll().iterator().forEachRemaining(recipes::add); return recipes; From 07997703bc0cf3f055c45e25522df97e7ae3e0d3 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Thu, 1 Jun 2023 16:53:14 -0500 Subject: [PATCH 15/23] refactor --- pom.xml | 2 +- .../bootstrap/RecipeBootstrap.java | 3 ++ .../controllers/IndexController.java | 3 ++ .../guru/springframework/domain/Category.java | 2 +- .../springframework/domain/Ingredient.java | 45 ++----------------- .../guru/springframework/domain/Notes.java | 27 ++--------- .../guru/springframework/domain/Recipe.java | 2 +- .../springframework/domain/UnitOfMeasure.java | 21 ++------- 8 files changed, 19 insertions(+), 86 deletions(-) diff --git a/pom.xml b/pom.xml index 2c35ae31d6..c419c689b7 100644 --- a/pom.xml +++ b/pom.xml @@ -14,7 +14,7 @@ org.springframework.boot spring-boot-starter-parent - 3.0.5 + 2.1.0.RELEASE diff --git a/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java b/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java index 28c7f65ead..ac0b6bb2c2 100644 --- a/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java +++ b/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java @@ -4,6 +4,7 @@ import guru.springframework.repositories.CategoryRepository; import guru.springframework.repositories.RecipeRepository; import guru.springframework.repositories.UnitOfMeasureRepository; +import lombok.extern.slf4j.Slf4j; import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; @@ -16,6 +17,7 @@ /** * Created by jt on 6/13/17. */ +@Slf4j @Component public class RecipeBootstrap implements ApplicationListener { @@ -32,6 +34,7 @@ public RecipeBootstrap(CategoryRepository categoryRepository, RecipeRepository r @Override public void onApplicationEvent(ContextRefreshedEvent event) { recipeRepository.saveAll(getRecipes()); + log.debug("Loading boostrap data"); } private List getRecipes() { diff --git a/src/main/java/guru/springframework/controllers/IndexController.java b/src/main/java/guru/springframework/controllers/IndexController.java index 92adfe9b85..dee7db03c6 100644 --- a/src/main/java/guru/springframework/controllers/IndexController.java +++ b/src/main/java/guru/springframework/controllers/IndexController.java @@ -1,10 +1,12 @@ package guru.springframework.controllers; import guru.springframework.services.RecipeService; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; +@Slf4j @Controller public class IndexController { @@ -16,6 +18,7 @@ public IndexController(RecipeService recipeService) { @RequestMapping({"", "/", "/index"}) public String getIndexPage(Model model) { + log.debug("getting index page"); model.addAttribute("recipes", recipeService.getRecipes()); diff --git a/src/main/java/guru/springframework/domain/Category.java b/src/main/java/guru/springframework/domain/Category.java index 9c6e5242d4..f867123a14 100644 --- a/src/main/java/guru/springframework/domain/Category.java +++ b/src/main/java/guru/springframework/domain/Category.java @@ -1,6 +1,6 @@ package guru.springframework.domain; -import jakarta.persistence.*; +import javax.persistence.*; import lombok.*; import java.util.Set; diff --git a/src/main/java/guru/springframework/domain/Ingredient.java b/src/main/java/guru/springframework/domain/Ingredient.java index 209e4e84b8..921799b26b 100644 --- a/src/main/java/guru/springframework/domain/Ingredient.java +++ b/src/main/java/guru/springframework/domain/Ingredient.java @@ -1,8 +1,11 @@ package guru.springframework.domain; -import jakarta.persistence.*; +import javax.persistence.*; +import lombok.Data; + import java.math.BigDecimal; +@Data @Entity public class Ingredient { @@ -33,44 +36,4 @@ public Ingredient(String description, BigDecimal amount, UnitOfMeasure uom, Reci this.uom = uom; this.recipe = recipe; } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - - public BigDecimal getAmount() { - return amount; - } - - public void setAmount(BigDecimal amount) { - this.amount = amount; - } - - public UnitOfMeasure getUom() { - return uom; - } - - public void setUom(UnitOfMeasure uom) { - this.uom = uom; - } - - public Recipe getRecipe() { - return recipe; - } - - public void setRecipe(Recipe recipe) { - this.recipe = recipe; - } } diff --git a/src/main/java/guru/springframework/domain/Notes.java b/src/main/java/guru/springframework/domain/Notes.java index b7b90f2adc..d26aa324d0 100644 --- a/src/main/java/guru/springframework/domain/Notes.java +++ b/src/main/java/guru/springframework/domain/Notes.java @@ -1,11 +1,13 @@ package guru.springframework.domain; -import jakarta.persistence.*; +import javax.persistence.*; +import lombok.*; /** * Created by jt on 6/13/17. */ +@Data @Entity public class Notes { @@ -19,27 +21,4 @@ public class Notes { @Lob private String recipeNotes; - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public Recipe getRecipe() { - return recipe; - } - - public void setRecipe(Recipe recipe) { - this.recipe = recipe; - } - - public String getRecipeNotes() { - return recipeNotes; - } - - public void setRecipeNotes(String recipeNotes) { - this.recipeNotes = recipeNotes; - } } diff --git a/src/main/java/guru/springframework/domain/Recipe.java b/src/main/java/guru/springframework/domain/Recipe.java index 55ba7b4e47..f4a42bf6fc 100644 --- a/src/main/java/guru/springframework/domain/Recipe.java +++ b/src/main/java/guru/springframework/domain/Recipe.java @@ -1,6 +1,6 @@ package guru.springframework.domain; -import jakarta.persistence.*; +import javax.persistence.*; import lombok.Data; import java.util.HashSet; diff --git a/src/main/java/guru/springframework/domain/UnitOfMeasure.java b/src/main/java/guru/springframework/domain/UnitOfMeasure.java index 4a59dd2454..e8ca1e1209 100644 --- a/src/main/java/guru/springframework/domain/UnitOfMeasure.java +++ b/src/main/java/guru/springframework/domain/UnitOfMeasure.java @@ -1,7 +1,9 @@ package guru.springframework.domain; -import jakarta.persistence.*; +import javax.persistence.*; +import lombok.*; +@Data @Entity public class UnitOfMeasure { @@ -9,22 +11,5 @@ public class UnitOfMeasure { @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; private String description; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - } From 0213dd92b1849a18393f3e3cdb7df94364e31568 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Thu, 1 Jun 2023 17:00:51 -0500 Subject: [PATCH 16/23] solve error --- .../java/guru/springframework/bootstrap/RecipeBootstrap.java | 2 ++ src/main/java/guru/springframework/domain/Category.java | 1 + src/main/java/guru/springframework/domain/Ingredient.java | 2 ++ src/main/java/guru/springframework/domain/Notes.java | 1 + 4 files changed, 6 insertions(+) diff --git a/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java b/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java index ac0b6bb2c2..b6951f9b6f 100644 --- a/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java +++ b/src/main/java/guru/springframework/bootstrap/RecipeBootstrap.java @@ -8,6 +8,7 @@ import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Transactional; import java.math.BigDecimal; import java.util.ArrayList; @@ -32,6 +33,7 @@ public RecipeBootstrap(CategoryRepository categoryRepository, RecipeRepository r } @Override + @Transactional public void onApplicationEvent(ContextRefreshedEvent event) { recipeRepository.saveAll(getRecipes()); log.debug("Loading boostrap data"); diff --git a/src/main/java/guru/springframework/domain/Category.java b/src/main/java/guru/springframework/domain/Category.java index f867123a14..4291db4c64 100644 --- a/src/main/java/guru/springframework/domain/Category.java +++ b/src/main/java/guru/springframework/domain/Category.java @@ -6,6 +6,7 @@ import java.util.Set; @Data +@EqualsAndHashCode(exclude = {"recipes"}) @Entity public class Category { diff --git a/src/main/java/guru/springframework/domain/Ingredient.java b/src/main/java/guru/springframework/domain/Ingredient.java index 921799b26b..fb56be5caa 100644 --- a/src/main/java/guru/springframework/domain/Ingredient.java +++ b/src/main/java/guru/springframework/domain/Ingredient.java @@ -2,10 +2,12 @@ import javax.persistence.*; import lombok.Data; +import lombok.EqualsAndHashCode; import java.math.BigDecimal; @Data +@EqualsAndHashCode(exclude = {"recipe"}) @Entity public class Ingredient { diff --git a/src/main/java/guru/springframework/domain/Notes.java b/src/main/java/guru/springframework/domain/Notes.java index d26aa324d0..a0573a2074 100644 --- a/src/main/java/guru/springframework/domain/Notes.java +++ b/src/main/java/guru/springframework/domain/Notes.java @@ -8,6 +8,7 @@ * Created by jt on 6/13/17. */ @Data +@EqualsAndHashCode(exclude = {"recipe"}) @Entity public class Notes { From 99d7af8821d8cb3d06e682e6d678d77f3fa11211 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Fri, 2 Jun 2023 11:51:12 -0500 Subject: [PATCH 17/23] adding boostrap CSS --- src/main/resources/templates/index.html | 57 ++++++++++++++++++++----- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/src/main/resources/templates/index.html b/src/main/resources/templates/index.html index dd0f2f031e..b34400d395 100644 --- a/src/main/resources/templates/index.html +++ b/src/main/resources/templates/index.html @@ -3,19 +3,54 @@ Recipe Home + + + + + + -

My Recipes!

- - - - - - - - - -
IDDescription
123Tasty Goodnees
+
+
+
+
+ +
+

My Recipes!

+
+
+
+ + + + + + + + + + + + + + + + + + + +
IDDescription
123Tasty Goodnees 1
12333Tasty Goodnees 2
334Tasty Goodnees 3
+
+
+
+
+
+
+ \ No newline at end of file From 133acb2ef3d9379251ba1d09581546504b086b42 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Fri, 2 Jun 2023 12:46:14 -0500 Subject: [PATCH 18/23] Creating a JUnit Test --- .../springframework/domain/CategoryTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 src/test/java/guru/springframework/domain/CategoryTest.java diff --git a/src/test/java/guru/springframework/domain/CategoryTest.java b/src/test/java/guru/springframework/domain/CategoryTest.java new file mode 100644 index 0000000000..2c38cd3218 --- /dev/null +++ b/src/test/java/guru/springframework/domain/CategoryTest.java @@ -0,0 +1,33 @@ +package guru.springframework.domain; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class CategoryTest { + + Category category; + + @Before + public void setUp(){ + category = new Category(); + } + + @Test + public void getId() { + Long idValue = 4L; + + category.setId(idValue); + + assertEquals(idValue, category.getId()); + } + + @Test + public void getDescription() { + } + + @Test + public void getRecipes() { + } +} \ No newline at end of file From 1d0c102f8029bdca226408e7ea023215a5bddb31 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Mon, 5 Jun 2023 10:36:21 -0500 Subject: [PATCH 19/23] create a test using Mockito Mocks --- .../services/RecipeServiceImplTest.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/test/java/guru/springframework/services/RecipeServiceImplTest.java diff --git a/src/test/java/guru/springframework/services/RecipeServiceImplTest.java b/src/test/java/guru/springframework/services/RecipeServiceImplTest.java new file mode 100644 index 0000000000..6fa5611530 --- /dev/null +++ b/src/test/java/guru/springframework/services/RecipeServiceImplTest.java @@ -0,0 +1,45 @@ +package guru.springframework.services; + +import guru.springframework.domain.Recipe; +import guru.springframework.repositories.RecipeRepository; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.*; +import static org.mockito.Mockito.*; + +public class RecipeServiceImplTest { + + RecipeServiceImpl recipeService; + + @Mock + RecipeRepository recipeRepository; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + recipeService = new RecipeServiceImpl(recipeRepository); + } + + @Test + public void getRecipes() { + + Recipe recipe = new Recipe(); + HashSet recipesData = new HashSet(); + recipesData.add(recipe); + + when(recipeRepository.findAll()).thenReturn(recipesData); + + Set recipes = recipeService.getRecipes(); + + assertEquals(recipes.size(), 1); + verify(recipeRepository, times(1)).findAll(); + + } +} \ No newline at end of file From 0a2d7b01058b99d1bdb4972a11863a3e2f452936 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Mon, 5 Jun 2023 10:55:38 -0500 Subject: [PATCH 20/23] Create indexController test --- .../controllers/IndexControllerTest.java | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 src/test/java/guru/springframework/controllers/IndexControllerTest.java diff --git a/src/test/java/guru/springframework/controllers/IndexControllerTest.java b/src/test/java/guru/springframework/controllers/IndexControllerTest.java new file mode 100644 index 0000000000..42a88320fd --- /dev/null +++ b/src/test/java/guru/springframework/controllers/IndexControllerTest.java @@ -0,0 +1,41 @@ +package guru.springframework.controllers; + +import guru.springframework.services.RecipeService; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; +import org.springframework.ui.Model; + +import static org.junit.Assert.*; +import static org.mockito.ArgumentMatchers.anySet; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +public class IndexControllerTest { + + @Mock + RecipeService recipeService; + + @Mock + Model model; + + IndexController controller; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + controller = new IndexController(recipeService); + } + + @Test + public void getIndexPage() { + + String viewName = controller.getIndexPage(model); + assertEquals("index", viewName); + verify(recipeService, times(1)).getRecipes(); + verify(model,times(1)).addAttribute(eq("recipes"), anySet()); + } +} \ No newline at end of file From 0995af0b020e3fe1753e015a02102fed0633904c Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Mon, 5 Jun 2023 11:09:31 -0500 Subject: [PATCH 21/23] Mockito Argument Capture --- .../controllers/IndexControllerTest.java | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/test/java/guru/springframework/controllers/IndexControllerTest.java b/src/test/java/guru/springframework/controllers/IndexControllerTest.java index 42a88320fd..33c70d54ae 100644 --- a/src/test/java/guru/springframework/controllers/IndexControllerTest.java +++ b/src/test/java/guru/springframework/controllers/IndexControllerTest.java @@ -1,17 +1,21 @@ package guru.springframework.controllers; +import guru.springframework.domain.Recipe; import guru.springframework.services.RecipeService; import org.junit.Before; import org.junit.Test; +import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; import org.springframework.ui.Model; +import java.util.HashSet; +import java.util.Set; + import static org.junit.Assert.*; import static org.mockito.ArgumentMatchers.anySet; import static org.mockito.ArgumentMatchers.eq; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.*; public class IndexControllerTest { @@ -33,9 +37,25 @@ public void setUp() throws Exception { @Test public void getIndexPage() { + //given + Set recipes = new HashSet<>(); + recipes.add(new Recipe()); + Recipe recipe = new Recipe(); + recipe.setId(1L); + recipes.add(recipe); + + when(recipeService.getRecipes()).thenReturn(recipes); + + ArgumentCaptor> argumentCaptor = ArgumentCaptor.forClass(Set.class); + + //when String viewName = controller.getIndexPage(model); + + //then assertEquals("index", viewName); verify(recipeService, times(1)).getRecipes(); - verify(model,times(1)).addAttribute(eq("recipes"), anySet()); + verify(model,times(1)).addAttribute(eq("recipes"), argumentCaptor.capture()); + Set setInController = argumentCaptor.getValue(); + assertEquals(2, setInController.size()); } } \ No newline at end of file From f78c69f8c806f0b253e3095fa4738e7bb05efb84 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Mon, 5 Jun 2023 11:19:49 -0500 Subject: [PATCH 22/23] Introduction to Spring MockMVC --- .../controllers/IndexControllerTest.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/test/java/guru/springframework/controllers/IndexControllerTest.java b/src/test/java/guru/springframework/controllers/IndexControllerTest.java index 33c70d54ae..e05ada6b93 100644 --- a/src/test/java/guru/springframework/controllers/IndexControllerTest.java +++ b/src/test/java/guru/springframework/controllers/IndexControllerTest.java @@ -7,15 +7,20 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.ui.Model; import java.util.HashSet; import java.util.Set; import static org.junit.Assert.*; -import static org.mockito.ArgumentMatchers.anySet; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.*; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; + public class IndexControllerTest { @@ -28,12 +33,20 @@ public class IndexControllerTest { IndexController controller; @Before - public void setUp() throws Exception { + public void setUp() { MockitoAnnotations.initMocks(this); controller = new IndexController(recipeService); } + @Test + public void testMockMVC() throws Exception { + MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); + mockMvc.perform(get("/")) + .andExpect(status().isOk()) + .andExpect(view().name("index")); + } + @Test public void getIndexPage() { From 6452a730a5f673c3b679d9cdbb509451ed9320d0 Mon Sep 17 00:00:00 2001 From: Nicolas Raigosa Date: Mon, 5 Jun 2023 11:30:57 -0500 Subject: [PATCH 23/23] Spring Integration Test --- .../UnitOfMeasureRepositoryIT.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/test/java/guru/springframework/repositories/UnitOfMeasureRepositoryIT.java diff --git a/src/test/java/guru/springframework/repositories/UnitOfMeasureRepositoryIT.java b/src/test/java/guru/springframework/repositories/UnitOfMeasureRepositoryIT.java new file mode 100644 index 0000000000..dda1870b44 --- /dev/null +++ b/src/test/java/guru/springframework/repositories/UnitOfMeasureRepositoryIT.java @@ -0,0 +1,39 @@ +package guru.springframework.repositories; + +import guru.springframework.domain.UnitOfMeasure; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest; +import org.springframework.test.context.junit4.SpringRunner; + +import java.util.Optional; + +import static org.junit.Assert.*; + +@RunWith(SpringRunner.class) +@DataJpaTest +public class UnitOfMeasureRepositoryIT { + + @Autowired + UnitOfMeasureRepository unitOfMeasureRepository; + + @Before + public void setUp() throws Exception { + } + + @Test + public void findByDescription() throws Exception { + + Optional uomOptional = unitOfMeasureRepository.findByDescription("Teaspoon"); + assertEquals("Teaspoon", uomOptional.get().getDescription()); + } + + @Test + public void findByDescriptionCup() throws Exception { + + Optional uomOptional = unitOfMeasureRepository.findByDescription("Cup"); + assertEquals("Cup", uomOptional.get().getDescription()); + } +} \ No newline at end of file