From f28fd85c501c544cb6197f30f357ed7df94c51e6 Mon Sep 17 00:00:00 2001
From: Giildo <giildo.jm@gmail.com>
Date: Wed, 5 Mar 2025 16:56:15 +0100
Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Creation=20de=20l'api=20+=20appel?=
 =?UTF-8?q?=20ajax?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 api/recipes/detail.php |   8 ++++++++
 assets/js/getRecipe.js |  33 +++++++++++++++++++++++++++++++++
 assets/php/Recipe.php  |  14 ++++++++++++++
 assets/php/db.sqlite   | Bin 12288 -> 12288 bytes
 create.php             |  28 ++++++++++++++++------------
 recipe.php             |  12 ++++++++++++
 6 files changed, 83 insertions(+), 12 deletions(-)
 create mode 100644 api/recipes/detail.php
 create mode 100644 assets/js/getRecipe.js
 create mode 100644 recipe.php

diff --git a/api/recipes/detail.php b/api/recipes/detail.php
new file mode 100644
index 0000000..83ed5d3
--- /dev/null
+++ b/api/recipes/detail.php
@@ -0,0 +1,8 @@
+<?php
+
+$id = 1;
+
+require_once $_SERVER['DOCUMENT_ROOT'] . '/assets/php/Recipe.php';
+
+$recipeObj = new Recipe();
+echo json_encode($recipeObj->get($_GET['id']));
diff --git a/assets/js/getRecipe.js b/assets/js/getRecipe.js
new file mode 100644
index 0000000..d18555c
--- /dev/null
+++ b/assets/js/getRecipe.js
@@ -0,0 +1,33 @@
+/**
+fetch('http://localhost:8000/api/recipes/detail.php?id=1')
+    .then((response) => {
+        return response.json()
+    })
+    .then((data) => {
+        console.log(data);
+    })
+**/
+
+// async function loadData(){}
+
+const loadData = async () => {
+    const url = new URLSearchParams(window.location.search)
+    const response = await fetch(`http://localhost:8000/api/recipes/detail.php?id=${url.get('id')}`)
+    const data = await response.json()
+
+    const main = document.querySelector('main')
+
+    const title = document.createElement('h1')
+    title.textContent = data.title
+    main.appendChild(title)
+
+    const img = document.createElement('img')
+    img.src = `/assets/img/${data.thumbnail}`
+    main.insertAdjacentElement('beforeend', img)
+
+    const p = document.createElement('p')
+    p.innerText = data.description
+    main.appendChild(p)
+}
+
+loadData()
\ No newline at end of file
diff --git a/assets/php/Recipe.php b/assets/php/Recipe.php
index d35f312..0670e2d 100644
--- a/assets/php/Recipe.php
+++ b/assets/php/Recipe.php
@@ -26,4 +26,18 @@ class Recipe extends Database
 
         $statement->execute();
     }
+
+    public function getAll()
+    {
+        return $this->db->query('SELECT * FROM recipe')->fetchAll();
+    }
+
+    public function get(string $id)
+    {
+        $stmt = $this->db->prepare('SELECT * FROM recipe WHERE id = :id');
+        $stmt->bindValue(':id', $id);
+        $stmt->execute();
+
+        return $stmt->fetch();
+    }
 }
\ No newline at end of file
diff --git a/assets/php/db.sqlite b/assets/php/db.sqlite
index 7441652bea712336b34f325339dfe3a68b23cf33..4587f5da33b47183c1566a3c16317baa079d1952 100644
GIT binary patch
delta 259
zcmZojXh@hK&B!@X##xY)L9eWnmw|zSiSGsjzZTz(&58mUe4B6ZX)+4)*D$d0t!3fA
z&%cLnEx$596W<4*>`Xp>M`kt#=~M>UlFX8#R0ZS7o$@*^xv7aIdIfpu0&Hvy{QL~E
zMn*6ZIaW3Xab>9Ll+@znqRfJl%=|o<e0mrQJA-(mAcO2=9eH;_xG>OB4&p#tCb!5N
idO<}Mn1D9J%u+Cdm{wANY?>S+hIJ?kHvgAr6aWAbt4Z1b

delta 237
zcmZojXh@hK&B#7c##xY^K`)t+mw|zSiSGsjzZTz(&58mUe4B6ZX)+1}McMe?Gw|Q%
z-^X9WuguTH_kOdW!fifVK{hr9etrgdBcp<h{E~dVtb%lTRyGE4Wd`|@%#xzil+@zn
zqRfJl%=|pKf^Zgg2JuEg2KmW4^6p}AVFzZQzEr5b$$R8<yx>yuOc+L%6d)TZ&xl{a
J=Ku1H0su8GMD73p

diff --git a/create.php b/create.php
index 56166d3..779f973 100644
--- a/create.php
+++ b/create.php
@@ -1,3 +1,9 @@
+<?php
+require_once 'assets/php/Recipe.php';
+$recipeObject = new Recipe();
+$recipes = $recipeObject->getAll();
+?>
+
 <!doctype html>
 <html lang="fr">
 <head>
@@ -30,18 +36,16 @@
   <button type="submit">Enregistrer</button>
 </form>
 
-<?php
-$pdo = new PDO('sqlite:'. __DIR__. '/assets/php/db.sqlite');
-$entries = $pdo->query('SELECT * FROM recipe')->fetchAll();
-?>
 <ul>
-<?php
-foreach ($entries as $entry) {
-    ?>
-<li><?php echo $entry['description']?></li>
-<?php
-}
-?>
-    </ul>
+    <?php foreach ($recipes as $recipe) : ?>
+      <li>
+        <article>
+          <h2><?= $recipe['title'] ?></h2>
+          <p><?php echo $recipe['description'] ?></p>
+          <p><a href="/recipe.php?id=<?= $recipe['id'] ?>">Voir plus</a></p>
+        </article>
+      </li>
+    <?php endforeach; ?>
+</ul>
 </body>
 </html>
\ No newline at end of file
diff --git a/recipe.php b/recipe.php
new file mode 100644
index 0000000..501c1f4
--- /dev/null
+++ b/recipe.php
@@ -0,0 +1,12 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+  <meta charset="UTF-8">
+  <title>Recipe</title>
+
+  <script src="assets/js/getRecipe.js" type="module"></script>
+</head>
+<body>
+<main></main>
+</body>
+</html>
\ No newline at end of file
-- 
GitLab