diff --git a/META-INF/context.xml b/META-INF/context.xml new file mode 100755 index 0000000000000000000000000000000000000000..195700d26cccd7914635d39f6c211a858edd9551 --- /dev/null +++ b/META-INF/context.xml @@ -0,0 +1 @@ +<Context reloadable="true" /> diff --git a/WEB-INF/bruno/pizzeria/1_ingredient/DeleteIngredient.bru b/WEB-INF/bruno/pizzeria/1_ingredient/DeleteIngredient.bru new file mode 100644 index 0000000000000000000000000000000000000000..e43cc0ffa25a6e626749943776da4eac81b21d70 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/1_ingredient/DeleteIngredient.bru @@ -0,0 +1,15 @@ +meta { + name: DeleteIngredient + type: http + seq: 7 +} + +delete { + url: http://localhost:8080/pizzeria/ingredients/0 + body: none + auth: none +} + +docs { + Avant de faire ce DELETE vou de vais faire la requête "IngrediantPost" +} diff --git a/WEB-INF/bruno/pizzeria/1_ingredient/FindIngrediantByID.bru b/WEB-INF/bruno/pizzeria/1_ingredient/FindIngrediantByID.bru new file mode 100644 index 0000000000000000000000000000000000000000..f3148a3fef218291b52de532112f9e56e8d6133b --- /dev/null +++ b/WEB-INF/bruno/pizzeria/1_ingredient/FindIngrediantByID.bru @@ -0,0 +1,11 @@ +meta { + name: FindIngrediantByID + type: http + seq: 2 +} + +get { + url: http://localhost:8080/pizzeria/ingredients/2 + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/1_ingredient/IngrediantPatch price.bru b/WEB-INF/bruno/pizzeria/1_ingredient/IngrediantPatch price.bru new file mode 100644 index 0000000000000000000000000000000000000000..08a16123940c5b9dc04a6d22c13143b5e4374ef1 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/1_ingredient/IngrediantPatch price.bru @@ -0,0 +1,17 @@ +meta { + name: IngrediantPatch price + type: http + seq: 6 +} + +patch { + url: http://localhost:8080/pizzeria/ingredients/0 + body: json + auth: none +} + +body:json { + { + "price":10.5 + } +} diff --git a/WEB-INF/bruno/pizzeria/1_ingredient/IngrediantPost.bru b/WEB-INF/bruno/pizzeria/1_ingredient/IngrediantPost.bru new file mode 100644 index 0000000000000000000000000000000000000000..e1f2213670a3a9678818ad98ca692d87c7280ca3 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/1_ingredient/IngrediantPost.bru @@ -0,0 +1,19 @@ +meta { + name: IngrediantPost + type: http + seq: 4 +} + +post { + url: http://localhost:8080/pizzeria/ingredients + body: json + auth: none +} + +body:json { + { + "id":0 , + "name":"poison" , + "price":0 + } +} diff --git a/WEB-INF/bruno/pizzeria/1_ingredient/Ingredient Patch name.bru b/WEB-INF/bruno/pizzeria/1_ingredient/Ingredient Patch name.bru new file mode 100644 index 0000000000000000000000000000000000000000..753735e8f7abcd22624191ed11275ec23e47abc9 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/1_ingredient/Ingredient Patch name.bru @@ -0,0 +1,17 @@ +meta { + name: Ingredient Patch name + type: http + seq: 5 +} + +patch { + url: http://localhost:8080/pizzeria/ingredients/0 + body: json + auth: none +} + +body:json { + { + "name":"poisson" + } +} diff --git a/WEB-INF/bruno/pizzeria/1_ingredient/IngretiantName.bru b/WEB-INF/bruno/pizzeria/1_ingredient/IngretiantName.bru new file mode 100644 index 0000000000000000000000000000000000000000..297ac658ec02269dc52ad3445f2f1ce4afe7f982 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/1_ingredient/IngretiantName.bru @@ -0,0 +1,11 @@ +meta { + name: IngretiantName + type: http + seq: 3 +} + +get { + url: http://localhost:8080/pizzeria/ingredients/2/name + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/1_ingredient/findall.bru b/WEB-INF/bruno/pizzeria/1_ingredient/findall.bru new file mode 100644 index 0000000000000000000000000000000000000000..a21f00c93543f295f1368a7c870fc661d9817267 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/1_ingredient/findall.bru @@ -0,0 +1,11 @@ +meta { + name: findall + type: http + seq: 1 +} + +get { + url: http://localhost:8080/pizzeria/ingredients + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/2_ingrediantError/FailDelete.bru b/WEB-INF/bruno/pizzeria/2_ingrediantError/FailDelete.bru new file mode 100644 index 0000000000000000000000000000000000000000..abb421b7f57ed5ee9593cf6c3817976a86c4e792 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/2_ingrediantError/FailDelete.bru @@ -0,0 +1,15 @@ +meta { + name: FailDelete + type: http + seq: 5 +} + +delete { + url: http://localhost:8080/pizzeria/ingredients/0 + body: none + auth: none +} + +assert { + res.status: eq 404 +} diff --git a/WEB-INF/bruno/pizzeria/2_ingrediantError/FindByIDFail.bru b/WEB-INF/bruno/pizzeria/2_ingrediantError/FindByIDFail.bru new file mode 100644 index 0000000000000000000000000000000000000000..5776f8fe833c4d84b8901489b77243febf6ff05b --- /dev/null +++ b/WEB-INF/bruno/pizzeria/2_ingrediantError/FindByIDFail.bru @@ -0,0 +1,15 @@ +meta { + name: FindByIDFail + type: http + seq: 1 +} + +get { + url: http://localhost:8080/pizzeria/ingredients/0 + body: none + auth: none +} + +assert { + res.status: eq 404 +} diff --git a/WEB-INF/bruno/pizzeria/2_ingrediantError/FindByIDNameFail.bru b/WEB-INF/bruno/pizzeria/2_ingrediantError/FindByIDNameFail.bru new file mode 100644 index 0000000000000000000000000000000000000000..ebb72f0fadaf1a6417f903d80b2567ad757fd237 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/2_ingrediantError/FindByIDNameFail.bru @@ -0,0 +1,19 @@ +meta { + name: FindByIDNameFail + type: http + seq: 6 +} + +get { + url: http://localhost:8080/pizzeria/ingredients/0/name + body: none + auth: none +} + +query { + : +} + +assert { + res.status: eq 404 +} diff --git a/WEB-INF/bruno/pizzeria/2_ingrediantError/IngrediantPostFail.bru b/WEB-INF/bruno/pizzeria/2_ingrediantError/IngrediantPostFail.bru new file mode 100644 index 0000000000000000000000000000000000000000..7b4f63fdc0f6eb100d8c8db0ae11ac3b6e3d58a0 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/2_ingrediantError/IngrediantPostFail.bru @@ -0,0 +1,23 @@ +meta { + name: IngrediantPostFail + type: http + seq: 2 +} + +post { + url: http://localhost:8080/pizzeria/ingredients + body: json + auth: none +} + +body:json { + { + "id":2 , + "name":"fromage" , + "price":10 + } +} + +assert { + res.status: eq 409 +} diff --git a/WEB-INF/bruno/pizzeria/2_ingrediantError/Ingredient Patch fail (not foud ingredient).bru b/WEB-INF/bruno/pizzeria/2_ingrediantError/Ingredient Patch fail (not foud ingredient).bru new file mode 100644 index 0000000000000000000000000000000000000000..85f2abd5d558377545d367eb3efb27f58fbe6d7a --- /dev/null +++ b/WEB-INF/bruno/pizzeria/2_ingrediantError/Ingredient Patch fail (not foud ingredient).bru @@ -0,0 +1,21 @@ +meta { + name: Ingredient Patch fail (not foud ingredient) + type: http + seq: 4 +} + +patch { + url: http://localhost:8080/pizzeria/ingredients/0 + body: json + auth: none +} + +body:json { + { + "name":"poisson" + } +} + +assert { + res.status: eq 404 +} diff --git a/WEB-INF/bruno/pizzeria/2_ingrediantError/Ingredient Patch name fail.bru b/WEB-INF/bruno/pizzeria/2_ingrediantError/Ingredient Patch name fail.bru new file mode 100644 index 0000000000000000000000000000000000000000..6b8c20d5903d42497c4d85c57a9530936e5146ce --- /dev/null +++ b/WEB-INF/bruno/pizzeria/2_ingrediantError/Ingredient Patch name fail.bru @@ -0,0 +1,21 @@ +meta { + name: Ingredient Patch name fail + type: http + seq: 3 +} + +patch { + url: http://localhost:8080/pizzeria/ingredients/1 + body: json + auth: none +} + +body:json { + { + "nom":"poisson" + } +} + +assert { + res.status: eq 500 +} diff --git a/WEB-INF/bruno/pizzeria/3_Pizza/Pizza Delete (une pizza).bru b/WEB-INF/bruno/pizzeria/3_Pizza/Pizza Delete (une pizza).bru new file mode 100644 index 0000000000000000000000000000000000000000..651c68955725cc1f376130576044dd8761455eb9 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/3_Pizza/Pizza Delete (une pizza).bru @@ -0,0 +1,11 @@ +meta { + name: Pizza Delete (une pizza) + type: http + seq: 6 +} + +delete { + url: http://localhost:8080/pizzeria/pizzas/chorizo + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/3_Pizza/PizzaByName.bru b/WEB-INF/bruno/pizzeria/3_Pizza/PizzaByName.bru new file mode 100644 index 0000000000000000000000000000000000000000..baa5588c22dae0f309520d6597e30e374ff03388 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/3_Pizza/PizzaByName.bru @@ -0,0 +1,11 @@ +meta { + name: PizzaByName + type: http + seq: 2 +} + +get { + url: http://localhost:8080/pizzeria/pizzas/3fromages + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/3_Pizza/PizzaDeleteIngredient (un ingredient d'une pizza).bru b/WEB-INF/bruno/pizzeria/3_Pizza/PizzaDeleteIngredient (un ingredient d'une pizza).bru new file mode 100644 index 0000000000000000000000000000000000000000..b55a0e44633ce64e21bdce2d14a07ff64b389564 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/3_Pizza/PizzaDeleteIngredient (un ingredient d'une pizza).bru @@ -0,0 +1,11 @@ +meta { + name: PizzaDeleteIngredient (un ingredient d'une pizza) + type: http + seq: 5 +} + +delete { + url: http://localhost:8080/pizzeria/pizzas/chorizo/7 + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/3_Pizza/PizzaPost.bru b/WEB-INF/bruno/pizzeria/3_Pizza/PizzaPost.bru new file mode 100644 index 0000000000000000000000000000000000000000..a31e7610b2b24b757e60274291ebdfea778af0c7 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/3_Pizza/PizzaPost.bru @@ -0,0 +1,31 @@ +meta { + name: PizzaPost + type: http + seq: 4 +} + +post { + url: http://localhost:8080/pizzeria/pizzas + body: json + auth: none +} + +body:json { + { + "nom" : "chorizo", + "pate" :"classique", + "prixBase" : 4, + "ingredients":[ + { + "id":7 , + "name":"sauce tomate" , + "price":0.50 + }, + {"id":1 , + "name":"pomme de terre" , + "price":1 + } + ] + } + +} diff --git a/WEB-INF/bruno/pizzeria/3_Pizza/PizzaPrixtotal.bru b/WEB-INF/bruno/pizzeria/3_Pizza/PizzaPrixtotal.bru new file mode 100644 index 0000000000000000000000000000000000000000..96a90a3842db8adb6f1f4ae945b8e033f5945235 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/3_Pizza/PizzaPrixtotal.bru @@ -0,0 +1,11 @@ +meta { + name: PizzaPrixtotal + type: http + seq: 3 +} + +get { + url: http://localhost:8080/pizzeria/pizzas/3fromages/prixfinal + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/3_Pizza/pizzaALL.bru b/WEB-INF/bruno/pizzeria/3_Pizza/pizzaALL.bru new file mode 100644 index 0000000000000000000000000000000000000000..ce2ec088fcd3e8385c8492605c0fb4ac83d294b7 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/3_Pizza/pizzaALL.bru @@ -0,0 +1,11 @@ +meta { + name: pizzaALL + type: http + seq: 1 +} + +get { + url: http://localhost:8080/pizzeria/pizzas + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/5_Commande/Commande Delete (une commande).bru b/WEB-INF/bruno/pizzeria/5_Commande/Commande Delete (une commande).bru new file mode 100644 index 0000000000000000000000000000000000000000..96f585faafeb92ba6a1aeeec8724f9ed2c5f563c --- /dev/null +++ b/WEB-INF/bruno/pizzeria/5_Commande/Commande Delete (une commande).bru @@ -0,0 +1,11 @@ +meta { + name: Commande Delete (une commande) + type: http + seq: 5 +} + +delete { + url: http://localhost:8080/pizzeria/commandes/4/3fromages + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/5_Commande/Commande Delete (une pizza).bru b/WEB-INF/bruno/pizzeria/5_Commande/Commande Delete (une pizza).bru new file mode 100644 index 0000000000000000000000000000000000000000..b625b595c742b88630be3727f57d63a929a8f396 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/5_Commande/Commande Delete (une pizza).bru @@ -0,0 +1,11 @@ +meta { + name: Commande Delete (une pizza) + type: http + seq: 4 +} + +delete { + url: http://localhost:8080/pizzeria/commandes/4 + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/5_Commande/CommandePost.bru b/WEB-INF/bruno/pizzeria/5_Commande/CommandePost.bru new file mode 100644 index 0000000000000000000000000000000000000000..effed991995bd308b0cc1fb2870d6cba247fa0e2 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/5_Commande/CommandePost.bru @@ -0,0 +1,27 @@ +meta { + name: CommandePost + type: http + seq: 3 +} + +post { + url: http://localhost:8080/pizzeria/commandes + body: json + auth: none +} + +body:json { + { + "id": 4, + "nom": "truc bidule", + "date": 1711922400000, + "pizzas": [ + { + "nom": "6fromages" + }, + { + "nom": "3fromages" + } + ] + } +} diff --git a/WEB-INF/bruno/pizzeria/5_Commande/Commandes fidAll.bru b/WEB-INF/bruno/pizzeria/5_Commande/Commandes fidAll.bru new file mode 100644 index 0000000000000000000000000000000000000000..3a2115c0d38ea1c6467f7b384e0e8b1670dbf99e --- /dev/null +++ b/WEB-INF/bruno/pizzeria/5_Commande/Commandes fidAll.bru @@ -0,0 +1,11 @@ +meta { + name: Commandes fidAll + type: http + seq: 1 +} + +get { + url: http://localhost:8080/pizzeria/commandes + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/5_Commande/Commandes findById.bru b/WEB-INF/bruno/pizzeria/5_Commande/Commandes findById.bru new file mode 100644 index 0000000000000000000000000000000000000000..723c18ae3e1fba1ef6c03c7076da29f358e2408b --- /dev/null +++ b/WEB-INF/bruno/pizzeria/5_Commande/Commandes findById.bru @@ -0,0 +1,11 @@ +meta { + name: Commandes findById + type: http + seq: 2 +} + +get { + url: http://localhost:8080/pizzeria/commandes/1 + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/5_Commande/Commandes totalPrice.bru b/WEB-INF/bruno/pizzeria/5_Commande/Commandes totalPrice.bru new file mode 100644 index 0000000000000000000000000000000000000000..1efca787b2f49831022e69bb3de6f46d4b82d1fa --- /dev/null +++ b/WEB-INF/bruno/pizzeria/5_Commande/Commandes totalPrice.bru @@ -0,0 +1,11 @@ +meta { + name: Commandes totalPrice + type: http + seq: 6 +} + +get { + url: http://localhost:8080/pizzeria/commandes/1/prixfinal + body: none + auth: none +} diff --git a/WEB-INF/bruno/pizzeria/bruno.json b/WEB-INF/bruno/pizzeria/bruno.json new file mode 100644 index 0000000000000000000000000000000000000000..c1538f7f960c8a2180af8cae9635fdce1985e789 --- /dev/null +++ b/WEB-INF/bruno/pizzeria/bruno.json @@ -0,0 +1,9 @@ +{ + "version": "1", + "name": "pizzeria", + "type": "collection", + "ignore": [ + "node_modules", + ".git" + ] +} \ No newline at end of file diff --git a/WEB-INF/classes/controleurs/CommandeRestAPI.class b/WEB-INF/classes/controleurs/CommandeRestAPI.class new file mode 100644 index 0000000000000000000000000000000000000000..04276ec9131778ccc102a3960304d92762e5adc6 Binary files /dev/null and b/WEB-INF/classes/controleurs/CommandeRestAPI.class differ diff --git a/WEB-INF/classes/controleurs/IngredientRestAPI.class b/WEB-INF/classes/controleurs/IngredientRestAPI.class new file mode 100644 index 0000000000000000000000000000000000000000..8f19fdae6e2690d550afc107a97dc4403156e46c Binary files /dev/null and b/WEB-INF/classes/controleurs/IngredientRestAPI.class differ diff --git a/WEB-INF/classes/controleurs/PizzasRestAPI.class b/WEB-INF/classes/controleurs/PizzasRestAPI.class new file mode 100644 index 0000000000000000000000000000000000000000..4be926579fa3acd9f6aa791e484994c348ae879e Binary files /dev/null and b/WEB-INF/classes/controleurs/PizzasRestAPI.class differ diff --git a/WEB-INF/classes/dao/CommandeDAO.class b/WEB-INF/classes/dao/CommandeDAO.class new file mode 100644 index 0000000000000000000000000000000000000000..63a4a0686cf2de0a30e4ab095ec70bf6c675117c Binary files /dev/null and b/WEB-INF/classes/dao/CommandeDAO.class differ diff --git a/WEB-INF/classes/dao/IngredientDAODatabase.class b/WEB-INF/classes/dao/IngredientDAODatabase.class new file mode 100644 index 0000000000000000000000000000000000000000..a2ed957200e05d1bad87951fbb6f9ad621d8cd88 Binary files /dev/null and b/WEB-INF/classes/dao/IngredientDAODatabase.class differ diff --git a/WEB-INF/classes/dao/PizzasDAO.class b/WEB-INF/classes/dao/PizzasDAO.class new file mode 100644 index 0000000000000000000000000000000000000000..91b0c032c9d8b5010e1c80e45ae4696067a276c3 Binary files /dev/null and b/WEB-INF/classes/dao/PizzasDAO.class differ diff --git a/WEB-INF/classes/dto/Commande.class b/WEB-INF/classes/dto/Commande.class new file mode 100644 index 0000000000000000000000000000000000000000..cfcc9a6b601dd06e02f7629dd6209c528fb2cd9a Binary files /dev/null and b/WEB-INF/classes/dto/Commande.class differ diff --git a/WEB-INF/classes/dto/CommandeDetail.class b/WEB-INF/classes/dto/CommandeDetail.class new file mode 100644 index 0000000000000000000000000000000000000000..d96f18c44e99303a239e20149f94a463faf551d3 Binary files /dev/null and b/WEB-INF/classes/dto/CommandeDetail.class differ diff --git a/WEB-INF/classes/dto/Ingredient.class b/WEB-INF/classes/dto/Ingredient.class new file mode 100644 index 0000000000000000000000000000000000000000..050f8be35caeb0088876032ebb37f7702de283ee Binary files /dev/null and b/WEB-INF/classes/dto/Ingredient.class differ diff --git a/WEB-INF/classes/dto/Pizza.class b/WEB-INF/classes/dto/Pizza.class new file mode 100644 index 0000000000000000000000000000000000000000..a1f5b011e24638075ca598a131ae182ea764859f Binary files /dev/null and b/WEB-INF/classes/dto/Pizza.class differ diff --git a/WEB-INF/classes/dto/PizzaCompo.class b/WEB-INF/classes/dto/PizzaCompo.class new file mode 100644 index 0000000000000000000000000000000000000000..812d177e3fdbe0aba470dd63198d8ec2b5dd78a3 Binary files /dev/null and b/WEB-INF/classes/dto/PizzaCompo.class differ diff --git a/WEB-INF/classes/utils/DS.class b/WEB-INF/classes/utils/DS.class new file mode 100644 index 0000000000000000000000000000000000000000..b98c3d89fa93baa8f49acb9ee1b7a3e0a212ee05 Binary files /dev/null and b/WEB-INF/classes/utils/DS.class differ diff --git a/WEB-INF/src/controleurs/CommandeRestAPI.java b/WEB-INF/src/controleurs/CommandeRestAPI.java new file mode 100644 index 0000000000000000000000000000000000000000..e4878b60e8824dd9509a595d2ebd6896a79538e3 --- /dev/null +++ b/WEB-INF/src/controleurs/CommandeRestAPI.java @@ -0,0 +1,86 @@ +package controleurs; + +import java.io.PrintWriter; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import dao.CommandeDAO; +import dto.Commande; +import dto.CommandeDetail; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.*; +import java.io.IOException; + + +@WebServlet("/commandes/*") +public class CommandeRestAPI extends HttpServlet{ + + private CommandeDAO commandeDAO = new CommandeDAO(); + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("application/json; charset=UTF-8"); + String pathInfo = request.getPathInfo(); + PrintWriter out = response.getWriter(); + + if (pathInfo != null) { + String[] pathParts = pathInfo.split("/"); + // si /commandes/{id}/prixtotal alors on renvoie le prix total de la commande {id} + if (pathParts.length > 2 && pathParts[2].equals("prixfinal")) { + int id = Integer.parseInt(pathParts[1]); + Commande commande = commandeDAO.findById(id); + if (commande == null) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } else { + out.print(commandeDAO.totalPrice(id)); + } + } + // si /commandes/{id} alors on renvoie la commande {id} en JSON + else if (pathParts.length > 1) { + int id = Integer.parseInt(pathParts[1]); + Commande commande = commandeDAO.findById(id); + if (commande == null) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } else { + ObjectMapper mapper = new ObjectMapper(); + out.print(mapper.writeValueAsString(commande)); + } + } + } + // si /commandes alors on renvoie la liste des commandes en JSON + else { + ObjectMapper mapper = new ObjectMapper(); + out.println(mapper.writeValueAsString(commandeDAO.findAll())); + } + } + + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("application/json; charset=UTF-8"); + PrintWriter out = response.getWriter(); + ObjectMapper mapper = new ObjectMapper(); + CommandeDetail commande = mapper.readValue(request.getReader(), CommandeDetail.class); + commandeDAO.save(commande); + out.println(mapper.writeValueAsString(commande)); + } + + public void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("application/json; charset=UTF-8"); + String pathInfo = request.getPathInfo(); + PrintWriter out = response.getWriter(); + + // si /commandes/{id}/{pizza} alors on supprime la pizza {pizza} de la commande {id} + if (pathInfo != null) { + String[] pathParts = pathInfo.split("/"); + if (pathParts.length > 2) { + int id = Integer.parseInt(pathParts[1]); + String pizza = pathParts[2]; + commandeDAO.deleteCommandeDetail(id, pizza); + } + if (pathParts.length > 1) { + int id = Integer.parseInt(pathParts[1]); + commandeDAO.deleteCommande(id); + } + } + } + +} diff --git a/WEB-INF/src/controleurs/IngredientRestAPI.java b/WEB-INF/src/controleurs/IngredientRestAPI.java new file mode 100644 index 0000000000000000000000000000000000000000..b86f8ad5557492f307505cdff6e739dd8c3822d1 --- /dev/null +++ b/WEB-INF/src/controleurs/IngredientRestAPI.java @@ -0,0 +1,175 @@ +package controleurs; + +import java.io.*; + +import com.fasterxml.jackson.databind.ObjectMapper; + + +import dao.IngredientDAODatabase; +import dto.Ingredient; +import jakarta.servlet.ServletException; +import jakarta.servlet.annotation.WebServlet; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + + +/*Créez une servlet contrôleur IngredientRestAPI.java. Dotez cette classe d’un endpoint /ingrédients qui +renvoie la collection d’ingrédients en JSON. */ + +@WebServlet("/ingredients/*") +public class IngredientRestAPI extends HttpServlet{ + + private IngredientDAODatabase ingredientDAOList = new IngredientDAODatabase(); + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("application/json; charset=UTF-8"); + String pathInfo = request.getPathInfo(); + PrintWriter out = response.getWriter(); + + if (pathInfo != null) { + String[] pathParts = pathInfo.split("/"); + // si /ingrédients/{id}/name alors on renvoie le nom de l'ingrédient {id} + if (pathParts.length > 2 && pathParts[2].equals("name")) { + int id = Integer.parseInt(pathParts[1]); + Ingredient ingredient = ingredientDAOList.findById(id); + if (ingredient == null) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } else { + out.print(ingredient.getName()); + } + } + // si /ingrédients/{id} alors on renvoie l'ingrédient {id} en JSON + else if (pathParts.length > 1) { + int id = Integer.parseInt(pathParts[1]); + Ingredient ingredient = ingredientDAOList.findById(id); + if (ingredient == null) { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } else { + ObjectMapper mapper = new ObjectMapper(); + out.print(mapper.writeValueAsString(ingredient)); + + } + } + } + // si /ingrédients alors on renvoie la liste des ingrédients en JSON + else { + ObjectMapper mapper = new ObjectMapper(); + out.println(mapper.writeValueAsString(ingredientDAOList.findAll())); + } + } + + public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("application/json; charset=UTF-8"); + PrintWriter out = response.getWriter(); + //récupère le corps de la requête + BufferedReader reader = request.getReader(); + String body = reader.readLine(); + ObjectMapper mapper = new ObjectMapper(); + Ingredient ingredient = mapper.readValue(body, Ingredient.class); + // si l'ingrédient existe déjà + if (ingredientDAOList.findById(ingredient.getId()) != null) { + //renvoie une erreur 409 + response.sendError(HttpServletResponse.SC_CONFLICT); + } else { + //sinon on l'ajoute + ingredientDAOList.save(ingredient); + //et renvoi un message is created + response.setStatus(HttpServletResponse.SC_CREATED); + out.println(mapper.writeValueAsString(ingredient)); + } + } + + public void doDelete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("application/json; charset=UTF-8"); + + PrintWriter out = response.getWriter(); + // si /ingrédients/1 alors on supprime l'ingrédient 1 + String pathInfo = request.getPathInfo(); + if (pathInfo != null) { + String[] pathParts = pathInfo.split("/"); + if (pathParts.length > 1) { + int id = Integer.parseInt(pathParts[1]); + // regarde si l'ingrédient existe + if (ingredientDAOList.findById(id) == null) { + //renvoie une erreur 404 + response.sendError(HttpServletResponse.SC_NOT_FOUND); + + out.println("L'ingrédient n'existe pas"); + + } else { + //sinon on le supprime + ingredientDAOList.delete(id); + //et renvoi un message is le json de l'ingrédient supprimé + + out.println("L'ingrédient a bien été supprimé"); + + } + } + } + } + + public void doPut(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("application/json; charset=UTF-8"); + PrintWriter out = response.getWriter(); + //récupère le corps de la requête + BufferedReader reader = request.getReader(); + String body = reader.readLine(); + ObjectMapper mapper = new ObjectMapper(); + Ingredient ingredient = mapper.readValue(body, Ingredient.class); + // si l'ingrédient n'existe pas + if (ingredientDAOList.findById(ingredient.getId()) == null) { + //renvoie une erreur 404 + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } else { + //sinon on le met à jour + ingredientDAOList.update(ingredient); + //et renvoi un message is updated + response.setStatus(HttpServletResponse.SC_OK); + out.println(mapper.writeValueAsString(ingredient)); + } + } + + + // fait un update sur un ingrédient existant en modifiant la colonne fournie dans le body de la requête au format JSON + public void doPatch(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + response.setContentType("application/json; charset=UTF-8"); + //récupère le corps de la requête + BufferedReader reader = request.getReader(); + String body = reader.readLine(); + ObjectMapper mapper = new ObjectMapper(); + Ingredient ingredient = mapper.readValue(body, Ingredient.class); + // récupère l'id de l'ingrédient à modifier + String pathInfo = request.getPathInfo(); + String[] pathParts = pathInfo.split("/"); + int id = Integer.parseInt(pathParts[1]); + // si l'ingrédient n'existe pas + if (ingredientDAOList.findById(id) == null){ + //renvoie une erreur 404 + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } else { + //sinon on le met à jour + ingredientDAOList.patch(id,ingredient); + //et renvoi un message is updated + response.setStatus(HttpServletResponse.SC_OK); + doGet(request, response); + } + } + + // faire une méthode service qui redirige vers les bonnes méthodes en fonction de la méthode de la requête + public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + if (request.getMethod().equals("GET")) { + doGet(request, response); + } else if (request.getMethod().equals("POST")) { + doPost(request, response); + } else if (request.getMethod().equals("DELETE")) { + doDelete(request, response); + } else if (request.getMethod().equals("UPDATE")) { + doPut(request, response); + } else if (request.getMethod().equals("PATCH")) { + doPatch(request, response); + } + } +} + + diff --git a/WEB-INF/src/controleurs/PizzasRestAPI.java b/WEB-INF/src/controleurs/PizzasRestAPI.java new file mode 100644 index 0000000000000000000000000000000000000000..96585b93eaa6b28a208d10c3526dfdab0b48ba16 --- /dev/null +++ b/WEB-INF/src/controleurs/PizzasRestAPI.java @@ -0,0 +1,99 @@ +package controleurs; + +import jakarta.servlet.annotation.WebServlet; + +import java.io.IOException; +import java.io.PrintWriter; +import java.util.List; + +import com.fasterxml.jackson.databind.ObjectMapper; + +import dao.PizzasDAO; +import dto.Pizza; +import dto.PizzaCompo; +import jakarta.servlet.http.HttpServlet; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; + +@WebServlet("/pizzas/*") +public class PizzasRestAPI extends HttpServlet{ + private PizzasDAO pizzasDAO = new PizzasDAO(); + + public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setContentType("application/json; charset=UTF-8"); + String pathInfo = request.getPathInfo(); + PrintWriter out = response.getWriter(); + ObjectMapper mapper = new ObjectMapper(); + if (pathInfo != null) { + String[] pathParts = pathInfo.split("/"); + if (pathParts.length > 2 && pathParts[2].equals("prixfinal")) { + String nom = pathParts[1]; + if (pizzasDAO.findByName(nom) != null){ + PizzaCompo pizza = pizzasDAO.findByName(nom); + out.print(pizzasDAO.totalPrice(nom)); + + } else { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + out.println("La pizza n'existe pas"); + } + } + else if (pathParts.length > 1) { + String nom = pathInfo.substring(1); + if (pizzasDAO.findByName(nom) != null){ + Pizza pizza = pizzasDAO.findByName(nom); + out.println(mapper.writeValueAsString(pizza)); + } else { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + } + } else { + List<Pizza> pizzas = pizzasDAO.findAll(); + out.println(mapper.writeValueAsString(pizzas)); + } + } + + // Ajoutez une nouvelle pizza et ses ingrédients à la base de données + public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setContentType("application/json; charset=UTF-8"); + PrintWriter out = response.getWriter(); + ObjectMapper mapper = new ObjectMapper(); + PizzaCompo pizza = mapper.readValue(request.getReader(), PizzaCompo.class); + pizzasDAO.save(pizza); + out.println(mapper.writeValueAsString(pizza)); + + } + + + // Supprimez une pizza de la base de données + public void doDelete(HttpServletRequest request, HttpServletResponse response) throws IOException { + response.setContentType("application/json; charset=UTF-8"); + String pathInfo = request.getPathInfo(); + PrintWriter out = response.getWriter(); + // si /pizza/chorizo/1 alors on supprime la relation entre la pizza et l'ingrédient 1 de la table pizzas_ingredient + if (pathInfo != null) { + String[] pathParts = pathInfo.split("/"); + if (pathParts.length > 2) { + String nom = pathParts[1]; + int id = Integer.parseInt(pathParts[2]); + if (pizzasDAO.findByName(nom) != null){ + pizzasDAO.deletePizzaIngredient(nom, id); + out.println("La relation entre la pizza et l'ingrédient a été supprimée"); + } else { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + } + // si /pizza/chorizo alors on supprime la pizza de la table pizzas + else { + String nom = pathInfo.substring(1); + if (pizzasDAO.findByName(nom) != null){ + pizzasDAO.delete(nom); + out.println("La pizza a été supprimée"); + } else { + response.sendError(HttpServletResponse.SC_NOT_FOUND); + } + } + } + + + } +} diff --git a/WEB-INF/src/dao/CommandeDAO.java b/WEB-INF/src/dao/CommandeDAO.java new file mode 100644 index 0000000000000000000000000000000000000000..ecdbc2efa1a880c2fc8d1a464d2c7d4b98898e28 --- /dev/null +++ b/WEB-INF/src/dao/CommandeDAO.java @@ -0,0 +1,133 @@ +package dao; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; + +import dto.Commande; +import dto.CommandeDetail; +import dto.PizzaCompo; +import utils.DS; + +public class CommandeDAO { + + private DS ds; + + public CommandeDAO() { + this.ds = new DS(); + } + + + public Commande findById(int id) { + try { + Connection con = ds.getConnection(); + String query = "select * from commandes where id = " + id + ";"; + ResultSet rs = con.createStatement().executeQuery(query); + rs.next(); + CommandeDetail commande = new CommandeDetail(rs.getInt("id"), rs.getString("nom"), rs.getDate("date")); + + query = "select * from commande_detail where id = " + id + ";"; + rs = con.createStatement().executeQuery(query); + while (rs.next()) { + String nom = rs.getString("pizza"); + PizzaCompo pizza = new PizzasDAO().findByName(nom); + commande.addPizza(pizza); + } + con.close(); + return commande; + }catch (Exception e) { + System.out.println(e.getMessage()); + } + return null; + } + + public List<Commande> findAll() { + List<Commande> commandes = new ArrayList<Commande>(); + try { + Connection con = ds.getConnection(); + String query = "select * from commandes;"; + ResultSet rs = con.createStatement().executeQuery(query); + while (rs.next()) { + Commande commande = new Commande(rs.getInt("id"), rs.getString("nom"), rs.getDate("date")); + commandes.add(commande); + } + con.close(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + return commandes; + } + + public void save(CommandeDetail commande) { + try { + Connection con = ds.getConnection(); + String query = "insert into commandes values (" + commande.getId() + ", '" + commande.getNom() + "', '" + commande.getDate() + "');"; + con.createStatement().executeUpdate(query); + for (PizzaCompo pizza : commande.getPizzas()) { + query = "insert into commande_detail values (" + commande.getId() + ", '" + pizza.getNom() + "');"; + con.createStatement().executeUpdate(query); + } + con.close(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + public void delete(int id) { + try { + Connection con = ds.getConnection(); + String query = "delete from commandes where id = " + id + ";"; + con.createStatement().executeUpdate(query); + query = "delete from commande_detail where id = " + id + ";"; + con.createStatement().executeUpdate(query); + con.close(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + public double totalPrice(int id) { + double total = 0; + try { + Connection con = ds.getConnection(); + String query = "select * from commande_detail where id = " + id + ";"; + ResultSet rs = con.createStatement().executeQuery(query); + + while (rs.next()) { + String nom = rs.getString("pizza"); + total += new PizzasDAO().totalPrice(nom); + } + con.close(); + + + } catch (Exception e) { + System.out.println(e.getMessage()); + } + return total; + } + + public void deleteCommandeDetail(int id, String pizza) { + try { + Connection con = ds.getConnection(); + String query = "delete from commande_detail where id = " + id + " and pizza = '" + pizza + "';"; + con.createStatement().executeUpdate(query); + con.close(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + public void deleteCommande(int id) { + try { + Connection con = ds.getConnection(); + String query = "delete from commande_detail where id = " + id + ";"; + con.createStatement().executeUpdate(query); + query = "delete from commandes where id = " + id + ";"; + con.createStatement().executeUpdate(query); + con.close(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } +} diff --git a/WEB-INF/src/dao/IngredientDAODatabase.java b/WEB-INF/src/dao/IngredientDAODatabase.java new file mode 100644 index 0000000000000000000000000000000000000000..053dc2bd58685f9c969ebd7facd2935c5bd02305 --- /dev/null +++ b/WEB-INF/src/dao/IngredientDAODatabase.java @@ -0,0 +1,107 @@ +package dao; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; + +import dto.Ingredient; +import utils.DS; + + +public class IngredientDAODatabase { + + + private DS ds; + + + public IngredientDAODatabase(){ + this.ds = new DS(); + } + public Ingredient findById(int id) { + try { + Connection con = ds.getConnection(); + String query = "select * from ingredients where id = " + id + ";"; + ResultSet rs = con.createStatement().executeQuery(query); + rs.next(); + Ingredient ingredient = new Ingredient(rs.getInt("id"), rs.getString("name"), rs.getDouble("price")); + con.close(); + return ingredient; + } + catch (Exception e) { + System.out.println(e.getMessage()); + } + return null; + + } + public List<Ingredient> findAll() { + List<Ingredient> ingredients = new ArrayList<Ingredient>(); + try { + Connection con = ds.getConnection(); + String query = "select * from ingredients;"; + ResultSet rs = con.createStatement().executeQuery(query); + while (rs.next()) { + Ingredient ingredient = new Ingredient(rs.getInt("id"), rs.getString("name"), rs.getDouble("price")); + ingredients.add(ingredient); + } + con.close(); + } + catch (Exception e) { + System.out.println(e.getMessage()); + } + return ingredients; + } + + public void save(Ingredient ingredient) { + try { + + Connection con = ds.getConnection(); + String query = "insert into ingredients values (" + ingredient.getId() + ", '" + ingredient.getName() + "', " + ingredient.getPrice() + ");"; + con.createStatement().executeUpdate(query); + con.close(); + } catch (Exception e) {System.out.println("l'ingrédient existe déjà");} + + } + + public void delete(int id) { + try { + Connection con = ds.getConnection(); + String query = "delete from ingredients where id = " + id + ";"; + con.createStatement().executeUpdate(query); + con.close(); + } catch (Exception e) {System.out.println(e.getMessage());} + } + + public void update(Ingredient ingredient) { + try { + Connection con = ds.getConnection(); + String query = "update ingredients set name = '" + ingredient.getName() + "', price = " + ingredient.getPrice() + " where id = " + ingredient.getId() + " where id=" + ingredient.getId() + ";"; + con.createStatement().executeUpdate(query); + con.close(); + } catch (Exception e) {System.out.println(e.getMessage());} + } + + public void patch(int id ,Ingredient ingredient) { + // le champ à modifier est la seule valeur non nulle de l'ingrédient + String champ = ""; + String valeur = ""; + if (ingredient.getName() != null) { + champ = "name"; + valeur = "'" + ingredient.getName() + "'"; + } + if (ingredient.getPrice() != null) { + champ = "price"; + valeur = "" + ingredient.getPrice(); + } + try { + Connection con = ds.getConnection(); + + + String query = "update ingredients set " + champ + " = " + valeur + " where id = " + id + ";"; + con.createStatement().executeUpdate(query); + con.close(); + } catch (Exception e) {System.out.println(e.getMessage());} + } +} + + diff --git a/WEB-INF/src/dao/PizzasDAO.java b/WEB-INF/src/dao/PizzasDAO.java new file mode 100644 index 0000000000000000000000000000000000000000..27cf9d04bdf95bffab0d986ccaeddb7c75629771 --- /dev/null +++ b/WEB-INF/src/dao/PizzasDAO.java @@ -0,0 +1,141 @@ +package dao; + +import java.sql.Connection; +import java.sql.ResultSet; +import java.util.ArrayList; +import java.util.List; +import dto.Ingredient; +import dto.Pizza; +import dto.PizzaCompo; +import utils.DS; + +public class PizzasDAO { + private DS ds; + + public PizzasDAO() { + this.ds = new DS(); + } + + public PizzaCompo findByName(String name) { + try { + Connection con = ds.getConnection(); + String query = "select * from pizzas where nom = '" + name + "';"; + ResultSet rs = con.createStatement().executeQuery(query); + rs.next(); + + PizzaCompo pizza = new PizzaCompo(rs.getString("nom"), rs.getString("pate"), rs.getDouble("prixBase")); + query = "select * from pizzas_ingredient where nom = '" + pizza.getNom() + "';"; + ResultSet rs2 = con.createStatement().executeQuery(query); + while (rs2.next()) { + int id = rs2.getInt("id"); + Ingredient ingredient = new IngredientDAODatabase().findById(id); + pizza.addIngredient(ingredient); + } + con.close(); + return pizza; + } catch (Exception e) { + System.out.println(e.getMessage()); + } + return null; + } + + public List<Pizza> findAll() { + List<Pizza> pizzas = new ArrayList<Pizza>(); + try { + Connection con = ds.getConnection(); + String query = "select * from pizzas;"; + ResultSet rs = con.createStatement().executeQuery(query); + while (rs.next()) { + Pizza pizza = new Pizza(rs.getString("nom"), rs.getString("pate"), rs.getDouble("prixbase")); + pizzas.add(pizza); + } + con.close(); + return pizzas; + } catch (Exception e) { + System.out.println(e.getMessage()); + } + return null; + } + + public double totalPrice(String name) { + try { + Connection con = ds.getConnection(); + String query = "select * from pizzas where nom = '" + name + "';"; + ResultSet rs = con.createStatement().executeQuery(query); + rs.next(); + double total = rs.getDouble("prixbase"); + query = "select sum(price) as price from ingredients, pizzas_ingredient as pi where pi.id = ingredients.id and pi.nom = '" + name + "';"; + ResultSet rs2 = con.createStatement().executeQuery(query); + rs2.next(); + total += rs2.getDouble("price"); + con.close(); + return total; + } catch (Exception e) { + System.out.println(e.getMessage()); + } + return 0; + } + + public void save(PizzaCompo pizzas) { + try { + Connection con = ds.getConnection(); + String query = "insert into pizzas (nom, pate, prixbase) values ('" + pizzas.getNom() + "', '" + pizzas.getPate() + "', " + pizzas.getPrixBase() + ");"; + con.createStatement().executeUpdate(query); + + for (Ingredient ingredient : pizzas.getIngredients()) { + query = "insert into pizzas_ingredient values ('" + pizzas.getNom() + "', " + ingredient.getId() + ");"; + con.createStatement().executeUpdate(query); + } + con.close(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + public void delete(String name) { + try { + Connection con = ds.getConnection(); + String query = "delete from pizzas_ingredient where nom = '" + name + "';"; + con.createStatement().executeUpdate(query); + query = "delete from pizzas where nom = '" + name + "';"; + con.createStatement().executeUpdate(query); + con.close(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + public void deletePizzaIngredient(String name, int id) { + try { + Connection con = ds.getConnection(); + String query = "delete from pizzas_ingredient where nom = '" + name + "' and id = " + id + ";"; + con.createStatement().executeUpdate(query); + con.close(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + public void addPizzaIngredient(String name, int id) { + try { + Connection con = ds.getConnection(); + String query = "insert into pizzas_ingredient values ('" + name + "', " + id + ");"; + con.createStatement().executeUpdate(query); + con.close(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + + public void update(PizzaCompo pizza) { + try { + Connection con = ds.getConnection(); + String query = "update pizzas set pate = '" + pizza.getPate() + "', prixbase = " + pizza.getPrixBase() + " where nom = '" + pizza.getNom() + "';"; + con.createStatement().executeUpdate(query); + con.close(); + } catch (Exception e) { + System.out.println(e.getMessage()); + } + } + +} diff --git a/WEB-INF/src/dto/Commande.java b/WEB-INF/src/dto/Commande.java new file mode 100644 index 0000000000000000000000000000000000000000..229f84c1ee2081c60dadf1e44884724ae590ca4b --- /dev/null +++ b/WEB-INF/src/dto/Commande.java @@ -0,0 +1,32 @@ +package dto; + +import java.util.Date; +public class Commande { + + private int id; + private String nom; + private Date date; + + public Commande() { + } + + public Commande(int id, String nom, Date date) { + this.id = id; + this.nom = nom; + this.date = date; + } + + public int getId() { + return id; + } + + public String getNom() { + return nom; + } + + public Date getDate() { + return date; + } + + +} diff --git a/WEB-INF/src/dto/CommandeDetail.java b/WEB-INF/src/dto/CommandeDetail.java new file mode 100644 index 0000000000000000000000000000000000000000..8361f7112273e231deef127e6bc2a8a4e6c8b739 --- /dev/null +++ b/WEB-INF/src/dto/CommandeDetail.java @@ -0,0 +1,38 @@ +package dto; + +import java.util.Date; +import java.util.List; +import java.util.ArrayList; + +public class CommandeDetail extends Commande{ + + private List<PizzaCompo> pizzas; + + public CommandeDetail() { + super(); + } + + public CommandeDetail(int id, String nom, Date date, List<PizzaCompo> pizzas) { + super(id, nom, date); + this.pizzas = pizzas; + } + + public CommandeDetail(int id,String nom, Date date) { + super(id, nom, date); + this.pizzas = new ArrayList<PizzaCompo>(); + } + + + public void addPizza(PizzaCompo pizza) { + this.pizzas.add(pizza); + } + + public void removePizza(PizzaCompo pizza) { + this.pizzas.remove(pizza); + } + + public List<PizzaCompo> getPizzas() { + return pizzas; + } +} + diff --git a/WEB-INF/src/dto/Ingredient.java b/WEB-INF/src/dto/Ingredient.java new file mode 100644 index 0000000000000000000000000000000000000000..590dcc258563cd955da88a1d395f392f7abdc65a --- /dev/null +++ b/WEB-INF/src/dto/Ingredient.java @@ -0,0 +1,56 @@ +package dto; + +/*Un ingrédient est caractérisé par +- un identifiant id de type int +- un name de stype String et */ + +public class Ingredient { + private int id; + private String name; + private Double price; + + public Ingredient() { + } + + public Ingredient(int id, String name, Double price) { + this.id = id; + this.name = name; + this.price = price; + } + + public Ingredient(int id) { + this.id = id; + this.name = null; + this.price = null; + } + + public int getId() { + return id; + } + + public String getName() { + return name; + } + + public void setId(int id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + @Override + public String toString() { + return "Ingredient [id=" + id + ", name=" + name + ", price=" + price + "]"; + } + + +} diff --git a/WEB-INF/src/dto/Pizza.java b/WEB-INF/src/dto/Pizza.java new file mode 100644 index 0000000000000000000000000000000000000000..daac47079472e148d484d0f0984d60e5b1915268 --- /dev/null +++ b/WEB-INF/src/dto/Pizza.java @@ -0,0 +1,41 @@ +package dto; + + +public class Pizza { + private String nom; + private String pate; + private Double prixBase; + + public Pizza() { + } + + public Pizza(String nom, String pate, Double prixBase) { + this.nom = nom; + this.pate = pate; + this.prixBase = prixBase; + } + public String getNom() { + return nom; + } + + public String getPate() { + return pate; + } + + public Double getPrixBase() { + return prixBase; + } + + public void setNom(String nom) { + this.nom = nom; + } + + public void setPate(String pate) { + this.pate = pate; + } + + public void setPrixBase(Double prixBase) { + this.prixBase = prixBase; + } + +} diff --git a/WEB-INF/src/dto/PizzaCompo.java b/WEB-INF/src/dto/PizzaCompo.java new file mode 100644 index 0000000000000000000000000000000000000000..d97db6734542a89c668bd8c9f961f28155021452 --- /dev/null +++ b/WEB-INF/src/dto/PizzaCompo.java @@ -0,0 +1,48 @@ +package dto; + +import java.util.ArrayList; +import java.util.List; + +public class PizzaCompo extends Pizza { + + private List<Ingredient> ingredients = new ArrayList<Ingredient>(); + + public PizzaCompo() { + super(); + } + + public PizzaCompo(String nom, String pate, Double prixBase, Ingredient ingredients) { + super(nom, pate, prixBase); + this.ingredients.add(ingredients); + } + + + + public PizzaCompo(String nom, String pate, Double prixBase) { + super(nom, pate, prixBase); + this.ingredients = new ArrayList<Ingredient>(); + } + public Double totalPrice() { + Double total = super.getPrixBase() ; + for (Ingredient ingredient : ingredients) { + total += ingredient.getPrice(); + } + return total; + } + public List<Ingredient> getIngredients() { + return ingredients; + } + + public void setIngredients(List<Ingredient> ingredients) { + this.ingredients = ingredients; + } + + public void addIngredient(Ingredient ingredient) { + this.ingredients.add(ingredient); + } + + public void removeIngredient(Ingredient ingredient) { + this.ingredients.remove(ingredient); + } + +} diff --git a/WEB-INF/src/utils/DS.java b/WEB-INF/src/utils/DS.java new file mode 100644 index 0000000000000000000000000000000000000000..0364d1bb5d6cf8669597bde78f28a625d97dc597 --- /dev/null +++ b/WEB-INF/src/utils/DS.java @@ -0,0 +1,29 @@ +package utils; + +import java.sql.Connection; +import java.sql.DriverManager; + +public class DS { + + private String url ="jdbc:postgresql://psqlserv/but2"; + private String login ="antoinegaienieretu"; + private String password ="moi"; + private String driver = "org.postgresql.Driver"; + + public DS() { + try { + Class.forName(driver); + } catch (ClassNotFoundException e) { + e.getMessage(); + } + } + + public Connection getConnection() throws Exception { + return DriverManager.getConnection(url, login, password); + } + + public void closeConnection(Connection con) throws Exception { + con.close(); + } + +} \ No newline at end of file