This repository has been archived by the owner on Oct 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spring-action: separated things by chapter
- Loading branch information
1 parent
b6f2ed3
commit ee410bd
Showing
49 changed files
with
1,064 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
74 changes: 74 additions & 0 deletions
74
...1.2/src/main/java/es/msanchez/spring/springinaction/controllers/DesignTacoController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package es.msanchez.spring.springinaction.controllers; | ||
|
||
import es.msanchez.spring.springinaction.entities.Ingredient; | ||
import es.msanchez.spring.springinaction.entities.Taco; | ||
import es.msanchez.spring.springinaction.enums.Type; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.validation.Errors; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
import org.springframework.web.bind.annotation.ModelAttribute; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
|
||
import javax.validation.Valid; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
@Slf4j | ||
@Controller | ||
@RequestMapping("/design") | ||
public class DesignTacoController { | ||
|
||
/** | ||
* This is going to be loaded every time we load for "design" view. | ||
* | ||
* @param model - | ||
*/ | ||
@ModelAttribute | ||
public void addIngredientsToModel(final Model model) { | ||
final List<Ingredient> ingredients = Arrays.asList( | ||
new Ingredient("FLTO", "Flour tortilla", Type.WRAP), | ||
new Ingredient("COTO", "Corn tortilla", Type.WRAP), | ||
new Ingredient("GRBF", "Ground Beef", Type.PROTEIN), | ||
new Ingredient("CARN", "Carnitas", Type.PROTEIN), | ||
new Ingredient("TMTO", "Diced tomatoes", Type.VEGGIES), | ||
new Ingredient("LETC", "Lettuce", Type.VEGGIES), | ||
new Ingredient("CHED", "Ceddar", Type.CHEESE), | ||
new Ingredient("JACK", "Monterrey Jack", Type.CHEESE), | ||
new Ingredient("SLSA", "Salsa", Type.SAUCE), | ||
new Ingredient("SRCR", "Sour cream", Type.SAUCE)); | ||
|
||
for (final Type type : Type.values()) { | ||
model.addAttribute(type.toString().toLowerCase(), filterByType(ingredients, type)); | ||
} | ||
} | ||
|
||
@GetMapping | ||
public String showDesignForm(final Model model) { | ||
model.addAttribute("design", new Taco()); | ||
return "design"; | ||
} | ||
|
||
@PostMapping | ||
public String processDesign(@Valid @ModelAttribute("design") final Taco design, | ||
final Errors errors, | ||
final Model model) { | ||
if (errors.hasErrors()) { | ||
log.error("Errors found on processDesign()"); | ||
return "design"; | ||
} | ||
|
||
log.info("processing design '{}'", design); | ||
return "redirect:/orders/current"; | ||
} | ||
|
||
private List<Ingredient> filterByType(final List<Ingredient> ingredients, | ||
final Type type) { | ||
return ingredients.stream() | ||
.filter(ing -> type.equals(ing.getType())) | ||
.collect(Collectors.toList()); | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
File renamed without changes.
24 changes: 24 additions & 0 deletions
24
...-in-action/chapter-1.2/src/main/java/es/msanchez/spring/springinaction/entities/Taco.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package es.msanchez.spring.springinaction.entities; | ||
|
||
import lombok.Data; | ||
|
||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Size; | ||
import java.sql.Date; | ||
import java.util.List; | ||
|
||
@Data | ||
public class Taco { | ||
|
||
private Long id; | ||
|
||
private Date createdAt; | ||
|
||
@NotNull | ||
@Size(min = 5, message = "Name must be at least 5 characters long") | ||
private String name; | ||
|
||
@Size(min = 1, message = "You must select at least 1 ingredient") | ||
private List<String> ingredients; | ||
|
||
} |
File renamed without changes.
13 changes: 13 additions & 0 deletions
13
....2/src/main/java/es/msanchez/spring/springinaction/repositories/IngredientRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package es.msanchez.spring.springinaction.repositories; | ||
|
||
import es.msanchez.spring.springinaction.entities.Ingredient; | ||
|
||
public interface IngredientRepository { | ||
|
||
Iterable<Ingredient> findAll(); | ||
|
||
Ingredient findOne(final String id); | ||
|
||
Ingredient save(final Ingredient ingredient); | ||
|
||
} |
36 changes: 36 additions & 0 deletions
36
...rc/main/java/es/msanchez/spring/springinaction/repositories/JdbcIngredientRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package es.msanchez.spring.springinaction.repositories; | ||
|
||
import es.msanchez.spring.springinaction.entities.Ingredient; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public class JdbcIngredientRepository implements IngredientRepository { | ||
|
||
private JdbcTemplate jdbcTemplate; | ||
|
||
/** | ||
* @param jdbcTemplate - | ||
*/ | ||
@Autowired | ||
public JdbcIngredientRepository(final JdbcTemplate jdbcTemplate) { | ||
this.jdbcTemplate = jdbcTemplate; | ||
} | ||
|
||
@Override | ||
public Iterable<Ingredient> findAll() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Ingredient findOne(final String id) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Ingredient save(final Ingredient ingredient) { | ||
return null; | ||
} | ||
|
||
} |
File renamed without changes.
22 changes: 22 additions & 0 deletions
22
spring/spring-in-action/chapter-1.2/src/main/resources/schema.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
CREATE TABLE IF NOT EXISTS Ingredient( | ||
id VARCHAR(4) NOT NULL, | ||
name VARCHAR(25) NOT NULL, | ||
type VARCHAR(10) NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS Taco( | ||
id IDENTITY, | ||
name VARCHAR(50) NOT NULL, | ||
createdAt TIMESTAMP NOT NULL | ||
); | ||
|
||
CREATE TABLE IF NOT EXISTS Taco_Ingredients( | ||
taco BIGINT NOT NULL, | ||
ingredient VARCHAR(4) NOT NULL | ||
); | ||
|
||
ALTER TABLE Taco_Ingredients | ||
ADD FOREIGN KEY (taco) REFERENCES Taco(id); | ||
|
||
ALTER TABLE Taco_Ingredients | ||
ADD FOREIGN KEY (ingredient) REFERENCES Ingredient(id); |
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
HELP.md | ||
/target/ | ||
!.mvn/wrapper/maven-wrapper.jar | ||
|
||
### STS ### | ||
.apt_generated | ||
.classpath | ||
.factorypath | ||
.project | ||
.settings | ||
.springBeans | ||
.sts4-cache | ||
|
||
### IntelliJ IDEA ### | ||
.idea | ||
*.iws | ||
*.iml | ||
*.ipr | ||
|
||
### NetBeans ### | ||
/nbproject/private/ | ||
/nbbuild/ | ||
/dist/ | ||
/nbdist/ | ||
/.nb-gradle/ | ||
/build/ | ||
|
||
### VS Code ### | ||
.vscode/ | ||
|
||
## Custom | ||
.mvn/ | ||
/web/ | ||
/lib/ |
Oops, something went wrong.