Skip to content
This repository has been archived by the owner on Oct 21, 2024. It is now read-only.

Commit

Permalink
spring-action: separated things by chapter
Browse files Browse the repository at this point in the history
  • Loading branch information
MarioCodes committed Oct 2, 2019
1 parent b6f2ed3 commit ee410bd
Show file tree
Hide file tree
Showing 49 changed files with 1,064 additions and 0 deletions.
File renamed without changes.
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());
}
}
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;

}
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);

}
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;
}

}
22 changes: 22 additions & 0 deletions spring/spring-in-action/chapter-1.2/src/main/resources/schema.sql
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);
34 changes: 34 additions & 0 deletions spring/spring-in-action/chapter-1.3/.gitignore
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/
Loading

0 comments on commit ee410bd

Please sign in to comment.