diff --git a/src/main/java/fr/ulille/iut/pizzaland/ApiV1.java b/src/main/java/fr/ulille/iut/pizzaland/ApiV1.java index df1bb21a4e678d8b53598998f2b7f49a305828f0..f7a21247d2470ca923702e55336b7c8908c4592e 100644 --- a/src/main/java/fr/ulille/iut/pizzaland/ApiV1.java +++ b/src/main/java/fr/ulille/iut/pizzaland/ApiV1.java @@ -2,8 +2,15 @@ package fr.ulille.iut.pizzaland; import org.glassfish.jersey.server.ResourceConfig; +import fr.ulille.iut.pizzaland.beans.Ingredient; +import fr.ulille.iut.pizzaland.dao.IngredientDao; + +import java.io.FileReader; +import java.util.ArrayList; +import java.util.List; import java.util.logging.Logger; +import jakarta.json.bind.JsonbBuilder; import jakarta.ws.rs.ApplicationPath; @ApplicationPath("api/v1/") @@ -12,5 +19,23 @@ 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"); + 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); + } + } catch ( Exception ex ) { + throw new IllegalStateException(ex); + } + } } } diff --git a/src/main/java/fr/ulille/iut/pizzaland/Main.java b/src/main/java/fr/ulille/iut/pizzaland/Main.java index 1c96a03d45ec5c92140f090aa2692f2918a4b940..6222aee4ee7f96000ee11470f7332d446b334d93 100644 --- a/src/main/java/fr/ulille/iut/pizzaland/Main.java +++ b/src/main/java/fr/ulille/iut/pizzaland/Main.java @@ -2,14 +2,10 @@ package fr.ulille.iut.pizzaland; import java.io.IOException; import java.net.URI; -import java.util.logging.Level; import java.util.logging.LogManager; -import java.util.logging.Logger; import org.glassfish.grizzly.http.server.HttpServer; import org.glassfish.jersey.grizzly2.httpserver.GrizzlyHttpServerFactory; -import org.glassfish.jersey.logging.LoggingFeature; -import org.glassfish.jersey.server.ResourceConfig; /** * Main class. diff --git a/src/main/java/fr/ulille/iut/pizzaland/beans/Ingredient.java b/src/main/java/fr/ulille/iut/pizzaland/beans/Ingredient.java new file mode 100644 index 0000000000000000000000000000000000000000..2aece26f74eca7b30dc5d9c8c9df151807abea60 --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/beans/Ingredient.java @@ -0,0 +1,106 @@ +package fr.ulille.iut.pizzaland.beans; + +import java.util.UUID; + +import fr.ulille.iut.pizzaland.dto.IngredientCreateDto; +import fr.ulille.iut.pizzaland.dto.IngredientDto; + +public class Ingredient { + private UUID id = UUID.randomUUID(); + private String name; + + public Ingredient() { + } + + public Ingredient(String name) { + this.name = name; + } + + public Ingredient(UUID id, String name) { + this.id = id; + this.name = name; + } + + public void setId(UUID id) { + this.id = id; + } + + public UUID 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; + } + + 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; + } + + + @Override + public String toString() { + return "Ingredient [id=" + id + ", name=" + name + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.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; + Ingredient other = (Ingredient) obj; + if (id == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } +} diff --git a/src/main/java/fr/ulille/iut/pizzaland/beans/Pizza.java b/src/main/java/fr/ulille/iut/pizzaland/beans/Pizza.java new file mode 100644 index 0000000000000000000000000000000000000000..003a45703cb71f560c880063b6d2f3a2db0bb43f --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/beans/Pizza.java @@ -0,0 +1,115 @@ +package fr.ulille.iut.pizzaland.beans; + +import java.util.UUID; + +import fr.ulille.iut.pizzaland.dto.PizzaCreateDto; +import fr.ulille.iut.pizzaland.dto.PizzaDto; + +public class Pizza { + private UUID id = UUID.randomUUID(); + private String name; + private Ingredient[] ingredients; + + public Pizza() { + } + + public Pizza(String name) { + this.name = name; + } + + public Pizza(UUID id, String name) { + this.id = id; + this.name = name; + } + + public void setId(UUID id) { + this.id = id; + } + + public UUID getId() { + return id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Ingredient[] getIngredients() { + return ingredients; + } + + public void setIngredients(Ingredient[] ingredients) { + this.ingredients = ingredients; + } + + public static PizzaDto toDto(Pizza p) { + PizzaDto dto = new PizzaDto(); + dto.setId(p.getId()); + dto.setName(p.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 String toString() { + return "Ingredient [id=" + id + ", name=" + name + "]"; + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((id == null) ? 0 : id.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 == null) { + if (other.id != null) + return false; + } else if (!id.equals(other.id)) + return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + return true; + } +} diff --git a/src/main/java/fr/ulille/iut/pizzaland/dao/IngredientDao.java b/src/main/java/fr/ulille/iut/pizzaland/dao/IngredientDao.java new file mode 100644 index 0000000000000000000000000000000000000000..0215ac70bb11def9b0bd7327dcf72ffcfda9c189 --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/dao/IngredientDao.java @@ -0,0 +1,40 @@ +package fr.ulille.iut.pizzaland.dao; + +import java.util.List; +import java.util.UUID; + +import org.jdbi.v3.sqlobject.config.RegisterBeanMapper; +import org.jdbi.v3.sqlobject.customizer.Bind; +import org.jdbi.v3.sqlobject.customizer.BindBean; +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 VARCHAR(128) PRIMARY KEY, name VARCHAR UNIQUE NOT NULL)") + void createTable(); + + @SqlUpdate("DROP TABLE IF EXISTS ingredients") + void dropTable(); + + @SqlUpdate("INSERT INTO ingredients (id, name) VALUES (:id, :name)") + void insert(@BindBean Ingredient ingredient); + + @SqlUpdate("DELETE FROM ingredients WHERE id = :id") + void remove(@Bind("id") UUID id); + + @SqlQuery("SELECT * FROM ingredients WHERE name = :name") + @RegisterBeanMapper(Ingredient.class) + Ingredient findByName(@Bind("name") String name); + + @SqlQuery("SELECT * FROM ingredients") + @RegisterBeanMapper(Ingredient.class) + List<Ingredient> getAll(); + + @SqlQuery("SELECT * FROM ingredients WHERE id = :id") + @RegisterBeanMapper(Ingredient.class) + Ingredient findById(@Bind("id") UUID id); + +} diff --git a/src/main/java/fr/ulille/iut/pizzaland/dao/PizzaDao.java b/src/main/java/fr/ulille/iut/pizzaland/dao/PizzaDao.java new file mode 100644 index 0000000000000000000000000000000000000000..6c4e0ee9f703bccd952cccebf45921309f022214 --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/dao/PizzaDao.java @@ -0,0 +1,61 @@ +package fr.ulille.iut.pizzaland.dao; + +import java.util.List; +import java.util.UUID; + +import org.jdbi.v3.sqlobject.config.RegisterBeanMapper; +import org.jdbi.v3.sqlobject.customizer.Bind; +import org.jdbi.v3.sqlobject.customizer.BindBean; +import org.jdbi.v3.sqlobject.statement.SqlQuery; +import org.jdbi.v3.sqlobject.statement.SqlUpdate; +import org.jdbi.v3.sqlobject.transaction.Transaction; + +import fr.ulille.iut.pizzaland.beans.Pizza; + +public interface PizzaDao { + + @SqlUpdate("CREATE TABLE IF NOT EXISTS pizzas (id VARCHAR(128) PRIMARY KEY, name VARCHAR UNIQUE NOT NULL)") + void createPizzaTable(); + + @SqlUpdate("CREATE TABLE IF NOT EXISTS PizzaIngredientsAssociation (PizzaID VARCHAR(128) ,IngID VARCHAR(128)," + + "PRIMARY KEY(PizzaID, IngID)," + + "FOREIGN KEY (PizzaID) REFERENCES pizzas(id)," + + "FOREIGN KEY (IngID) REFERENCES ingredients(id))") + void createAssociationTable(); + + @Transaction + default void createTableAndIngredientAssociation() { + createPizzaTable(); + createAssociationTable(); + } + + @SqlUpdate("DROP TABLE IF EXISTS pizzas") + void dropPizzaTable(); + + @SqlUpdate("DROP TABLE IF EXISTS PizzaIngredientsAssociation") + void dropAssociationTable(); + + @Transaction + default void dropTableAndIngredientAssociation() { + dropAssociationTable(); + dropPizzaTable(); + } + + @SqlUpdate("INSERT INTO Pizzas (id, name) VALUES (:id, :name)") + void insert(@BindBean Pizza pizza); + + @SqlUpdate("DELETE FROM Pizzas WHERE id = :id") + void remove(@Bind("id") UUID id); + + @SqlQuery("SELECT * FROM Pizzas WHERE name = :name") + @RegisterBeanMapper(Pizza.class) + Pizza findByName(@Bind("name") String name); + + @SqlQuery("SELECT * FROM Pizzas") + @RegisterBeanMapper(Pizza.class) + List<Pizza> getAll(); + + @SqlQuery("SELECT * FROM Pizzas WHERE id = :id") + @RegisterBeanMapper(Pizza.class) + Pizza findById(@Bind("id") UUID id); +} diff --git a/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientCreateDto.java b/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientCreateDto.java new file mode 100644 index 0000000000000000000000000000000000000000..65d428ea6e73cf9b053011fc7b15f2cab28a94dd --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientCreateDto.java @@ -0,0 +1,16 @@ +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; + } +} + diff --git a/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientDto.java b/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientDto.java index 1a9360bf01fa2544b241a017cfa0c056dada02e2..6004ba0fa8e3672a28f19e8fdc5322a708ac2663 100644 --- a/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientDto.java +++ b/src/main/java/fr/ulille/iut/pizzaland/dto/IngredientDto.java @@ -1,8 +1,29 @@ package fr.ulille.iut.pizzaland.dto; +import java.util.UUID; + public class IngredientDto { + private UUID id; + private String name; + public IngredientDto() { - } + + public void setId(UUID id) { + this.id = id; + } + + public UUID getId() { + return id; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } + } diff --git a/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaCreateDto.java b/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaCreateDto.java new file mode 100644 index 0000000000000000000000000000000000000000..ecb6682934a4374497d0bb648079ee4abbda8392 --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaCreateDto.java @@ -0,0 +1,16 @@ +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; + } +} + diff --git a/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaDto.java b/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaDto.java new file mode 100644 index 0000000000000000000000000000000000000000..d05cdc83202942114718515c2eab11ca139b57cc --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/dto/PizzaDto.java @@ -0,0 +1,28 @@ +package fr.ulille.iut.pizzaland.dto; + +import java.util.UUID; + +public class PizzaDto { + + private UUID id; + private String name; + + public PizzaDto() { + } + + public void setId(UUID id) { + this.id = id; + } + + public UUID getId() { + return id; + } + + public void setName(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java b/src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java index 8121cada0c6bbb0ebcfb18cf8e0b8d4125e97233..35cd497d1bab27369a687159049012f98ecfa993 100644 --- a/src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java +++ b/src/main/java/fr/ulille/iut/pizzaland/resources/IngredientResource.java @@ -2,31 +2,137 @@ package fr.ulille.iut.pizzaland.resources; import java.net.URI; import java.util.List; -import java.util.ArrayList; +import java.util.UUID; import java.util.logging.Logger; +import java.util.stream.Collectors; +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.IngredientDto; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.DELETE; +import jakarta.ws.rs.FormParam; import jakarta.ws.rs.GET; import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.WebApplicationException; import jakarta.ws.rs.core.Context; import jakarta.ws.rs.core.Response; import jakarta.ws.rs.core.UriInfo; +import jakarta.ws.rs.POST; +import fr.ulille.iut.pizzaland.dto.IngredientCreateDto; -import fr.ulille.iut.pizzaland.dto.IngredientDto; +@Produces("application/json") @Path("/ingredients") public class IngredientResource { private static final Logger LOGGER = Logger.getLogger(IngredientResource.class.getName()); + private IngredientDao ingredients; + @Context public UriInfo uriInfo; public IngredientResource() { + ingredients = BDDFactory.buildDao(IngredientDao.class); + ingredients.createTable(); } @GET public List<IngredientDto> getAll() { LOGGER.info("IngredientResource:getAll"); - return new ArrayList<IngredientDto>(); + List<IngredientDto> l = ingredients.getAll().stream().map(Ingredient::toDto).collect(Collectors.toList()); + LOGGER.info(l.toString()); + return l; + } + + @GET + @Path("{id}") + @Produces({ "application/json", "application/xml" }) + public IngredientDto getOneIngredient(@PathParam("id") UUID id) { + LOGGER.info("getOneIngredient(" + id + ")"); + try { + Ingredient ingredient = ingredients.findById(id); + LOGGER.info(ingredient.toString()); + return Ingredient.toDto(ingredient); + } catch (Exception e) { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + } + + @POST + @Consumes("application/json") + 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); + ingredients.insert(ingredient); + IngredientDto ingredientDto = Ingredient.toDto(ingredient); + + URI uri = uriInfo.getAbsolutePathBuilder().path(ingredient.getId().toString()).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") UUID 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") UUID 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); + + ingredients.insert(ingredient); + + IngredientDto ingredientDto = Ingredient.toDto(ingredient); + + URI uri = uriInfo.getAbsolutePathBuilder().path("" + ingredient.getId()).build(); + + return Response.created(uri).entity(ingredientDto).build(); + } catch (Exception e) { + e.printStackTrace(); + throw new WebApplicationException(Response.Status.NOT_ACCEPTABLE); + } + } + } diff --git a/src/main/java/fr/ulille/iut/pizzaland/resources/PizzaResource.java b/src/main/java/fr/ulille/iut/pizzaland/resources/PizzaResource.java new file mode 100644 index 0000000000000000000000000000000000000000..51329c14c0072513647c17729dfe2f542ced38d9 --- /dev/null +++ b/src/main/java/fr/ulille/iut/pizzaland/resources/PizzaResource.java @@ -0,0 +1,143 @@ +package fr.ulille.iut.pizzaland.resources; + +import java.net.URI; +import java.util.List; +import java.util.UUID; +import java.util.logging.Logger; +import java.util.stream.Collectors; + +import fr.ulille.iut.pizzaland.BDDFactory; +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.PizzaDao; +import fr.ulille.iut.pizzaland.dto.IngredientDto; +import fr.ulille.iut.pizzaland.dto.PizzaCreateDto; +import fr.ulille.iut.pizzaland.dto.PizzaDto; +import jakarta.ws.rs.Consumes; +import jakarta.ws.rs.DELETE; +import jakarta.ws.rs.FormParam; +import jakarta.ws.rs.GET; +import jakarta.ws.rs.Path; +import jakarta.ws.rs.PathParam; +import jakarta.ws.rs.Produces; +import jakarta.ws.rs.WebApplicationException; +import jakarta.ws.rs.core.Context; +import jakarta.ws.rs.core.Response; +import jakarta.ws.rs.core.UriInfo; +import jakarta.ws.rs.POST; +import fr.ulille.iut.pizzaland.dto.IngredientCreateDto; + + +@Produces("application/json") +@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"); + + List<PizzaDto> l = pizzas.getAll().stream().map(Pizza::toDto).collect(Collectors.toList()); + + LOGGER.info(l.toString()); + return l; + } + + @GET + @Path("{id}") + @Produces({ "application/json", "application/xml" }) + public PizzaDto getOneIngredient(@PathParam("id") UUID id) { + LOGGER.info("getOneIngredient(" + id + ")"); + try { + Pizza pizza = pizzas.findById(id); + LOGGER.info(pizza.toString()); + return Pizza.toDto(pizza); + } catch (Exception e) { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + } + + @POST + @Consumes("application/json") + public Response createPizza(PizzaCreateDto pizzaCreateDto) { + Pizza existing = pizzas.findByName(pizzaCreateDto.getName()); + if (existing != null) { + throw new WebApplicationException(Response.Status.CONFLICT); + } + + try { + Pizza ingredient = Pizza.fromPizzaCreateDto(pizzaCreateDto); + pizzas.insert(ingredient); + PizzaDto ingredientDto = Pizza.toDto(ingredient); + + URI uri = uriInfo.getAbsolutePathBuilder().path(ingredient.getId().toString()).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 deletePizza(@PathParam("id") UUID id) { + if ( pizzas.findById(id) == null ) { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + + pizzas.remove(id); + + return Response.status(Response.Status.ACCEPTED).build(); + } + + @GET + @Path("{id}/name") + public String getPizzaName(@PathParam("id") UUID id) { + Pizza pizza = pizzas.findById(id); + + if (pizza == null) { + throw new WebApplicationException(Response.Status.NOT_FOUND); + } + + return pizza.getName(); + } + + @POST + @Consumes("application/x-www-form-urlencoded") + public Response createPizza(@FormParam("name") String name) { + Pizza existing = pizzas.findByName(name); + if (existing != null) { + throw new WebApplicationException(Response.Status.CONFLICT); + } + + try { + Pizza pizza = new Pizza(); + pizza.setName(name); + + pizzas.insert(pizza); + + PizzaDto pizzaDto = Pizza.toDto(pizza); + + URI uri = uriInfo.getAbsolutePathBuilder().path("" + pizza.getId()).build(); + + return Response.created(uri).entity(pizzaDto).build(); + } catch (Exception e) { + e.printStackTrace(); + throw new WebApplicationException(Response.Status.NOT_ACCEPTABLE); + } + } + +}