Skip to content
Snippets Groups Projects
Commit 9e68076f authored by deksor's avatar deksor
Browse files

Tuto terminé et Pizza en cours d'ajout

parent b3f5ebe9
Branches master
No related tags found
No related merge requests found
Showing
with 684 additions and 6 deletions
README.md 100644 → 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
pom.xml 100644 → 100755
File mode changed from 100644 to 100755
......@@ -5,6 +5,18 @@ import org.glassfish.jersey.server.ResourceConfig;
import java.util.logging.Logger;
import javax.ws.rs.ApplicationPath;
import fr.ulille.iut.pizzaland.beans.Ingredient;
import fr.ulille.iut.pizzaland.dao.IngredientDao;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import javax.json.bind.Jsonb;
import javax.json.bind.JsonbBuilder;
@ApplicationPath("api/v1/")
public class ApiV1 extends ResourceConfig {
......@@ -12,5 +24,26 @@ public class ApiV1 extends ResourceConfig {
public ApiV1() {
packages("fr.ulille.iut.pizzaland");
String environment = System.getenv("PIZZAENV");
if ( environment != null && environment.equals("withdb") ) {
LOGGER.info("Loading with database");
Jsonb jsonb = JsonbBuilder.create();
try {
FileReader reader = new FileReader( getClass().getClassLoader().getResource("ingredients.json").getFile() );
List<Ingredient> ingredients = JsonbBuilder.create().fromJson(reader, new ArrayList<Ingredient>(){}.getClass().getGenericSuperclass());
IngredientDao ingredientDao = BDDFactory.buildDao(IngredientDao.class);
ingredientDao.dropTable();
ingredientDao.createTable();
for ( Ingredient ingredient: ingredients) {
ingredientDao.insert(ingredient.getName());
}
}
catch ( Exception ex ) {
throw new IllegalStateException(ex);
}
}
}
}
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
package fr.ulille.iut.pizzaland.beans;
import fr.ulille.iut.pizzaland.dto.IngredientDto;
import fr.ulille.iut.pizzaland.dto.IngredientCreateDto;
public class Ingredient {
private long id;
private String name;
public Ingredient() {
}
public Ingredient(long id, String name) {
this.id = id;
this.name = name;
}
public void setId(long id) {
this.id = id;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static IngredientDto toDto(Ingredient i) {
IngredientDto dto = new IngredientDto();
dto.setId(i.getId());
dto.setName(i.getName());
return dto;
}
public static Ingredient fromDto(IngredientDto dto) {
Ingredient ingredient = new Ingredient();
ingredient.setId(dto.getId());
ingredient.setName(dto.getName());
return ingredient;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Ingredient other = (Ingredient) obj;
if (id != other.id)
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
@Override
public String toString() {
return "Ingredient [id=" + id + ", name=" + name + "]";
}
public static IngredientCreateDto toCreateDto(Ingredient ingredient) {
IngredientCreateDto dto = new IngredientCreateDto();
dto.setName(ingredient.getName());
return dto;
}
public static Ingredient fromIngredientCreateDto(IngredientCreateDto dto) {
Ingredient ingredient = new Ingredient();
ingredient.setName(dto.getName());
return ingredient;
}
}
package fr.ulille.iut.pizzaland.beans;
import java.util.List;
import java.util.ArrayList;
import fr.ulille.iut.pizzaland.dto.PizzaDto;
import fr.ulille.iut.pizzaland.dto.PizzaCreateDto;
public class Pizza {
private long id;
private String name;
private List<Ingredient> ingredients;
public Pizza() {
this.ingredients = new ArrayList<Ingredient>();
}
public Pizza(long id, String name) {
this.id = id;
this.name = name;
this.ingredients = new ArrayList<Ingredient>();
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public List<Ingredient> getIngredients() {
return this.ingredients;
}
public void setIngredients(List<Ingredient> ingredients) {
this.ingredients = ingredients;
}
public void addIngredients(Ingredient ingredient) {
this.ingredients.add(ingredient);
}
public static PizzaDto toDto(Pizza p) {
PizzaDto dto = new PizzaDto();
dto.setId(p.getId());
dto.setName(p.getName());
dto.setIngredients(p.getIngredients());
return dto;
}
public static Pizza fromDto(PizzaDto dto) {
Pizza pizza = new Pizza();
pizza.setId(dto.getId());
pizza.setName(dto.getName());
pizza.setIngredients(dto.getIngredients());
return pizza;
}
@Override
public String toString() {
return "Pizza [id=" + id + ", ingredients=" + ingredients + ", name=" + name + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (int) (id ^ (id >>> 32));
result = prime * result + ((ingredients == null) ? 0 : ingredients.hashCode());
result = prime * result + ((name == null) ? 0 : name.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Pizza other = (Pizza) obj;
if (id != other.id)
return false;
if (ingredients == null) {
if (other.ingredients != null)
return false;
} else if (!ingredients.equals(other.ingredients))
return false;
if (name == null) {
if (other.name != null)
return false;
} else if (!name.equals(other.name))
return false;
return true;
}
public static PizzaCreateDto toCreateDto(Pizza pizza) {
PizzaCreateDto dto = new PizzaCreateDto();
dto.setName(pizza.getName());
return dto;
}
public static Pizza fromPizzaCreateDto(PizzaCreateDto dto) {
Pizza pizza = new Pizza();
pizza.setName(dto.getName());
return pizza;
}
}
File mode changed from 100644 to 100755
package fr.ulille.iut.pizzaland.dao;
import java.util.List;
import org.jdbi.v3.sqlobject.config.RegisterBeanMapper;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import fr.ulille.iut.pizzaland.beans.Ingredient;
public interface IngredientDao {
@SqlUpdate("CREATE TABLE IF NOT EXISTS ingredients (id INTEGER PRIMARY KEY, name VARCHAR UNIQUE NOT NULL)")
void createTable();
@SqlUpdate("DROP TABLE IF EXISTS ingredients")
void dropTable();
@SqlUpdate("INSERT INTO ingredients (name) VALUES (:name)")
@GetGeneratedKeys
long insert(String name);
@SqlQuery("SELECT * FROM ingredients")
@RegisterBeanMapper(Ingredient.class)
List<Ingredient> getAll();
@SqlQuery("SELECT * FROM ingredients WHERE id = :id")
@RegisterBeanMapper(Ingredient.class)
Ingredient findById(long id);
@SqlQuery("SELECT * FROM ingredients WHERE name = :name")
@RegisterBeanMapper(Ingredient.class)
Ingredient findByName(String name);
@SqlUpdate("DELETE FROM ingredients WHERE id = :id")
void remove(long id);
}
package fr.ulille.iut.pizzaland.dao;
import org.jdbi.v3.sqlobject.transaction.Transaction;
import org.jdbi.v3.sqlobject.config.RegisterBeanMapper;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;
import java.util.List;
import fr.ulille.iut.pizzaland.beans.Pizza;
import fr.ulille.iut.pizzaland.beans.Ingredient;
public interface PizzaDao {
@SqlUpdate("CREATE TABLE IF NOT EXISTS pizza (id INTEGER PRIMARY KEY, name VARCHAR UNIQUE NOT NULL)")
void createPizzaTable();
@SqlUpdate("CREATE TABLE IF NOT EXISTS PizzaIngredientsAssociation (idPizza INTEGER NOT NULL, idIngredient INTEGER NOT NULL, CONSTRAINT PizzaIngredientsAssociation_pk PRIMARY KEY (idPizza, idIngredient), FOREIGN KEY (idIngredient) REFERENCES Ingredient(id), FOREIGN KEY (idPizza) REFERENCES pizza(id))")
void createAssociationTable();
@SqlUpdate("DROP TABLE IF EXISTS pizza")
void dropPizzaTable();
@SqlUpdate("DROP TABLE IF EXISTS PizzaIngredientsAssociation")
void dropAssociationTable();
@Transaction
default void createTableAndIngredientAssociation() {
createPizzaTable();
createAssociationTable();
}
@Transaction
default void dropTableAndIngredientAssociation() {
dropAssociationTable();
dropPizzaTable();
}
@Transaction
default void removePizza(long id) {
removePizzaFromAssociationTable(id);
removePizza(id);
}
@SqlUpdate("INSERT INTO pizza (name) VALUES (:name)")
@GetGeneratedKeys
long insert(String name);
@SqlUpdate("INSERT INTO PizzaIngredientsAssociation VALUES (:pizzaId, :ingredientId)")
long addIngredient(long pizzaId, long ingredientId);
@SqlQuery("SELECT * FROM pizza")
@RegisterBeanMapper(Pizza.class)
List<Pizza> getAll();
@SqlQuery("SELECT ingredients.* FROM ingredients JOIN PizzaIngredientsAssociation ON idIngredient = ingredients.id WHERE pizzaId = :id")
@RegisterBeanMapper(Pizza.class)
List<Ingredient> getIngredients(long id);
@SqlQuery("SELECT * FROM pizza WHERE id = :id")
@RegisterBeanMapper(Pizza.class)
Pizza findById(long id);
@SqlQuery("SELECT * FROM ingredients WHERE name = :name")
@RegisterBeanMapper(Pizza.class)
Pizza findByName(String name);
@SqlUpdate("DELETE FROM pizza WHERE id = :id")
void removePizzaFromTable(long id);
@SqlUpdate("DELETE FROM PizzaIngredientsAssociation WHERE idPizza = :id")
void removePizzaFromAssociationTable(long id);
@SqlUpdate("DELETE FROM PizzaIngredientsAssociation WHERE idPizza = :idPizza AND idIngredient = :idIngredient")
void removeIngredient(long idPizza, long idIngredient);
}
package fr.ulille.iut.pizzaland.dto;
public class IngredientCreateDto {
private String name;
public IngredientCreateDto() {}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
......@@ -2,7 +2,25 @@ package fr.ulille.iut.pizzaland.dto;
public class IngredientDto {
public IngredientDto() {
private long id;
private String name;
public IngredientDto() {}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
package fr.ulille.iut.pizzaland.dto;
import java.util.List;
import fr.ulille.iut.pizzaland.beans.Ingredient;
public class PizzaCreateDto {
private String name;
private List<Ingredient> ingredients;
public PizzaCreateDto() {}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setIngredients(List<Ingredient> ingredients) {
this.ingredients = ingredients;
}
public List<Ingredient> getIngredients() {
return this.ingredients;
}
}
package fr.ulille.iut.pizzaland.dto;
import java.util.List;
import fr.ulille.iut.pizzaland.beans.Ingredient;
public class PizzaDto {
private long id;
private String name;
private List<Ingredient> ingredients;
public PizzaDto() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public List<Ingredient> getIngredients() {
return this.ingredients;
}
public void setIngredients(List<Ingredient> ingredients) {
this.ingredients = ingredients;
}
public void addIngredients(Ingredient ingredient) {
this.ingredients.add(ingredient);
}
}
package fr.ulille.iut.pizzaland.resources;
import fr.ulille.iut.pizzaland.BDDFactory;
import fr.ulille.iut.pizzaland.beans.Ingredient;
import fr.ulille.iut.pizzaland.dao.IngredientDao;
import fr.ulille.iut.pizzaland.dto.IngredientCreateDto;
import fr.ulille.iut.pizzaland.dto.IngredientDto;
import java.net.URI;
import java.util.List;
import java.util.ArrayList;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import fr.ulille.iut.pizzaland.dto.IngredientDto;
@Path("/ingredients")
/*@Path("/ingredients")
public class IngredientResource {
private static final Logger LOGGER = Logger.getLogger(IngredientResource.class.getName());
......@@ -29,4 +40,118 @@ public class IngredientResource {
return null;
}
}*/
@Path("ingredients")
public class IngredientResource {
private IngredientDao ingredients;
private static final Logger LOGGER = Logger.getLogger(IngredientResource.class.getName());
@Context
public UriInfo uriInfo;
public IngredientResource() {
ingredients = BDDFactory.buildDao(IngredientDao.class);
ingredients.createTable();
}
@GET
public List<IngredientDto> getAll() {
LOGGER.info("IngredientResource:getAll");
List<IngredientDto> l = ingredients.getAll().stream().map(Ingredient::toDto).collect(Collectors.toList());
return l;
}
@GET
@Path("{id}")
public IngredientDto getOneIngredient(@PathParam("id") long id) {
LOGGER.info("getOneIngredient(" + id + ")");
try {
Ingredient ingredient = ingredients.findById(id);
return Ingredient.toDto(ingredient);
}
catch ( Exception e ) {
// Cette exception générera une réponse avec une erreur 404
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
}
@POST
public Response createIngredient(IngredientCreateDto ingredientCreateDto) {
Ingredient existing = ingredients.findByName(ingredientCreateDto.getName());
if ( existing != null ) {
throw new WebApplicationException(Response.Status.CONFLICT);
}
try {
Ingredient ingredient = Ingredient.fromIngredientCreateDto(ingredientCreateDto);
long id = ingredients.insert(ingredient.getName());
ingredient.setId(id);
IngredientDto ingredientDto = Ingredient.toDto(ingredient);
URI uri = uriInfo.getAbsolutePathBuilder().path("" + id).build();
return Response.created(uri).entity(ingredientDto).build();
}
catch ( Exception e ) {
e.printStackTrace();
throw new WebApplicationException(Response.Status.NOT_ACCEPTABLE);
}
}
@DELETE
@Path("{id}")
public Response deleteIngredient(@PathParam("id") long id) {
if ( ingredients.findById(id) == null ) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
ingredients.remove(id);
return Response.status(Response.Status.ACCEPTED).build();
}
@GET
@Path("{id}/name")
public String getIngredientName(@PathParam("id") long id) {
Ingredient ingredient = ingredients.findById(id);
if ( ingredient == null ) {
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
return ingredient.getName();
}
@POST
@Consumes("application/x-www-form-urlencoded")
public Response createIngredient(@FormParam("name") String name) {
Ingredient existing = ingredients.findByName(name);
if ( existing != null ) {
throw new WebApplicationException(Response.Status.CONFLICT);
}
try {
Ingredient ingredient = new Ingredient();
ingredient.setName(name);
long id = ingredients.insert(ingredient.getName());
ingredient.setId(id);
IngredientDto ingredientDto = Ingredient.toDto(ingredient);
URI uri = uriInfo.getAbsolutePathBuilder().path("" + id).build();
return Response.created(uri).entity(ingredientDto).build();
}
catch ( Exception e ) {
e.printStackTrace();
throw new WebApplicationException(Response.Status.NOT_ACCEPTABLE);
}
}
}
package fr.ulille.iut.pizzaland.resources;
import fr.ulille.iut.pizzaland.BDDFactory;
import fr.ulille.iut.pizzaland.beans.Pizza;
import fr.ulille.iut.pizzaland.dao.PizzaDao;
import fr.ulille.iut.pizzaland.dto.PizzaCreateDto;
import fr.ulille.iut.pizzaland.dto.PizzaDto;
import java.net.URI;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
@Path("pizzas")
public class PizzaResource {
private static final Logger LOGGER = Logger.getLogger(PizzaResource.class.getName());
private PizzaDao pizzas;
@Context
public UriInfo uriInfo;
public PizzaResource() {
pizzas = BDDFactory.buildDao(PizzaDao.class);
pizzas.createTableAndIngredientAssociation();
}
@GET
public List<PizzaDto> getAll() {
LOGGER.info("PizzaResource:getAll");
return null;
}
@GET
@Path("{id}")
public PizzaDto getOnePizza(@PathParam("id") long id) {
LOGGER.info("getOnePizza(" + id + ")");
try {
Pizza pizza = pizzas.findById(id);
return Pizza.toDto(pizza);
}
catch ( Exception e ) {
// Cette exception générera une réponse avec une erreur 404
throw new WebApplicationException(Response.Status.NOT_FOUND);
}
}
@POST
public Response createPizza(PizzaCreateDto pizzaCreateDto) {
Pizza existing = pizzas.findByName(pizzaCreateDto.getName());
if ( existing != null ) {
throw new WebApplicationException(Response.Status.CONFLICT);
}
try {
Pizza pizza = Pizza.fromPizzaCreateDto(pizzaCreateDto);
long id = pizzas.insert(pizza.getName());
pizza.setId(id);
PizzaDto pizzaDto = Pizza.toDto(pizza);
URI uri = uriInfo.getAbsolutePathBuilder().path("" + id).build();
return Response.created(uri).entity(pizzaDto).build();
}
catch ( Exception e ) {
e.printStackTrace();
throw new WebApplicationException(Response.Status.NOT_ACCEPTABLE);
}
}
}
\ No newline at end of file
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
File mode changed from 100644 to 100755
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment