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

Commit b6f2ed3

Browse files
committed
spring-action: implementing embedded DBB
1 parent 9d7fe0c commit b6f2ed3

File tree

12 files changed

+255
-67
lines changed

12 files changed

+255
-67
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package es.msanchez.spring.springinaction.config;
2+
3+
import org.springframework.context.annotation.ComponentScan;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
@ComponentScan(basePackages = {
8+
"es.msanchez.spring.springinaction.**"
9+
})
10+
public class SpringConfig {
11+
}

spring/spring-in-action/src/main/java/es/msanchez/spring/springinaction/controllers/DesignTacoController.java

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,54 @@
11
package es.msanchez.spring.springinaction.controllers;
22

3+
import es.msanchez.spring.springinaction.dao.IngredientRepository;
4+
import es.msanchez.spring.springinaction.dao.TacoRepository;
35
import es.msanchez.spring.springinaction.entities.Ingredient;
6+
import es.msanchez.spring.springinaction.entities.Order;
47
import es.msanchez.spring.springinaction.entities.Taco;
58
import es.msanchez.spring.springinaction.enums.Type;
69
import lombok.extern.slf4j.Slf4j;
10+
import org.springframework.beans.factory.annotation.Autowired;
711
import org.springframework.stereotype.Controller;
812
import org.springframework.ui.Model;
13+
import org.springframework.util.CollectionUtils;
914
import org.springframework.validation.Errors;
10-
import org.springframework.web.bind.annotation.GetMapping;
11-
import org.springframework.web.bind.annotation.ModelAttribute;
12-
import org.springframework.web.bind.annotation.PostMapping;
13-
import org.springframework.web.bind.annotation.RequestMapping;
15+
import org.springframework.web.bind.annotation.*;
1416

1517
import javax.validation.Valid;
18+
import java.util.ArrayList;
1619
import java.util.Arrays;
1720
import java.util.List;
1821
import java.util.stream.Collectors;
1922

2023
@Slf4j
2124
@Controller
2225
@RequestMapping("/design")
26+
@SessionAttributes("order")
2327
public class DesignTacoController {
2428

29+
private final IngredientRepository ingredientRepository;
30+
private final TacoRepository tacoRepository;
31+
32+
/**
33+
* @param ingredientRepository injected
34+
* @param tacoRepository injected
35+
*/
36+
@Autowired
37+
public DesignTacoController(IngredientRepository ingredientRepository, TacoRepository tacoRepository) {
38+
this.ingredientRepository = ingredientRepository;
39+
this.tacoRepository = tacoRepository;
40+
}
41+
42+
@ModelAttribute(name = "order")
43+
public Order order() {
44+
return new Order();
45+
}
46+
47+
@ModelAttribute(name = "taco")
48+
public Taco taco() {
49+
return new Taco();
50+
}
51+
2552
/**
2653
* This is going to be loaded every time we load for "design" view.
2754
*
@@ -30,16 +57,16 @@ public class DesignTacoController {
3057
@ModelAttribute
3158
public void addIngredientsToModel(final Model model) {
3259
final List<Ingredient> ingredients = Arrays.asList(
33-
new Ingredient("FLTO", "Flour tortilla", Type.WRAP),
34-
new Ingredient("COTO", "Corn tortilla", Type.WRAP),
35-
new Ingredient("GRBF", "Ground Beef", Type.PROTEIN),
36-
new Ingredient("CARN", "Carnitas", Type.PROTEIN),
37-
new Ingredient("TMTO", "Diced tomatoes", Type.VEGGIES),
38-
new Ingredient("LETC", "Lettuce", Type.VEGGIES),
39-
new Ingredient("CHED", "Ceddar", Type.CHEESE),
40-
new Ingredient("JACK", "Monterrey Jack", Type.CHEESE),
41-
new Ingredient("SLSA", "Salsa", Type.SAUCE),
42-
new Ingredient("SRCR", "Sour cream", Type.SAUCE));
60+
new Ingredient("FLTO", "Flour tortilla", Type.WRAP),
61+
new Ingredient("COTO", "Corn tortilla", Type.WRAP),
62+
new Ingredient("GRBF", "Ground Beef", Type.PROTEIN),
63+
new Ingredient("CARN", "Carnitas", Type.PROTEIN),
64+
new Ingredient("TMTO", "Diced tomatoes", Type.VEGGIES),
65+
new Ingredient("LETC", "Lettuce", Type.VEGGIES),
66+
new Ingredient("CHED", "Ceddar", Type.CHEESE),
67+
new Ingredient("JACK", "Monterrey Jack", Type.CHEESE),
68+
new Ingredient("SLSA", "Salsa", Type.SAUCE),
69+
new Ingredient("SRCR", "Sour cream", Type.SAUCE));
4370

4471
for (final Type type : Type.values()) {
4572
model.addAttribute(type.toString().toLowerCase(), filterByType(ingredients, type));
@@ -48,19 +75,29 @@ public void addIngredientsToModel(final Model model) {
4875

4976
@GetMapping
5077
public String showDesignForm(final Model model) {
51-
model.addAttribute("design", new Taco());
78+
final List<Ingredient> ingredients = new ArrayList<>();
79+
this.ingredientRepository.findAll().forEach(ingredients::add);
80+
81+
final List<Type> types = CollectionUtils.arrayToList(Type.values());
82+
for (final Type type : types) {
83+
model.addAttribute(type.toString().toLowerCase(),
84+
filterByType(ingredients, type));
85+
}
86+
5287
return "design";
5388
}
5489

5590
@PostMapping
5691
public String processDesign(@Valid @ModelAttribute("design") final Taco design,
57-
final Errors errors,
58-
final Model model) {
92+
final Errors errors,
93+
@ModelAttribute final Order order) {
5994
if (errors.hasErrors()) {
6095
log.error("Errors found on processDesign()");
6196
return "design";
6297
}
6398

99+
final Taco saved = this.tacoRepository.save(design);
100+
64101
log.info("processing design '{}'", design);
65102
return "redirect:/orders/current";
66103
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package es.msanchez.spring.springinaction.dao;
2+
3+
import es.msanchez.spring.springinaction.entities.Ingredient;
4+
import org.springframework.stereotype.Repository;
5+
6+
@Repository
7+
public interface IngredientRepository {
8+
9+
Iterable<Ingredient> findAll();
10+
11+
Ingredient findOne(final String id);
12+
13+
Ingredient save(final Ingredient ingredient);
14+
15+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package es.msanchez.spring.springinaction.dao;
2+
3+
import es.msanchez.spring.springinaction.entities.Ingredient;
4+
import es.msanchez.spring.springinaction.enums.Type;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.jdbc.core.JdbcTemplate;
7+
import org.springframework.stereotype.Repository;
8+
9+
import java.sql.ResultSet;
10+
import java.sql.SQLException;
11+
12+
@Repository
13+
public class JdbcIngredientRepository implements IngredientRepository {
14+
15+
private JdbcTemplate jdbcTemplate;
16+
17+
/**
18+
* @param jdbcTemplate -
19+
*/
20+
@Autowired
21+
public JdbcIngredientRepository(final JdbcTemplate jdbcTemplate) {
22+
this.jdbcTemplate = jdbcTemplate;
23+
}
24+
25+
@Override
26+
public Iterable<Ingredient> findAll() {
27+
return this.jdbcTemplate.query("select id, name, type from Ingredient", this::mapRowToIngredient);
28+
}
29+
30+
private Ingredient mapRowToIngredient(final ResultSet rs,
31+
final int rowNum) throws SQLException {
32+
return new Ingredient(rs.getString("id"),
33+
rs.getString("name"),
34+
Type.valueOf(rs.getString("type")));
35+
}
36+
37+
@Override
38+
public Ingredient findOne(final String id) {
39+
return this.jdbcTemplate.queryForObject("select id, name, type from Ingredient where id = ?",
40+
this::mapRowToIngredient, id);
41+
}
42+
43+
@Override
44+
public Ingredient save(final Ingredient ingredient) {
45+
this.jdbcTemplate.update("insert into Ingredient (id, name, type) values (?, ?, ?)",
46+
ingredient.getId(),
47+
ingredient.getName(),
48+
ingredient.getType().toString());
49+
return ingredient;
50+
}
51+
52+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package es.msanchez.spring.springinaction.dao;
2+
3+
import es.msanchez.spring.springinaction.entities.Ingredient;
4+
import es.msanchez.spring.springinaction.entities.Taco;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.jdbc.core.JdbcTemplate;
7+
import org.springframework.jdbc.core.PreparedStatementCreator;
8+
import org.springframework.jdbc.core.PreparedStatementCreatorFactory;
9+
import org.springframework.jdbc.support.GeneratedKeyHolder;
10+
import org.springframework.jdbc.support.KeyHolder;
11+
import org.springframework.stereotype.Repository;
12+
13+
import java.sql.Date;
14+
import java.sql.Timestamp;
15+
import java.sql.Types;
16+
import java.time.LocalDate;
17+
import java.util.Arrays;
18+
19+
@Repository
20+
public class JdbcTacoRepository implements TacoRepository {
21+
22+
private final JdbcTemplate jdbc;
23+
24+
@Autowired
25+
public JdbcTacoRepository(final JdbcTemplate jdbc) {
26+
this.jdbc = jdbc;
27+
}
28+
29+
@Override
30+
public Taco save(final Taco taco) {
31+
final long tacoId = this.saveTacoInfo(taco);
32+
taco.setId(tacoId);
33+
taco.getIngredients().forEach(ing -> this.saveIngredientToTaco(ing, tacoId));
34+
return taco;
35+
}
36+
37+
private void saveIngredientToTaco(final Ingredient ingredient,
38+
final long tacoId) {
39+
this.jdbc.update("insert into Taco_Ingredients (taco, ingredient) values (?, ?)",
40+
tacoId, ingredient.getId());
41+
}
42+
43+
private long saveTacoInfo(final Taco taco) {
44+
taco.setCreatedAt(Date.valueOf(LocalDate.now()));
45+
final PreparedStatementCreator psc = new PreparedStatementCreatorFactory(
46+
"insert into Taco (name, createdAt) values (?,?)",
47+
Types.VARCHAR, Types.TIMESTAMP
48+
).newPreparedStatementCreator(Arrays.asList(taco.getName(),
49+
new Timestamp(taco.getCreatedAt().getTime())));
50+
51+
final KeyHolder keyHolder = new GeneratedKeyHolder();
52+
this.jdbc.update(psc, keyHolder);
53+
return keyHolder.getKey().longValue();
54+
}
55+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package es.msanchez.spring.springinaction.dao;
2+
3+
import es.msanchez.spring.springinaction.entities.Order;
4+
5+
public interface OrderRepository {
6+
7+
public Order save(final Order order);
8+
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package es.msanchez.spring.springinaction.dao;
2+
3+
import es.msanchez.spring.springinaction.entities.Taco;
4+
5+
public interface TacoRepository {
6+
7+
public Taco save(final Taco taco);
8+
9+
}

spring/spring-in-action/src/main/java/es/msanchez/spring/springinaction/entities/Taco.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ public class Taco {
1919
private String name;
2020

2121
@Size(min = 1, message = "You must select at least 1 ingredient")
22-
private List<String> ingredients;
22+
private List<Ingredient> ingredients;
2323

2424
}

spring/spring-in-action/src/main/java/es/msanchez/spring/springinaction/repositories/IngredientRepository.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

spring/spring-in-action/src/main/java/es/msanchez/spring/springinaction/repositories/JdbcIngredientRepository.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

0 commit comments

Comments
 (0)