From 5ff4c53a10ebe7c85b01fc245ba2d09de5547eeb Mon Sep 17 00:00:00 2001 From: rognant Date: Thu, 28 Aug 2025 14:34:18 +0200 Subject: [PATCH] thomas --- TP1/EX1/Ant.js | 58 +++++++++++ TP1/EX1/app.js | 40 ++++++++ TP1/EX2/Ant.js | 63 ++++++++++++ TP1/EX3/array.html | 25 +++++ TP1/EX3/minmax.html | 31 ++++++ TP1/EX4/gameoflife.js | 63 ++++++++++++ TP2/EX0/simpsons.html | 62 ++++++++++++ TP2/EX1/lights.html | 73 ++++++++++++++ TP2/EX2/classement_ligue1.html | 89 +++++++++++++++++ TP2/EX3/View.js | 65 ++++++++++++ TP3/EX1/app.js | 42 ++++++++ TP3/EX1/index.html | 24 +++++ TP3/EX1/model.js | 16 +++ TP3/EX2/thrones.html | 114 +++++++++++++++++++++ TP3/EX3/communes.html | 177 +++++++++++++++++++++++++++++++++ TP3/EX4/foxes.html | 137 +++++++++++++++++++++++++ TP4/EX0/app.riot | 39 ++++++++ TP4/EX0/index.html | 43 ++++++++ TP4/EX0/my-list.riot | 22 ++++ TP4/EX1/index.html | 21 ++++ TP4/EX1/todo-app.riot | 96 ++++++++++++++++++ TP4/EX2/index.html | 21 ++++ TP4/EX2/search-app.riot | 34 +++++++ TP4/EX2/service.js | 13 +++ TP4/EX3/index.html | 24 +++++ TP4/EX3/todo-router.riot | 98 ++++++++++++++++++ TP5/EX1/app.js | 54 ++++++++++ TP5/EX1/index.php | 110 ++++++++++++++++++++ TP5/EX1/model.php | 51 ++++++++++ 29 files changed, 1705 insertions(+) create mode 100644 TP1/EX1/Ant.js create mode 100644 TP1/EX1/app.js create mode 100644 TP1/EX2/Ant.js create mode 100644 TP1/EX3/array.html create mode 100644 TP1/EX3/minmax.html create mode 100644 TP1/EX4/gameoflife.js create mode 100644 TP2/EX0/simpsons.html create mode 100644 TP2/EX1/lights.html create mode 100644 TP2/EX2/classement_ligue1.html create mode 100644 TP2/EX3/View.js create mode 100644 TP3/EX1/app.js create mode 100644 TP3/EX1/index.html create mode 100644 TP3/EX1/model.js create mode 100644 TP3/EX2/thrones.html create mode 100644 TP3/EX3/communes.html create mode 100644 TP3/EX4/foxes.html create mode 100644 TP4/EX0/app.riot create mode 100644 TP4/EX0/index.html create mode 100644 TP4/EX0/my-list.riot create mode 100644 TP4/EX1/index.html create mode 100644 TP4/EX1/todo-app.riot create mode 100644 TP4/EX2/index.html create mode 100644 TP4/EX2/search-app.riot create mode 100644 TP4/EX2/service.js create mode 100644 TP4/EX3/index.html create mode 100644 TP4/EX3/todo-router.riot create mode 100644 TP5/EX1/app.js create mode 100644 TP5/EX1/index.php create mode 100644 TP5/EX1/model.php diff --git a/TP1/EX1/Ant.js b/TP1/EX1/Ant.js new file mode 100644 index 0000000..8783230 --- /dev/null +++ b/TP1/EX1/Ant.js @@ -0,0 +1,58 @@ +class Ant { + constructor(grid, startX, startY) { + this.grid = grid; + this.x = startX; + this.y = startY; + this.direction = 0; // 0: N, 90: E, 180: S, 270: W + this.colors = { white: 0, black: 1 }; + } + + // Effectue un pas de la fourmi + step() { + const currentColor = this.grid[this.y][this.x]; + + if (currentColor === this.colors.white) { + // Case blanche : la repeindre en noir et tourner à droite + this.grid[this.y][this.x] = this.colors.black; + this.direction = (this.direction + 90) % 360; + } else { + // Case noire : la repeindre en blanc et tourner à gauche + this.grid[this.y][this.x] = this.colors.white; + this.direction = (this.direction - 90 + 360) % 360; + } + + // Déplacer la fourmi d'une case dans sa nouvelle direction + switch (this.direction) { + case 0: + this.y--; + break; // Nord + case 90: + this.x++; + break; // Est + case 180: + this.y++; + break; // Sud + case 270: + this.x--; + break; // Ouest + } + + // Gérer les bords de la grille (déplacement cyclique) + const gridSize = this.grid.length; + this.x = (this.x + gridSize) % gridSize; + this.y = (this.y + gridSize) % gridSize; + } + + // Réinitialise la fourmi et la grille + reset() { + const gridSize = this.grid.length; + for (let y = 0; y < gridSize; y++) { + for (let x = 0; x < gridSize; x++) { + this.grid[y][x] = this.colors.white; + } + } + this.x = Math.floor(gridSize / 2); + this.y = Math.floor(gridSize / 2); + this.direction = 90; // Réinitialise la direction vers l'est + } +} \ No newline at end of file diff --git a/TP1/EX1/app.js b/TP1/EX1/app.js new file mode 100644 index 0000000..29c2d75 --- /dev/null +++ b/TP1/EX1/app.js @@ -0,0 +1,40 @@ +import Ant from './Ant.js'; +// Supposons que vous avez une fonction render pour l'affichage +// Par exemple : function render(grid) { ... } + +let ant; +let grid; +const gridSize = 50; +const intervalId; + +function setup() { + grid = createGrid(gridSize); + ant = new Ant(grid, Math.floor(gridSize / 2), Math.floor(gridSize / 2)); + document.getElementById('resetBtn').addEventListener('click', resetSimulation); + startSimulation(); +} + +function createGrid(size) { + const newGrid = []; + for (let i = 0; i < size; i++) { + newGrid[i] = new Array(size).fill(0); + } + return newGrid; +} + +function startSimulation() { + intervalId = setInterval(() => { + ant.step(); + render(grid); // Appelez votre fonction de rendu ici + }, 100); +} + +function resetSimulation() { + clearInterval(intervalId); + ant.reset(); + render(grid); + startSimulation(); +} + +// Assurez-vous d'appeler setup() au chargement de la page +window.onload = setup; \ No newline at end of file diff --git a/TP1/EX2/Ant.js b/TP1/EX2/Ant.js new file mode 100644 index 0000000..62f8b4f --- /dev/null +++ b/TP1/EX2/Ant.js @@ -0,0 +1,63 @@ +class Ant { + constructor(grid, startX, startY, transitionTable) { + this.grid = grid; + this.x = startX; + this.y = startY; + this.direction = 90; // 0: N, 90: E, 180: S, 270: W + this.antState = 0; + this.transitionTable = transitionTable; + this.numAntStates = transitionTable.length; + this.numTileStates = transitionTable[0].length; + } + + step() { + const tileState = this.grid[this.y][this.x]; + const transition = this.transitionTable[this.antState][tileState]; + + const [antStateChange, directionChange, tileStateChange] = transition; + + // Appliquer les changements + this.antState = (this.antState + antStateChange) % this.numAntStates; + this.direction = (this.direction + directionChange + 360) % 360; + this.grid[this.y][this.x] = (tileState + tileStateChange) % this.numTileStates; + + // Déplacer la fourmi + switch (this.direction) { + case 0: this.y--; break; + case 90: this.x++; break; + case 180: this.y++; break; + case 270: this.x--; break; + } + + // Gérer les bords de la grille + const gridSize = this.grid.length; + this.x = (this.x + gridSize) % gridSize; + this.y = (this.y + gridSize) % gridSize; + } + + reset() { + const gridSize = this.grid.length; + for (let y = 0; y < gridSize; y++) { + for (let x = 0; x < gridSize; x++) { + this.grid[y][x] = 0; + } + } + this.x = Math.floor(gridSize / 2); + this.y = Math.floor(gridSize / 2); + this.direction = 90; + this.antState = 0; + } +} + +// Exemple d'utilisation dans app.js +// const langtonTable = [ +// [[0, 90, 1], [0, -90, 0]] +// ]; +// ant = new Ant(grid, Math.floor(gridSize / 2), Math.floor(gridSize / 2), langtonTable); + +// Exemple de la table du TP +const tpTable = [ + // Tuile 0 Tuile 1 + [[1, 0, 1], [0, 90, 1]], // Fourmi 0 + [[0, -90, 1], [1, 0, 1]] // Fourmi 1 +]; \ No newline at end of file diff --git a/TP1/EX3/array.html b/TP1/EX3/array.html new file mode 100644 index 0000000..1103a2e --- /dev/null +++ b/TP1/EX3/array.html @@ -0,0 +1,25 @@ + + + + Array Operations + + + + + + + \ No newline at end of file diff --git a/TP1/EX3/minmax.html b/TP1/EX3/minmax.html new file mode 100644 index 0000000..c975fdc --- /dev/null +++ b/TP1/EX3/minmax.html @@ -0,0 +1,31 @@ + + + + Min/Max with Reduce + + + + + + + \ No newline at end of file diff --git a/TP1/EX4/gameoflife.js b/TP1/EX4/gameoflife.js new file mode 100644 index 0000000..2410836 --- /dev/null +++ b/TP1/EX4/gameoflife.js @@ -0,0 +1,63 @@ +class GameOfLife { + constructor(width, height) { + this.width = width; + this.height = height; + this.grid = new Array(height).fill(null).map(() => new Array(width).fill(0)); + } + + // Initialise la grille avec l'état de départ + setInitialState(initialStateString) { + this.grid = new Array(this.height).fill(null).map(() => new Array(this.width).fill(0)); + const liveCells = initialStateString.split(';').map(cell => cell.split(',')); + liveCells.forEach(([x, y]) => { + const cellX = parseInt(x); + const cellY = parseInt(y); + if (cellX >= 0 && cellX < this.width && cellY >= 0 && cellY < this.height) { + this.grid[cellY][cellX] = 1; + } + }); + } + + // Calcule le nombre de voisins vivants d'une cellule + getNumberActiveNeighbourCells(x, y) { + let count = 0; + for (let i = -1; i <= 1; i++) { + for (let j = -1; j <= 1; j++) { + if (i === 0 && j === 0) continue; + + const neighborX = x + j; + const neighborY = y + i; + + if (neighborX >= 0 && neighborX < this.width && neighborY >= 0 && neighborY < this.height) { + count += this.grid[neighborY][neighborX]; + } + } + } + return count; + } + + // Calcule la génération suivante + computeNextGeneration() { + const nextGrid = new Array(this.height).fill(null).map(() => new Array(this.width).fill(0)); + + for (let y = 0; y < this.height; y++) { + for (let x = 0; x < this.width; x++) { + const liveNeighbors = this.getNumberActiveNeighbourCells(x, y); + const cellState = this.grid[y][x]; + + if (cellState === 1) { + // Règle 2 : Cellule vivante + if (liveNeighbors === 2 || liveNeighbors === 3) { + nextGrid[y][x] = 1; + } + } else { + // Règle 1 : Cellule morte + if (liveNeighbors === 3) { + nextGrid[y][x] = 1; + } + } + } + } + this.grid = nextGrid; + } +} \ No newline at end of file diff --git a/TP2/EX0/simpsons.html b/TP2/EX0/simpsons.html new file mode 100644 index 0000000..456d2d2 --- /dev/null +++ b/TP2/EX0/simpsons.html @@ -0,0 +1,62 @@ + + + + + Les Simpson + + + +

Membres des familles Simpson

+
+ + + + \ No newline at end of file diff --git a/TP2/EX1/lights.html b/TP2/EX1/lights.html new file mode 100644 index 0000000..edd0d68 --- /dev/null +++ b/TP2/EX1/lights.html @@ -0,0 +1,73 @@ + + + + + Lights Out + + + + +

Lights Out

+
+ + + + + \ No newline at end of file diff --git a/TP2/EX2/classement_ligue1.html b/TP2/EX2/classement_ligue1.html new file mode 100644 index 0000000..737c29c --- /dev/null +++ b/TP2/EX2/classement_ligue1.html @@ -0,0 +1,89 @@ + + + + + Classement Ligue 1 + + + +

Classement Ligue 1 2022-2023

+ + + + + + + + + + + + + + + +
RangÉquipeButs PourButs ContrePoints
+ + + + + \ No newline at end of file diff --git a/TP2/EX3/View.js b/TP2/EX3/View.js new file mode 100644 index 0000000..a0f566d --- /dev/null +++ b/TP2/EX3/View.js @@ -0,0 +1,65 @@ +class View { + constructor() { + this.app = document.getElementById('root'); + this.form = this.app.querySelector('.todo-form'); + this.input = this.form.querySelector('.todo-input'); + this.todoList = this.app.querySelector('.todo-list'); + this.filterTabs = this.app.querySelector('.tabs'); + } + + // ... autres méthodes de la classe View + + bindAddTodo(handler) { + this.form.addEventListener('submit', event => { + event.preventDefault(); + const text = this.input.value.trim(); + if (text !== '') { + handler(text); + this.input.value = ''; + } + }); + } + + bindDeleteTodo(handler) { + this.todoList.addEventListener('click', event => { + if (event.target.classList.contains('delete-button')) { + const id = parseInt(event.target.closest('li').dataset.id); + handler(id); + } + }); + } + + bindToggleTodo(handler) { + this.todoList.addEventListener('click', event => { + if (event.target.type === 'checkbox') { + const id = parseInt(event.target.closest('li').dataset.id); + handler(id); + } + }); + } + + bindEditTodo(handler) { + // Événement 'blur' pour détecter la fin de l'édition d'une tâche + this.todoList.addEventListener('blur', event => { + if (event.target.classList.contains('editable-text')) { + const id = parseInt(event.target.closest('li').dataset.id); + const newText = event.target.textContent.trim(); + handler(id, newText); + } + }, true); + } + + // Méthode pour le double-clic + bindDoubleClickTodo(handler) { + this.todoList.addEventListener('dblclick', event => { + const li = event.target.closest('li'); + if (li) { + const textElement = li.querySelector('.editable-text'); + if (textElement) { + textElement.contentEditable = true; + textElement.focus(); + } + } + }); + } +} \ No newline at end of file diff --git a/TP3/EX1/app.js b/TP3/EX1/app.js new file mode 100644 index 0000000..def4bc1 --- /dev/null +++ b/TP3/EX1/app.js @@ -0,0 +1,42 @@ +import { getMovies } from './model.js'; + +const searchInput = document.getElementById('searchInput'); +const searchBtn = document.getElementById('searchBtn'); +const movieList = document.getElementById('movie-list'); + +async function search(searchMovie) { + if (searchMovie.trim() === '') { + return; + } + + const movies = await getMovies(searchMovie); + renderMovies(movies); +} + +function renderMovies(movies) { + movieList.innerHTML = ''; + if (movies.length === 0) { + movieList.innerHTML = '

Aucun film trouvé.

'; + return; + } + + movies.forEach(movie => { + const movieCard = document.createElement('div'); + movieCard.classList.add('movie-card'); + movieCard.innerHTML = ` +

${movie.Title} (${movie.Year})

+ ${movie.Title} + `; + movieList.appendChild(movieCard); + }); +} + +searchBtn.addEventListener('click', () => { + search(searchInput.value); +}); + +searchInput.addEventListener('keydown', (e) => { + if (e.key === 'Enter') { + search(searchInput.value); + } +}); \ No newline at end of file diff --git a/TP3/EX1/index.html b/TP3/EX1/index.html new file mode 100644 index 0000000..3c7785e --- /dev/null +++ b/TP3/EX1/index.html @@ -0,0 +1,24 @@ + + + + + Recherche de films + + + +

Recherche de films

+
+ + +
+
+ + + + \ No newline at end of file diff --git a/TP3/EX1/model.js b/TP3/EX1/model.js new file mode 100644 index 0000000..3d5bcf2 --- /dev/null +++ b/TP3/EX1/model.js @@ -0,0 +1,16 @@ +export async function getMovies(search) { + const apiKey = '2fcb2848'; + const url = `https://www.omdbapi.com/?apikey=${apiKey}&s=${encodeURIComponent(search)}`; + + try { + const response = await fetch(url); + if (!response.ok) { + throw new Error(`HTTP error! status: ${response.status}`); + } + const data = await response.json(); + return data.Search || []; // Renvoie un tableau vide si aucun résultat + } catch (error) { + console.error("Erreur lors de la recherche de films:", error); + return []; + } +} \ No newline at end of file diff --git a/TP3/EX2/thrones.html b/TP3/EX2/thrones.html new file mode 100644 index 0000000..737769d --- /dev/null +++ b/TP3/EX2/thrones.html @@ -0,0 +1,114 @@ + + + + + Personnages Game of Thrones + + + + +
+

Personnages Game of Thrones

+
+

Personnages par page:

+
+
+ +
+
+
+ +
+ +
+ + +
+
+ + + + \ No newline at end of file diff --git a/TP3/EX3/communes.html b/TP3/EX3/communes.html new file mode 100644 index 0000000..2a4b140 --- /dev/null +++ b/TP3/EX3/communes.html @@ -0,0 +1,177 @@ + + + + + Communes de France + + + + +
+

Rechercher des communes

+
+ +
+
+ +
+
+
+
+ +
+
+ +
+
+
+
+ +
+ +
+
+
+
+ +
+
+ +
+
+ + + + \ No newline at end of file diff --git a/TP3/EX4/foxes.html b/TP3/EX4/foxes.html new file mode 100644 index 0000000..1fc3450 --- /dev/null +++ b/TP3/EX4/foxes.html @@ -0,0 +1,137 @@ + + + + + Jeu des renards + + + +

Jeu des renards

+
+
+

Renards restants : ?

+

Coups joués : 0

+ +
+ + + + \ No newline at end of file diff --git a/TP4/EX0/app.riot b/TP4/EX0/app.riot new file mode 100644 index 0000000..9c0626e --- /dev/null +++ b/TP4/EX0/app.riot @@ -0,0 +1,39 @@ + +

Personnages des Simpsons

+
+ + +
+ + + +
+

Personnage sélectionné :

+

Nom : { state.selectedCharacter.prenom } { state.selectedCharacter.nom }

+

Âge : { state.selectedCharacter.age !== null ? state.selectedCharacter.age : 'inconnu' }

+
+ + +
\ No newline at end of file diff --git a/TP4/EX0/index.html b/TP4/EX0/index.html new file mode 100644 index 0000000..04d9eaf --- /dev/null +++ b/TP4/EX0/index.html @@ -0,0 +1,43 @@ + + + + + Les Simpson avec Riot.js + + + + + + + + + \ No newline at end of file diff --git a/TP4/EX0/my-list.riot b/TP4/EX0/my-list.riot new file mode 100644 index 0000000..5eb3cf9 --- /dev/null +++ b/TP4/EX0/my-list.riot @@ -0,0 +1,22 @@ + + + + + + + \ No newline at end of file diff --git a/TP4/EX1/index.html b/TP4/EX1/index.html new file mode 100644 index 0000000..45aaa7c --- /dev/null +++ b/TP4/EX1/index.html @@ -0,0 +1,21 @@ + + + + + Todo List avec Riot.js + + + + + + + + \ No newline at end of file diff --git a/TP4/EX1/todo-app.riot b/TP4/EX1/todo-app.riot new file mode 100644 index 0000000..cee8bdf --- /dev/null +++ b/TP4/EX1/todo-app.riot @@ -0,0 +1,96 @@ + +

Ma Todo List

+
+ + +
+ +
+ + + + +
+ + + + + + +
\ No newline at end of file diff --git a/TP4/EX2/index.html b/TP4/EX2/index.html new file mode 100644 index 0000000..6e554b4 --- /dev/null +++ b/TP4/EX2/index.html @@ -0,0 +1,21 @@ + + + + + Recherche de films Riot.js + + + + + + + + \ No newline at end of file diff --git a/TP4/EX2/search-app.riot b/TP4/EX2/search-app.riot new file mode 100644 index 0000000..881a5fd --- /dev/null +++ b/TP4/EX2/search-app.riot @@ -0,0 +1,34 @@ + +

Recherche de films

+
+ + +
+ +
+
+

{ movie.Title } ({ movie.Year })

+ { movie.Title } +
+

Aucun film trouvé.

+
+ + +
\ No newline at end of file diff --git a/TP4/EX2/service.js b/TP4/EX2/service.js new file mode 100644 index 0000000..794e428 --- /dev/null +++ b/TP4/EX2/service.js @@ -0,0 +1,13 @@ +export const getMovies = async (search) => { + const apiKey = '2fcb2848'; + const url = `https://www.omdbapi.com/?apikey=${apiKey}&s=${encodeURIComponent(search)}`; + + try { + const response = await fetch(url); + const data = await response.json(); + return data.Search || []; + } catch (error) { + console.error("Erreur lors de la recherche de films:", error); + return []; + } +}; \ No newline at end of file diff --git a/TP4/EX3/index.html b/TP4/EX3/index.html new file mode 100644 index 0000000..7f7eca0 --- /dev/null +++ b/TP4/EX3/index.html @@ -0,0 +1,24 @@ + + + + + Todo List avec Router + + + + + + + + + \ No newline at end of file diff --git a/TP4/EX3/todo-router.riot b/TP4/EX3/todo-router.riot new file mode 100644 index 0000000..59d8d12 --- /dev/null +++ b/TP4/EX3/todo-router.riot @@ -0,0 +1,98 @@ + +

Ma Todo List

+
+ + +
+ + + + + + + + +
\ No newline at end of file diff --git a/TP5/EX1/app.js b/TP5/EX1/app.js new file mode 100644 index 0000000..fd69165 --- /dev/null +++ b/TP5/EX1/app.js @@ -0,0 +1,54 @@ +// app.js + +import { TodoAPI } from './api.js'; + +const todoList = document.getElementById('todo-list'); +const todoForm = document.getElementById('todo-form'); +const todoInput = document.getElementById('todo-input'); + +async function renderTodos() { + todoList.innerHTML = ''; + const todos = await TodoAPI.getTodos(); + todos.forEach(todo => { + const li = document.createElement('li'); + li.classList.add('todo-item'); + li.dataset.id = todo.id; + li.innerHTML = ` + + ${todo.title} + + `; + todoList.appendChild(li); + }); +} + +todoForm.addEventListener('submit', async (e) => { + e.preventDefault(); + const title = todoInput.value.trim(); + if (title) { + await TodoAPI.addTodo(title); + todoInput.value = ''; + renderTodos(); + } +}); + +todoList.addEventListener('click', async (e) => { + const li = e.target.closest('.todo-item'); + if (!li) return; + + const id = li.dataset.id; + + if (e.target.type === 'checkbox') { + const isDone = e.target.checked; + await TodoAPI.toggleTodo(id, isDone); + } + + if (e.target.classList.contains('delete-btn')) { + await TodoAPI.deleteTodo(id); + } + + renderTodos(); +}); + +// Initial rendering +document.addEventListener('DOMContentLoaded', renderTodos); \ No newline at end of file diff --git a/TP5/EX1/index.php b/TP5/EX1/index.php new file mode 100644 index 0000000..13a7252 --- /dev/null +++ b/TP5/EX1/index.php @@ -0,0 +1,110 @@ +query->filter ?? "all"; + + if ($id === null) { + switch($filter) { + case "done": + $todos = Todo::findCompleted(); + break; + case "active": + $todos = Todo::findUnCompleted(); + break; + default: + $todos = Todo::findAll(); + } + Flight::json(["results" => $todos]); + } else { + $todo = Todo::find($id); + if ($todo) { + Flight::json($todo); + } else { + Flight::halt(404, 'Todo not found.'); + } + } +}); + +// POST a new todo +Flight::route('POST /todo', function() { + Flight::cors(); + $requestData = Flight::request()->data; + if (!isset($requestData->title)) { + Flight::halt(400, 'Title is required.'); + } + + $todoData = [ + "title" => $requestData->title, + "done" => $requestData->done ?? false + ]; + + $id = Todo::create($todoData); + $todoData['id'] = $id; + + Flight::response()->header("Location", Flight::request()->url . $id); + Flight::json($todoData, 201); +}); + +// DELETE a todo +Flight::route('DELETE /todo/@id', function($id) { + Flight::cors(); + $success = Todo::delete($id); + if ($success) { + Flight::halt(204); + } else { + Flight::halt(404, 'Todo not found.'); + } +}); + +// PUT (update) a todo +Flight::route('PUT /todo/@id', function($id) { + Flight::cors(); + $requestData = Flight::request()->data; + + $todo = Todo::find($id); + if (!$todo) { + Flight::halt(404, 'Todo not found.'); + } + + $updateData = []; + if (isset($requestData->title)) { + $updateData['title'] = $requestData->title; + } + if (isset($requestData->done)) { + $updateData['done'] = $requestData->done; + } + + if (empty($updateData)) { + Flight::halt(400, 'No data to update.'); + } + + $updatedTodo = Todo::update($id, $updateData); + if ($updatedTodo) { + Flight::json($updatedTodo); + } else { + Flight::halt(500, 'Update failed.'); + } +}); + +Flight::start(); +?> \ No newline at end of file diff --git a/TP5/EX1/model.php b/TP5/EX1/model.php new file mode 100644 index 0000000..fb3424f --- /dev/null +++ b/TP5/EX1/model.php @@ -0,0 +1,51 @@ +title = $data['title']; + $todo->done = $data['done']; + $id = R::store($todo); + return $id; + } + + public static function update($id, $data) { + $todo = R::load('todo', $id); + if ($todo->id === 0) { + return false; + } + + foreach ($data as $key => $value) { + $todo->$key = $value; + } + R::store($todo); + return $todo->export(); + } + + public static function delete($id) { + $todo = R::load('todo', $id); + if ($todo->id !== 0) { + R::trash($todo); + return true; + } + return false; + } +} +?> \ No newline at end of file