Skip to content
Snippets Groups Projects
Commit a4012f12 authored by Lucas Constant's avatar Lucas Constant
Browse files

ressource pizza

parent 99aecdf3
Branches master
No related tags found
No related merge requests found
......@@ -6,11 +6,42 @@ 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 {
private static final Logger LOGGER = Logger.getLogger(ApiV1.class.getName());
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);
}
}
}
}
......@@ -4,6 +4,8 @@ import fr.ulille.iut.pizzaland.dto.IngredientCreateDto;
import fr.ulille.iut.pizzaland.dto.IngredientDto;
import java.util.Objects;
public class Ingredient {
private long id;
private String name;
......@@ -80,6 +82,11 @@ public class Ingredient {
return ingredient;
}
@Override
public int hashCode() {
return Objects.hash(id, name);
}
@Override
public String toString() {
return "Ingredient [id=" + id + ", name=" + name + "]";
......
package fr.ulille.iut.pizzaland.beans;
import fr.ulille.iut.pizzaland.dto.IngredientCreateDto;
import fr.ulille.iut.pizzaland.dto.IngredientDto;
import fr.ulille.iut.pizzaland.dto.PizzaCreateDto;
import fr.ulille.iut.pizzaland.dto.PizzaDto;
import java.util.Objects;
public class Pizza {
private long idPizza;
private String name;
public Pizza() {
}
public Pizza(long id, String name) {
this.idPizza = id;
this.name = name;
}
public void setId(long id) {
this.idPizza = id;
}
public long getId() {
return idPizza;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static PizzaDto toDto(Pizza i) {
PizzaDto dto = new PizzaDto();
dto.setId(i.getId());
dto.setName(i.getName());
return dto;
}
public static Pizza fromDto(PizzaDto dto) {
Pizza pizza = new Pizza();
pizza.setId(dto.getId());
pizza.setName(dto.getName());
return pizza;
}
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;
}
@Override
public int hashCode() {
return Objects.hash(idPizza, name);
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Pizza pizza = (Pizza) o;
return idPizza == pizza.idPizza &&
Objects.equals(name, pizza.name);
}
@Override
public String toString() {
return "Pizza [id=" + idPizza + ", name=" + name + "]";
}
}
\ No newline at end of file
package fr.ulille.iut.pizzaland.dao;
import fr.ulille.iut.pizzaland.beans.Ingredient;
import fr.ulille.iut.pizzaland.beans.Pizza;
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 org.jdbi.v3.sqlobject.transaction.Transaction;
import java.util.List;
public interface PizzaDao {
@SqlUpdate("CREATE TABLE IF NOT EXISTS Pizzas (idPizza INTEGER PRIMARY KEY, name VARCHAR UNIQUE NOT NULL)")
void createPizzaTable();
@SqlUpdate("CREATE TABLE IF NOT EXISTS PizzaIngredientsAssociation ( idPizza int NOT NULL,\n" +
" idIngredient int NOT NULL,\n" +
" CONSTRAINT PK_PizzaIngredient PRIMARY KEY (\n" +
" idPizza,\n" +
" idIngredient\n" +
" ),\n" +
" FOREIGN KEY (idPizza) REFERENCES Pizzas (idPizza),\n" +
" FOREIGN KEY (idIngredient) REFERENCES ingredients (id))")
void createAssociationTable();
@Transaction
default void createTableAndIngredientAssociation() {
createAssociationTable();
createPizzaTable();
}
@SqlUpdate("DROP TABLE IF EXISTS Pizzas")
void dropTable();
@SqlUpdate("INSERT INTO Pizzas (name) VALUES (:name)")
@GetGeneratedKeys
long insert(String name);
@SqlQuery("SELECT * FROM Pizzas")
@RegisterBeanMapper(Pizza.class)
List<Ingredient> getAll();
@SqlQuery("SELECT * FROM Pizzas WHERE idPizzas = :id")
@RegisterBeanMapper(Pizza.class)
Ingredient findById(long id);
@SqlQuery("SELECT * FROM Pizzas WHERE name = :name")
@RegisterBeanMapper(Pizza.class)
Ingredient findByName(String name);
@SqlUpdate("DELETE FROM Pizzas WHERE idPizzas = :id")
void remove(long id);
}
\ No newline at end of file
package fr.ulille.iut.pizzaland.dto;
public class PizzaCreateDto {
private String name;
public PizzaCreateDto() {}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
package fr.ulille.iut.pizzaland.dto;
public class PizzaDto {
private long id;
private String name;
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;
}
}
......@@ -85,4 +85,41 @@ public class IngredientResource {
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);
}
}
}
\ No newline at end of file
......@@ -9,11 +9,10 @@ import org.glassfish.jersey.test.TestProperties;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertNotNull;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.GenericType;
import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.*;
import static org.junit.Assert.assertEquals;
......@@ -163,4 +162,40 @@ public class IngredientResourceTest extends JerseyTest {
response.getStatus());
}
@Test
public void testGetIngredientName() {
Ingredient ingredient = new Ingredient();
ingredient.setName("mozzarella");
long id = dao.insert(ingredient.getName());
Response response = target("ingredients/" + id + "/name").request().get();
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
assertEquals("mozzarella", response.readEntity(String.class));
}
@Test
public void testGetNotExistingIngredientName() {
Response response = target("ingredients/125/name").request().get();
assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus());
}
/*
@Test
public void testCreateWithForm() {
Form form = new Form();
form.param("name", "chorizo");
Entity<Form> formEntity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED_TYPE);
Response response = target("ingredients").request().post(formEntity);
assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
String location = response.getHeaderString("Location");
long id = Integer.parseInt(location.substring(location.lastIndexOf('/') + 1));
Ingredient result = dao.findById(id);
assertNotNull(result);
}
*/
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment