diff --git a/README.md b/README.md index 1c62bdef76d8d310cf9fc8a1b90cb8377233d8d8..c943b403cdc422c8d9cdff5ac910f43ed854884b 100644 --- a/README.md +++ b/README.md @@ -187,7 +187,7 @@ package fr.ulille.iut.pizzaland.dto; public class IngredientDto { - public IngredientDto() { + public IngredientDto() { } } ~~~ @@ -214,7 +214,6 @@ public class IngredientResource { return new ArrayList<IngredientDto>(); } } - ~~~ Avec cette première implémentation, on va pouvoir tester notre @@ -275,26 +274,26 @@ public class IngredientDto { } public void setName(String name) { - this.name = name; - } + this.name = name; + } - public String getName() { - return name; - } + public String getName() { + return name; + } ~~~ Du côté de la ressource, on peut fournir une première implémentation : ~~~java - import jakarta.ws.rs.PathParam; - import fr.ulille.iut.pizzaland.beans.Ingredient; +import jakarta.ws.rs.PathParam; +import fr.ulille.iut.pizzaland.beans.Ingredient; @GET @Path("{id}") public IngredientDto getOneIngredient(@PathParam("id") UUID id) { - Ingredient ingredient = new Ingredient(); - ingredient.setId(id); - ingredient.setName("Chorizo"); + Ingredient ingredient = new Ingredient(); + ingredient.setId(id); // juste pour avoir le même id pour le test + ingredient.setName("Chorizo"); return Ingredient.toDto(ingredient); } @@ -428,6 +427,7 @@ JDBI fonctionne par annotations : requêtes SQL en modification ou non. - `@RegisterBeanMapper` permet d'associer une classe à un résultat (les champs de la table sont associés aux propriétés du bean). + - `@Bind` permet d'associer un paramètre de méthode à un paramètre nommé dans la requête SQL. Reprenons maintenant le code déjà écrit pour aller chercher les ingrédients dans une base de données (nous utiliserons `Sqlite`). @@ -677,7 +677,7 @@ Comme nous vérifions qu'il n'y a pas déjà un ingrédient avec le nom fourni, nous devont ajouter une méthode `findbyName` à notre DAO ~~~java - @SqlQuery("SELECT * FROM ingredients WHERE name = :name") + @SqlQuery("SELECT * FROM ingredients WHERE name = :name") @RegisterBeanMapper(Ingredient.class) Ingredient findByName(@Bind("name") String name); ~~~ @@ -688,19 +688,19 @@ Nous avons également besoin de rajouter les méthodes de conversion ~~~java import fr.ulille.iut.pizzaland.dto.IngredientCreateDto; - public static IngredientCreateDto toCreateDto(Ingredient ingredient) { - IngredientCreateDto dto = new IngredientCreateDto(); - dto.setName(ingredient.getName()); + public static IngredientCreateDto toCreateDto(Ingredient ingredient) { + IngredientCreateDto dto = new IngredientCreateDto(); + dto.setName(ingredient.getName()); - return dto; - } + return dto; + } - public static Ingredient fromIngredientCreateDto(IngredientCreateDto dto) { - Ingredient ingredient = new Ingredient(); - ingredient.setName(dto.getName()); + public static Ingredient fromIngredientCreateDto(IngredientCreateDto dto) { + Ingredient ingredient = new Ingredient(); + ingredient.setName(dto.getName()); - return ingredient; - } + return ingredient; + } ~~~ Nous pouvons maintenant vérifier nos tests : @@ -722,24 +722,24 @@ Les tests liés à la méthode DELETE sont les suivants : ~~~java @Test - public void testDeleteExistingIngredient() { - Ingredient ingredient = new Ingredient(); - ingredient.setName("Chorizo"); - dao.insert(ingredient); + public void testDeleteExistingIngredient() { + Ingredient ingredient = new Ingredient(); + ingredient.setName("Chorizo"); + dao.insert(ingredient); - Response response = target("/ingredients/").path(ingredient.getId().toString()).request().delete(); + Response response = target("/ingredients/").path(ingredient.getId().toString()).request().delete(); - assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus()); + assertEquals(Response.Status.ACCEPTED.getStatusCode(), response.getStatus()); - Ingredient result = dao.findById(ingredient.getId()); - assertEquals(result, null); - } + Ingredient result = dao.findById(ingredient.getId()); + assertEquals(result, null); + } - @Test - public void testDeleteNotExistingIngredient() { - Response response = target("/ingredients").path(UUID.randomUUID().toString()).request().delete(); - assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); - } + @Test + public void testDeleteNotExistingIngredient() { + Response response = target("/ingredients").path(UUID.randomUUID().toString()).request().delete(); + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); + } ~~~ Après avoir constaté que ces tests échouent, nous pouvons fournir une @@ -748,17 +748,17 @@ implémentation pour la méthode DELETE : ~~~java import jakarta.ws.rs.DELETE; - @DELETE - @Path("{id}") - public Response deleteIngredient(@PathParam("id") UUID id) { - if ( ingredients.findById(id) == null ) { - throw new WebApplicationException(Response.Status.NOT_FOUND); - } + @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); + ingredients.remove(id); - return Response.status(Response.Status.ACCEPTED).build(); - } + return Response.status(Response.Status.ACCEPTED).build(); + } ~~~ Nous devons également implémenter la méthode remove dans @@ -766,7 +766,7 @@ Nous devons également implémenter la méthode remove dans ~~~java @SqlUpdate("DELETE FROM ingredients WHERE id = :id") - void remove(@Bind("id") UUID id); + void remove(@Bind("id") UUID id); ~~~ Avec cette implémentation, nos tests réussissent. @@ -777,24 +777,24 @@ Commençons par les tests correspondant à cette URI (GET ~~~java @Test - public void testGetIngredientName() { - Ingredient ingredient = new Ingredient(); - ingredient.setName("Chorizo"); - dao.insert(ingredient); + public void testGetIngredientName() { + Ingredient ingredient = new Ingredient(); + ingredient.setName("Chorizo"); + dao.insert(ingredient); - Response response = target("ingredients").path(ingredient.getId().toString()).path("name").request().get(); + Response response = target("ingredients").path(ingredient.getId().toString()).path("name").request().get(); - assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); + assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); - assertEquals("Chorizo", response.readEntity(String.class)); - } + assertEquals("Chorizo", response.readEntity(String.class)); + } - @Test - public void testGetNotExistingIngredientName() { - Response response = target("ingredients").path(UUID.randomUUID().toString()).path("name").request().get(); + @Test + public void testGetNotExistingIngredientName() { + Response response = target("ingredients").path(UUID.randomUUID().toString()).path("name").request().get(); - assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); - } + assertEquals(Response.Status.NOT_FOUND.getStatusCode(), response.getStatus()); + } ~~~ L'implémentation correspondant à ce test est simple : @@ -841,30 +841,30 @@ On peut déjà préparer un test pour cette méthode de création : On peut maintenant fournir une implémentation pour cette méthode : ~~~java - @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); - } + @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); + try { + Ingredient ingredient = new Ingredient(); + ingredient.setName(name); - ingredients.insert(ingredient); + ingredients.insert(ingredient); - IngredientDto ingredientDto = Ingredient.toDto(ingredient); + IngredientDto ingredientDto = Ingredient.toDto(ingredient); - URI uri = uriInfo.getAbsolutePathBuilder().path("" + ingredient.getId()).build(); + 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); - } + return Response.created(uri).entity(ingredientDto).build(); + } catch (Exception e) { + e.printStackTrace(); + throw new WebApplicationException(Response.Status.NOT_ACCEPTABLE); } + } ~~~ # Créer une base de données de test @@ -947,6 +947,9 @@ Content-Length: 760 ~~~ # Implémentation de la ressource Pizza +Maintenant que vous avez une ressource `ingrédients` fonctionnelle, vous pouvez passer à l'implémentation de la ressource `Pizzas`. Pour cette ressource, vous devrez d'abord définir l'API dans le fichier `pizzas.md` (URI, méthodes, représentations). Ensuite, vous pourrez développer la ressource avec les tests associés. + +## Note sur la base de données Une pizza comprend des ingrédients. Pour développer cette ressource, vous aurez donc besoin d'un table d'association au niveau de la base de données. Cela pourra être géré au niveau du DAO grâce à @@ -954,20 +957,20 @@ de données. Cela pourra être géré au niveau du DAO grâce à comment faire : ~~~java - import org.jdbi.v3.sqlobject.transaction.Transaction; +import org.jdbi.v3.sqlobject.transaction.Transaction; - public interface PizzaDao { + public interface PizzaDao { - @SqlUpdate("CREATE TABLE IF NOT EXISTS Pizzas ....") - void createPizzaTable(); + @SqlUpdate("CREATE TABLE IF NOT EXISTS Pizzas ....") + void createPizzaTable(); - @SqlUpdate("CREATE TABLE IF NOT EXISTS PizzaIngredientsAssociation .....") - void createAssociationTable(); + @SqlUpdate("CREATE TABLE IF NOT EXISTS PizzaIngredientsAssociation .....") + void createAssociationTable(); - @Transaction - default void createTableAndIngredientAssociation() { - createAssociationTable(); - createPizzaTable(); + @Transaction + default void createTableAndIngredientAssociation() { + createAssociationTable(); + createPizzaTable(); } ~~~