Skip to content
Snippets Groups Projects
Commit faf68521 authored by Hugo Blanquart's avatar Hugo Blanquart
Browse files

json

parent 2879b8fb
Branches master
No related tags found
No related merge requests found
...@@ -6,6 +6,7 @@ import java.util.logging.Logger; ...@@ -6,6 +6,7 @@ import java.util.logging.Logger;
import javax.ws.rs.ApplicationPath; import javax.ws.rs.ApplicationPath;
import fr.ulille.iut.pizzaland.beans.Ingredient; import fr.ulille.iut.pizzaland.beans.Ingredient;
import fr.ulille.iut.pizzaland.beans.Pizza;
import fr.ulille.iut.pizzaland.dao.IngredientDao; import fr.ulille.iut.pizzaland.dao.IngredientDao;
import fr.ulille.iut.pizzaland.dao.PizzaDao; import fr.ulille.iut.pizzaland.dao.PizzaDao;
...@@ -33,6 +34,7 @@ public class ApiV1 extends ResourceConfig { ...@@ -33,6 +34,7 @@ public class ApiV1 extends ResourceConfig {
FileReader reader = new FileReader( getClass().getClassLoader().getResource("ingredients.json").getFile() ); FileReader reader = new FileReader( getClass().getClassLoader().getResource("ingredients.json").getFile() );
List<Ingredient> ingredients = JsonbBuilder.create().fromJson(reader, new ArrayList<Ingredient>(){}.getClass().getGenericSuperclass()); List<Ingredient> ingredients = JsonbBuilder.create().fromJson(reader, new ArrayList<Ingredient>(){}.getClass().getGenericSuperclass());
IngredientDao ingredientDao = BDDFactory.buildDao(IngredientDao.class); IngredientDao ingredientDao = BDDFactory.buildDao(IngredientDao.class);
ingredientDao.dropTable(); ingredientDao.dropTable();
ingredientDao.createTable(); ingredientDao.createTable();
...@@ -40,7 +42,21 @@ public class ApiV1 extends ResourceConfig { ...@@ -40,7 +42,21 @@ public class ApiV1 extends ResourceConfig {
ingredientDao.insert(ingredient.getName()); ingredientDao.insert(ingredient.getName());
} }
FileReader readerPizza = new FileReader( getClass().getClassLoader().getResource("pizzas.json").getFile() );
List<Pizza> pizzas= JsonbBuilder.create().fromJson(readerPizza, new ArrayList<Pizza>(){}.getClass().getGenericSuperclass());
PizzaDao pizzaDao = BDDFactory.buildDao(PizzaDao.class); PizzaDao pizzaDao = BDDFactory.buildDao(PizzaDao.class);
pizzaDao.dropTablePizza();
pizzaDao.dropTableAssociations();
pizzaDao.createTableAndIngredientAssociation();
for (Pizza pizza : pizzas) {
System.out.println(pizza.getName() + " - " +pizza.getIngredients().toString());
pizzaDao.insert(pizza.getName());
for(Ingredient ingredient : pizza.getIngredients()) {
System.out.println(pizza.getId() +"-" +ingredient.getId());
pizzaDao.insertAssoc(pizza.getId(), ingredient.getId());
}
}
} catch ( Exception ex ) { } catch ( Exception ex ) {
......
...@@ -42,7 +42,7 @@ public class Pizza { ...@@ -42,7 +42,7 @@ public class Pizza {
} }
public List<Ingredient> getIngredients() { public List<Ingredient> getIngredients() {
return ingredient; return this.ingredient;
} }
public static PizzaDto toDto(Pizza p) { public static PizzaDto toDto(Pizza p) {
......
...@@ -12,7 +12,7 @@ import fr.ulille.iut.pizzaland.beans.Pizza; ...@@ -12,7 +12,7 @@ import fr.ulille.iut.pizzaland.beans.Pizza;
public interface PizzaDao { public interface PizzaDao {
@SqlUpdate("CREATE TABLE IF NOT EXISTS Pizzas (id INTEGER PRIMARY KEY, name VARCHAR UNIQUE NOT NULL") @SqlUpdate("CREATE TABLE IF NOT EXISTS Pizzas (id INTEGER PRIMARY KEY, name VARCHAR UNIQUE NOT NULL)")
void createPizzaTable(); void createPizzaTable();
@SqlUpdate("CREATE TABLE IF NOT EXISTS PizzaIngredientsAssociation(pizza Integer," @SqlUpdate("CREATE TABLE IF NOT EXISTS PizzaIngredientsAssociation(pizza Integer,"
...@@ -23,8 +23,8 @@ public interface PizzaDao { ...@@ -23,8 +23,8 @@ public interface PizzaDao {
@Transaction @Transaction
default void createTableAndIngredientAssociation() { default void createTableAndIngredientAssociation() {
createAssociationTable();
createPizzaTable(); createPizzaTable();
createAssociationTable();
} }
...@@ -50,6 +50,10 @@ public interface PizzaDao { ...@@ -50,6 +50,10 @@ public interface PizzaDao {
@SqlUpdate("INSERT INTO Pizzas (name) VALUES(:name)") @SqlUpdate("INSERT INTO Pizzas (name) VALUES(:name)")
@GetGeneratedKeys @GetGeneratedKeys
long insert(String name); long insert(String name);
@SqlUpdate("INSERT INTO PizzaIngredientsAssociation (pizza,ingredient) VALUES(:pizza,:ingredient)")
@GetGeneratedKeys
long insertAssoc(long pizza,long ingredient);
@SqlUpdate("DELETE FROM Pizzas WHERE id = :id") @SqlUpdate("DELETE FROM Pizzas WHERE id = :id")
void remove(long id); void remove(long id);
......
...@@ -43,7 +43,7 @@ public class PizzaResource { ...@@ -43,7 +43,7 @@ public class PizzaResource {
public PizzaResource() { public PizzaResource() {
pizzas = BDDFactory.buildDao(PizzaDao.class); pizzas = BDDFactory.buildDao(PizzaDao.class);
pizzas.createPizzaTable(); pizzas.createTableAndIngredientAssociation();
} }
@GET @GET
......
[
{"name": "reine","ingredient": [{"id":1,"name":"mozzarella" },{"id":2,"name":"jambon" },{"id":3,"name":"champignons" }]},
{"name": "margarita","ingredient": [{"id":1,"name":"mozzarella" },{"id":2,"name":"jambon" },{"id":5,"name":"tomate" }]},
{"name": "4 fromages","ingredient": [{"id":1,"name":"mozzarella" },{"id":8,"name":"fromage" }]},
{"name": "pesto","ingredient": [{"id":1,"name":"mozzarella" },{"id":2,"name":"jambon" },{"id":6,"name":"merguez" }]}
]
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment