thomas
This commit is contained in:
54
TP5/EX1/app.js
Normal file
54
TP5/EX1/app.js
Normal file
@@ -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 = `
|
||||
<input type="checkbox" ${todo.done ? 'checked' : ''}>
|
||||
<span>${todo.title}</span>
|
||||
<button class="delete-btn">X</button>
|
||||
`;
|
||||
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);
|
110
TP5/EX1/index.php
Normal file
110
TP5/EX1/index.php
Normal file
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
require 'flight/Flight.php';
|
||||
require 'model/model.php';
|
||||
|
||||
// Connexion à la base de données
|
||||
R::setup('mysql:host=localhost;dbname=votre_base_de_donnees','votre_nom_utilisateur', 'votre_mot_de_passe');
|
||||
R::freeze(true);
|
||||
|
||||
// CORS Headers
|
||||
Flight::map('cors', function() {
|
||||
header('Access-Control-Allow-Origin: *');
|
||||
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
|
||||
header('Access-Control-Allow-Headers: Content-Type, Authorization');
|
||||
});
|
||||
|
||||
Flight::route('OPTIONS /todo(/@id)', function(){
|
||||
Flight::cors();
|
||||
return true;
|
||||
});
|
||||
|
||||
// GET all todos or a single todo
|
||||
Flight::route('GET /todo(/@id)', function($id = null) {
|
||||
Flight::cors();
|
||||
$filter = Flight::request()->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();
|
||||
?>
|
51
TP5/EX1/model.php
Normal file
51
TP5/EX1/model.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
require 'rb-mysql.php';
|
||||
|
||||
class Todo {
|
||||
public static function findAll() {
|
||||
return R::findAll('todo');
|
||||
}
|
||||
|
||||
public static function find($id) {
|
||||
return R::load('todo', $id);
|
||||
}
|
||||
|
||||
public static function findCompleted() {
|
||||
return R::find('todo', 'done = ?', [true]);
|
||||
}
|
||||
|
||||
public static function findUnCompleted() {
|
||||
return R::find('todo', 'done = ?', [false]);
|
||||
}
|
||||
|
||||
public static function create($data) {
|
||||
$todo = R::dispense('todo');
|
||||
$todo->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;
|
||||
}
|
||||
}
|
||||
?>
|
Reference in New Issue
Block a user