From ef4187a2a191d6da8aa554b544528ddfa8aaec4c Mon Sep 17 00:00:00 2001 From: Yanis HESSINI <yanis@Yanis-LAPTOP> Date: Thu, 17 Nov 2022 15:44:50 +0100 Subject: [PATCH] Fonctionne avec PostgreSQL --- Dockerfile | 12 +++++ docker-compose.yml | 41 ++++++++++++----- www/data/memo.json | 2 +- www/index.php | 110 +++++++++++++++++++++++++++++++++++---------- 4 files changed, 130 insertions(+), 35 deletions(-) create mode 100644 Dockerfile diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3f41301 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM nowaja/php:7.4.8-fpm-alpine-pgsql + +# Install redis + +RUN apk add --no-cache --virtual .build-deps $PHPIZE_DEPS \ + && pecl install redis \ + && docker-php-ext-enable redis \ + && apk del .build-deps + +# Add redis to php.ini + +RUN echo "extension=redis.so" > /usr/local/etc/php/conf.d/redis.ini \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index c412abd..0fb22a3 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,9 +1,9 @@ -version: '3.8' +version: '3.9' services: nginx: image: nginx:stable-alpine - container_name: nginx + container_name: nginx-memo volumes: - "./www:/usr/share/nginx/html:ro" - "./log:/var/log/nginx" @@ -11,19 +11,22 @@ services: ports: - published: 80 target: 80 + networks: + - databases php: - image: php:7.4-fpm-alpine - container_name: php + build: . + container_name: php-memo volumes: - "./www:/script" + networks: + - databases - postgresql: + postgresql-memo: image: postgres:latest restart: unless-stopped - container_name: postgresql + container_name: postgresql-memo environment: - HOSTNAME: postgres POSTGRES_USER: user POSTGRES_PASSWORD: password ports: @@ -31,17 +34,35 @@ services: target: 5432 volumes: - postgres_data:/var/lib/postgresdata/data:rw + networks: + - databases - redis: + redis-memo: image: redis:latest restart: unless-stopped - container_name: redis + container_name: redis-memo ports: - published: 6379 target: 6379 volumes: - redis_data:/var/lib/redisdata/data:rw + networks: + - databases + + adminer: + image: adminer + restart: unless-stopped + container_name: adminer-memo + ports: + - published: 8080 + target: 8080 + networks: + - databases volumes: postgres_data: - redis_data: \ No newline at end of file + redis_data: + +networks: + databases: + driver: bridge \ No newline at end of file diff --git a/www/data/memo.json b/www/data/memo.json index 1e39478..8471fbd 100644 --- a/www/data/memo.json +++ b/www/data/memo.json @@ -1 +1 @@ -{"5defb94651ea3":{"texte":"finir le tp de prog web","accomplie":true,"dateAjout":"2019-12-10T15:27:02.000Z"},"5defb9a88e9a3":{"texte":"finir le tp d'animation ","accomplie":false,"dateAjout":"2019-12-10T15:28:40.000Z"},"5defbc36499e1":{"texte":"finir le tp de design","accomplie":true,"dateAjout":"2019-12-10T15:39:34.000Z"},"5defbc3f62e43":{"texte":"finir le tp de 3D","accomplie":false,"dateAjout":"2019-12-10T15:39:43.000Z"},"5defbc4b20c23":{"texte":"finir le tp de bdd","accomplie":false,"dateAjout":"2019-12-10T15:39:55.000Z"},"5defbc7418ff8":{"texte":"finir le tp de vid\u00e9o ","accomplie":true,"dateAjout":"2019-12-10T15:40:36.000Z"}} \ No newline at end of file +{"5defb94651ea3":{"texte":"finir le tp de prog web","accomplie":true,"dateAjout":"2019-12-10T15:27:02.000Z"},"5defb9a88e9a3":{"texte":"finir le tp d'animation ","accomplie":false,"dateAjout":"2019-12-10T15:28:40.000Z"},"5defbc36499e1":{"texte":"finir le tp de design","accomplie":true,"dateAjout":"2019-12-10T15:39:34.000Z"},"5defbc4b20c23":{"texte":"finir le tp de bdd","accomplie":false,"dateAjout":"2019-12-10T15:39:55.000Z"},"5defbc7418ff8":{"texte":"finir le tp de vid\u00e9o ","accomplie":true,"dateAjout":"2019-12-10T15:40:36.000Z"},"63763c41d6d4f":{"texte":"pr\u00e9parer la pr\u00e9sentation d'anglais","accomplie":true,"dateAjout":"2022-11-17 13:50:57"},"63764775e6206":{"texte":"finir projet nosql","accomplie":false,"dateAjout":"2022-11-17 14:38:45"},"637648750c469":{"texte":"finir projet web services","accomplie":false,"dateAjout":"2022-11-17 14:43:01"},"637648842e03f":{"texte":"appr\u00e9cier la puissance de docker","accomplie":false,"dateAjout":"2022-11-17 14:43:16"}} \ No newline at end of file diff --git a/www/index.php b/www/index.php index cf4a8f7..6459625 100644 --- a/www/index.php +++ b/www/index.php @@ -8,6 +8,13 @@ </script> <?php + function debug_to_console($data) { + $output = $data; + if (is_array($output)) + $output = implode(',', $output); + + echo "<script>console.log('Debug Objects: " . $output . "' );</script>"; + } /************************************************* * @@ -15,6 +22,20 @@ * * **********************************************/ +// Connexion à la base de données PostgreSQL + +$config = "pgsql:host=postgresql-memo;port=5432;dbname=postgres"; +$username = "user"; +$password = "password"; + +$db_connection = new PDO($config, $username, $password); + +// Connexion à la base de données Redis + +$redis = new Redis(); +$redis->connect('redis-memo', 6379); + + $tachesFichier = "data/memo.json"; $tachesJSON = file_get_contents($tachesFichier); @@ -22,8 +43,11 @@ $tachesJSON = file_get_contents($tachesFichier); $tachesArray = json_decode($tachesJSON, true); $tachesFilter = $tachesArray; +$sql = "SELECT * FROM taches"; +$stmt = $db_connection->prepare($sql); +$stmt->execute(); +$tachesArrayDb = $stmt->fetchAll(PDO::FETCH_ASSOC); - /*********************** * @@ -39,27 +63,37 @@ if (isset($_POST["texteTache"])) { $idTache = uniqid(); - - $dateHeureTache = gmdate('Y-m-d\TH:i:s.v\Z'); - + $dateHeureTache = gmdate('Y-m-d H:i:s'); + - - $tachesArray[$idTache] = [ "texte" => $texte, "accomplie" => false, "dateAjout" => $dateHeureTache, ]; - - - + $tachesJSON = json_encode($tachesArray); - file_put_contents($tachesFichier, $tachesJSON); -} + + // Insert dans postgresql + + $sql = "INSERT INTO taches (id, texte, accomplie, date_ajout) VALUES (:id, :texte, :accomplie, :date_ajout)"; + $stmt = $db_connection->prepare($sql); + $stmt->execute([ + ':id' => $idTache, + ':texte' => $texte, + ':accomplie' => 0, + ':date_ajout' => $dateHeureTache, + ]); + + + // Insert dans redis + + + } /************************************************************* @@ -81,18 +115,13 @@ if (isset($_POST["texteTache"])) { if (isset($_GET["action"]) && $_GET["action"] == "filtrer") { - - - - - if(isset($_GET["accomplie"]) && $_GET["accomplie"]==="1"){ - $tachesArray=array_filter($tachesArray,function($p){ + $tachesArrayDb=array_filter($tachesArrayDb,function($p){ return ($p["accomplie"] == true); }); } if(isset($_GET["accomplie"]) && $_GET["accomplie"]==="0"){ - $tachesArray=array_filter($tachesArray,function($p){ + $tachesArrayDb=array_filter($tachesArrayDb,function($p){ return ($p["accomplie"] == false); }); } @@ -108,11 +137,28 @@ if (isset($_GET["action"]) && $_GET["action"] == "filtrer") { if (isset($_GET["action"]) && $_GET["action"]=="basculer" && isset($_GET["id"])) { $tachesArray[$_GET["id"]]["accomplie"] = !$tachesArray[$_GET["id"]]["accomplie"]; - + + $sql = "SELECT * FROM taches WHERE id = :id"; + $stmt = $db_connection->prepare($sql); + $stmt->execute([ + ':id' => $_GET["id"], + ]); + $tache = $stmt->fetch(PDO::FETCH_ASSOC); + + // Basculer dans postgresql + + $sql = "UPDATE taches SET accomplie = :accomplie WHERE id = :id"; + $stmt = $db_connection->prepare($sql); + $stmt->execute([ + ':accomplie' => $tache["accomplie"] ? 0 : 1, + ':id' => $_GET["id"], + ]); + + // Basculer dans redis + $tachesJSON = json_encode($tachesArray); - file_put_contents($tachesFichier, $tachesJSON); } @@ -120,13 +166,29 @@ if (isset($_GET["action"]) && $_GET["action"]=="supprimer" && isset($_GET["id"]) unset($tachesArray[$_GET["id"]]); + // Supprimer dans la db postgresql + + $sql = "DELETE FROM taches WHERE id = :id"; + $stmt = $db_connection->prepare($sql); + $stmt->execute([ + ':id' => $_GET["id"], + ]); + + // Supprimer dans redis + + $tachesJSON = json_encode($tachesArray); file_put_contents($tachesFichier, $tachesJSON); } +$sql = "SELECT * FROM taches"; +$stmt = $db_connection->prepare($sql); +$stmt->execute(); +$tachesArrayDb = $stmt->fetchAll(PDO::FETCH_ASSOC); + ?> <!DOCTYPE html> @@ -163,15 +225,15 @@ if (isset($_GET["action"]) && $_GET["action"]=="supprimer" && isset($_GET["id"]) --> <?php - foreach ($tachesArray as $idTache => $infoTache) : + foreach ($tachesArrayDb as $infoTache) : ?> <li class="<?= ($infoTache["accomplie"] === true)?"accomplie":""; ?>"> - <span class="coche done"><a href="?action=basculer&id=<?= $idTache; ?>" title="Cliquez pour faire basculer l'état de cette tâche."><img src="ressources/images/coche.svg" alt=""></a></span> + <span class="coche done"><a href="?action=basculer&id=<?= $infoTache["id"]; ?>" title="Cliquez pour faire basculer l'état de cette tâche."><img src="ressources/images/coche.svg" alt=""></a></span> <span class="texte"><?= $infoTache["texte"]; ?></span> - <span class="ajout"><?= $infoTache["dateAjout"]; ?></span> - <span class="coche"><a href="?action=supprimer&id=<?= $idTache; ?>" title="Cliquez pour supprimer cette tâche."><img src="ressources/images/delete.svg" alt=""></a></span> + <span class="ajout"><?= $infoTache["date_ajout"]; ?></span> + <span class="coche"><a href="?action=supprimer&id=<?= $infoTache["id"]; ?>" title="Cliquez pour supprimer cette tâche."><img src="ressources/images/delete.svg" alt=""></a></span> </li> -- GitLab