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: implementing embedded DBB
spring-action: implementing embedded DBB
- Loading branch information
1 parent
0bf98bc
commit c462b1a
Showing
12 changed files
with
299 additions
and
18 deletions.
There are no files selected for viewing
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
11 changes: 11 additions & 0 deletions
11
...spring-in-action/src/main/java/es/msanchez/spring/springinaction/config/SpringConfig.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,11 @@ | ||
package es.msanchez.spring.springinaction.config; | ||
|
||
import org.springframework.context.annotation.ComponentScan; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
@ComponentScan(basePackages = { | ||
"es.msanchez.spring.springinaction.**" | ||
}) | ||
public class SpringConfig { | ||
} |
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
15 changes: 15 additions & 0 deletions
15
...g-in-action/src/main/java/es/msanchez/spring/springinaction/dao/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,15 @@ | ||
package es.msanchez.spring.springinaction.dao; | ||
|
||
import es.msanchez.spring.springinaction.entities.Ingredient; | ||
import org.springframework.stereotype.Repository; | ||
|
||
@Repository | ||
public interface IngredientRepository { | ||
|
||
Iterable<Ingredient> findAll(); | ||
|
||
Ingredient findOne(final String id); | ||
|
||
Ingredient save(final Ingredient ingredient); | ||
|
||
} |
52 changes: 52 additions & 0 deletions
52
...-action/src/main/java/es/msanchez/spring/springinaction/dao/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,52 @@ | ||
package es.msanchez.spring.springinaction.dao; | ||
|
||
import es.msanchez.spring.springinaction.entities.Ingredient; | ||
import es.msanchez.spring.springinaction.enums.Type; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.sql.ResultSet; | ||
import java.sql.SQLException; | ||
|
||
@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 this.jdbcTemplate.query("select id, name, type from Ingredient", this::mapRowToIngredient); | ||
} | ||
|
||
private Ingredient mapRowToIngredient(final ResultSet rs, | ||
final int rowNum) throws SQLException { | ||
return new Ingredient(rs.getString("id"), | ||
rs.getString("name"), | ||
Type.valueOf(rs.getString("type"))); | ||
} | ||
|
||
@Override | ||
public Ingredient findOne(final String id) { | ||
return this.jdbcTemplate.queryForObject("select id, name, type from Ingredient where id = ?", | ||
this::mapRowToIngredient, id); | ||
} | ||
|
||
@Override | ||
public Ingredient save(final Ingredient ingredient) { | ||
this.jdbcTemplate.update("insert into Ingredient (id, name, type) values (?, ?, ?)", | ||
ingredient.getId(), | ||
ingredient.getName(), | ||
ingredient.getType().toString()); | ||
return ingredient; | ||
} | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
...ing-in-action/src/main/java/es/msanchez/spring/springinaction/dao/JdbcTacoRepository.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,55 @@ | ||
package es.msanchez.spring.springinaction.dao; | ||
|
||
import es.msanchez.spring.springinaction.entities.Ingredient; | ||
import es.msanchez.spring.springinaction.entities.Taco; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.jdbc.core.JdbcTemplate; | ||
import org.springframework.jdbc.core.PreparedStatementCreator; | ||
import org.springframework.jdbc.core.PreparedStatementCreatorFactory; | ||
import org.springframework.jdbc.support.GeneratedKeyHolder; | ||
import org.springframework.jdbc.support.KeyHolder; | ||
import org.springframework.stereotype.Repository; | ||
|
||
import java.sql.Date; | ||
import java.sql.Timestamp; | ||
import java.sql.Types; | ||
import java.time.LocalDate; | ||
import java.util.Arrays; | ||
|
||
@Repository | ||
public class JdbcTacoRepository implements TacoRepository { | ||
|
||
private final JdbcTemplate jdbc; | ||
|
||
@Autowired | ||
public JdbcTacoRepository(final JdbcTemplate jdbc) { | ||
this.jdbc = jdbc; | ||
} | ||
|
||
@Override | ||
public Taco save(final Taco taco) { | ||
final long tacoId = this.saveTacoInfo(taco); | ||
taco.setId(tacoId); | ||
taco.getIngredients().forEach(ing -> this.saveIngredientToTaco(ing, tacoId)); | ||
return taco; | ||
} | ||
|
||
private void saveIngredientToTaco(final Ingredient ingredient, | ||
final long tacoId) { | ||
this.jdbc.update("insert into Taco_Ingredients (taco, ingredient) values (?, ?)", | ||
tacoId, ingredient.getId()); | ||
} | ||
|
||
private long saveTacoInfo(final Taco taco) { | ||
taco.setCreatedAt(Date.valueOf(LocalDate.now())); | ||
final PreparedStatementCreator psc = new PreparedStatementCreatorFactory( | ||
"insert into Taco (name, createdAt) values (?,?)", | ||
Types.VARCHAR, Types.TIMESTAMP | ||
).newPreparedStatementCreator(Arrays.asList(taco.getName(), | ||
new Timestamp(taco.getCreatedAt().getTime()))); | ||
|
||
final KeyHolder keyHolder = new GeneratedKeyHolder(); | ||
this.jdbc.update(psc, keyHolder); | ||
return keyHolder.getKey().longValue(); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
...spring-in-action/src/main/java/es/msanchez/spring/springinaction/dao/OrderRepository.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,9 @@ | ||
package es.msanchez.spring.springinaction.dao; | ||
|
||
import es.msanchez.spring.springinaction.entities.Order; | ||
|
||
public interface OrderRepository { | ||
|
||
public Order save(final Order order); | ||
|
||
} |
9 changes: 9 additions & 0 deletions
9
.../spring-in-action/src/main/java/es/msanchez/spring/springinaction/dao/TacoRepository.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,9 @@ | ||
package es.msanchez.spring.springinaction.dao; | ||
|
||
import es.msanchez.spring.springinaction.entities.Taco; | ||
|
||
public interface TacoRepository { | ||
|
||
public Taco save(final Taco taco); | ||
|
||
} |
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
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
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,26 @@ | ||
DELETE FROM Taco_Order_Tacos; | ||
DELETE FROM Taco_Ingredients; | ||
DELETE FROM Taco; | ||
DELETE FROM Taco_Order; | ||
DELETE FROM Ingredient; | ||
|
||
INSERT INTO Ingredient (id, name, type) | ||
VALUES ("FLTO", "Flour Tortilla", "Wrap"); | ||
INSERT INTO Ingredient (id, name, type) | ||
VALUES ("COTO", "Corn Tortilla", "Wrap"); | ||
INSERT INTO Ingredient (id, name, type) | ||
VALUES ("GRBF", "Ground Beef", "Protein"); | ||
INSERT INTO Ingredient (id, name, type) | ||
VALUES ("CARN", "Carnitas", "Protein"); | ||
INSERT INTO Ingredient (id, name, type) | ||
VALUES ("TMTO", "Diced Tomatoes", "Veggies"); | ||
INSERT INTO Ingredient (id, name, type) | ||
VALUES ("LETC", "Lettuce", "Veggies"); | ||
INSERT INTO Ingredient (id, name, type) | ||
VALUES ("CHED", "Cheddar", "Cheese"); | ||
INSERT INTO Ingredient (id, name, type) | ||
VALUES ("JACK", "Monterrey Jack", "Cheese"); | ||
INSERT INTO Ingredient (id, name, type) | ||
VALUES ("SLSA", "Salsa", "Sauce"); | ||
INSERT INTO Ingredient (id, name, type) | ||
VALUES ("SRCR", "Sour Cream", "Sauce"); |
Oops, something went wrong.