188 lines
5.7 KiB
PHP
188 lines
5.7 KiB
PHP
<?php
|
|
require_once 'Todo.php';
|
|
|
|
class TodoController {
|
|
private $dataFile = 'todos.json';
|
|
|
|
public function __construct() {
|
|
// Create data file if it doesn't exist
|
|
if (!file_exists($this->dataFile)) {
|
|
file_put_contents($this->dataFile, json_encode([]));
|
|
}
|
|
}
|
|
|
|
private function loadTodos() {
|
|
$data = file_get_contents($this->dataFile);
|
|
$todosData = json_decode($data, true) ?: [];
|
|
|
|
$todos = [];
|
|
foreach ($todosData as $todoData) {
|
|
$todos[] = new Todo(
|
|
$todoData['id'],
|
|
$todoData['title'],
|
|
$todoData['description'] ?? '',
|
|
$todoData['completed'] ?? false
|
|
);
|
|
}
|
|
|
|
return $todos;
|
|
}
|
|
|
|
private function saveTodos($todos) {
|
|
$todosData = [];
|
|
foreach ($todos as $todo) {
|
|
$todosData[] = $todo->toArray();
|
|
}
|
|
|
|
file_put_contents($this->dataFile, json_encode($todosData, JSON_PRETTY_PRINT));
|
|
}
|
|
|
|
private function getNextId() {
|
|
$todos = $this->loadTodos();
|
|
if (empty($todos)) {
|
|
return 1;
|
|
}
|
|
|
|
$maxId = 0;
|
|
foreach ($todos as $todo) {
|
|
if ($todo->getId() > $maxId) {
|
|
$maxId = $todo->getId();
|
|
}
|
|
}
|
|
|
|
return $maxId + 1;
|
|
}
|
|
|
|
public function getAllTodos() {
|
|
try {
|
|
$todos = $this->loadTodos();
|
|
$todosArray = [];
|
|
|
|
foreach ($todos as $todo) {
|
|
$todosArray[] = $todo->toArray();
|
|
}
|
|
|
|
echo json_encode($todosArray);
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Failed to fetch todos: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function createTodo() {
|
|
try {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$input || !isset($input['title']) || empty(trim($input['title']))) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Title is required']);
|
|
return;
|
|
}
|
|
|
|
$id = $this->getNextId();
|
|
$title = trim($input['title']);
|
|
$description = isset($input['description']) ? trim($input['description']) : '';
|
|
$completed = isset($input['completed']) ? (bool)$input['completed'] : false;
|
|
|
|
$todo = new Todo($id, $title, $description, $completed);
|
|
|
|
$todos = $this->loadTodos();
|
|
$todos[] = $todo;
|
|
$this->saveTodos($todos);
|
|
|
|
http_response_code(201);
|
|
echo json_encode($todo->toArray());
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Failed to create todo: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function updateTodo($id) {
|
|
try {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$input) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Invalid JSON input']);
|
|
return;
|
|
}
|
|
|
|
$todos = $this->loadTodos();
|
|
$todoIndex = -1;
|
|
|
|
for ($i = 0; $i < count($todos); $i++) {
|
|
if ($todos[$i]->getId() === $id) {
|
|
$todoIndex = $i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($todoIndex === -1) {
|
|
http_response_code(404);
|
|
echo json_encode(['error' => 'Todo not found']);
|
|
return;
|
|
}
|
|
|
|
$todo = $todos[$todoIndex];
|
|
|
|
// Update fields if provided
|
|
if (isset($input['title'])) {
|
|
if (empty(trim($input['title']))) {
|
|
http_response_code(400);
|
|
echo json_encode(['error' => 'Title cannot be empty']);
|
|
return;
|
|
}
|
|
$todo->setTitle(trim($input['title']));
|
|
}
|
|
|
|
if (isset($input['description'])) {
|
|
$todo->setDescription(trim($input['description']));
|
|
}
|
|
|
|
if (isset($input['completed'])) {
|
|
$todo->setCompleted((bool)$input['completed']);
|
|
}
|
|
|
|
$this->saveTodos($todos);
|
|
|
|
echo json_encode($todo->toArray());
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Failed to update todo: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
|
|
public function deleteTodo($id) {
|
|
try {
|
|
$todos = $this->loadTodos();
|
|
$todoIndex = -1;
|
|
|
|
for ($i = 0; $i < count($todos); $i++) {
|
|
if ($todos[$i]->getId() === $id) {
|
|
$todoIndex = $i;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if ($todoIndex === -1) {
|
|
http_response_code(404);
|
|
echo json_encode(['error' => 'Todo not found']);
|
|
return;
|
|
}
|
|
|
|
$deletedTodo = $todos[$todoIndex];
|
|
array_splice($todos, $todoIndex, 1);
|
|
$this->saveTodos($todos);
|
|
|
|
echo json_encode([
|
|
'message' => 'Todo deleted successfully',
|
|
'deleted_todo' => $deletedTodo->toArray()
|
|
]);
|
|
} catch (Exception $e) {
|
|
http_response_code(500);
|
|
echo json_encode(['error' => 'Failed to delete todo: ' . $e->getMessage()]);
|
|
}
|
|
}
|
|
}
|
|
?>
|