tasks.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. header('Content-Type: application/json');
  3. header('Access-Control-Allow-Origin: *');
  4. header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE');
  5. header('Access-Control-Allow-Headers: Content-Type, Authorization');
  6. require_once __DIR__ . '/../config/database.php';
  7. class Task {
  8. private $conn;
  9. private $table = 'tasks';
  10. public function __construct($db) {
  11. $this->conn = $db;
  12. }
  13. public function create($data) {
  14. $sql = "INSERT INTO {$this->table} (title, description, status, priority, project_id, due_date, created_at, updated_at)
  15. VALUES (:title, :description, :status, :priority, :project_id, :due_date, NOW(), NOW())";
  16. $stmt = $this->conn->prepare($sql);
  17. $stmt->bindParam(':title', $data['title']);
  18. $stmt->bindParam(':description', $data['description']);
  19. $stmt->bindParam(':status', $data['status']);
  20. $stmt->bindParam(':priority', $data['priority']);
  21. $stmt->bindParam(':project_id', $data['project_id']);
  22. $stmt->bindParam(':due_date', $data['due_date']);
  23. if ($stmt->execute()) {
  24. return [
  25. 'success' => true,
  26. 'message' => 'Task created successfully',
  27. 'id' => $this->conn->lastInsertId()
  28. ];
  29. }
  30. return [
  31. 'success' => false,
  32. 'message' => 'Failed to create task'
  33. ];
  34. }
  35. public function getAll($projectId = null) {
  36. $sql = "SELECT t.*, p.project_name
  37. FROM {$this->table} t
  38. LEFT JOIN projects p ON t.project_id = p.id
  39. WHERE 1=1";
  40. if ($projectId) {
  41. $sql .= " AND t.project_id = :project_id";
  42. }
  43. $sql .= " ORDER BY t.created_at DESC";
  44. $stmt = $this->conn->prepare($sql);
  45. if ($projectId) {
  46. $stmt->bindParam(':project_id', $projectId);
  47. }
  48. $stmt->execute();
  49. $tasks = $stmt->fetchAll(PDO::FETCH_ASSOC);
  50. return [
  51. 'success' => true,
  52. 'data' => $tasks
  53. ];
  54. }
  55. public function getById($id) {
  56. $sql = "SELECT t.*, p.project_name
  57. FROM {$this->table} t
  58. LEFT JOIN projects p ON t.project_id = p.id
  59. WHERE t.id = :id";
  60. $stmt = $this->conn->prepare($sql);
  61. $stmt->bindParam(':id', $id);
  62. $stmt->execute();
  63. $task = $stmt->fetch(PDO::FETCH_ASSOC);
  64. if ($task) {
  65. return [
  66. 'success' => true,
  67. 'data' => $task
  68. ];
  69. }
  70. return [
  71. 'success' => false,
  72. 'message' => 'Task not found'
  73. ];
  74. }
  75. public function update($id, $data) {
  76. $sql = "UPDATE {$this->table}
  77. SET title = :title, description = :description, status = :status,
  78. priority = :priority, project_id = :project_id, due_date = :due_date, updated_at = NOW()
  79. WHERE id = :id";
  80. $stmt = $this->conn->prepare($sql);
  81. $stmt->bindParam(':title', $data['title']);
  82. $stmt->bindParam(':description', $data['description']);
  83. $stmt->bindParam(':status', $data['status']);
  84. $stmt->bindParam(':priority', $data['priority']);
  85. $stmt->bindParam(':project_id', $data['project_id']);
  86. $stmt->bindParam(':due_date', $data['due_date']);
  87. $stmt->bindParam(':id', $id);
  88. if ($stmt->execute()) {
  89. return [
  90. 'success' => true,
  91. 'message' => 'Task updated successfully'
  92. ];
  93. }
  94. return [
  95. 'success' => false,
  96. 'message' => 'Failed to update task'
  97. ];
  98. }
  99. public function delete($id) {
  100. $sql = "DELETE FROM {$this->table} WHERE id = :id";
  101. $stmt = $this->conn->prepare($sql);
  102. $stmt->bindParam(':id', $id);
  103. if ($stmt->execute()) {
  104. return [
  105. 'success' => true,
  106. 'message' => 'Task deleted successfully'
  107. ];
  108. }
  109. return [
  110. 'success' => false,
  111. 'message' => 'Failed to delete task'
  112. ];
  113. }
  114. }
  115. // Handle API requests
  116. $method = $_SERVER['REQUEST_METHOD'];
  117. try {
  118. $database = new Database();
  119. $conn = $database->getConnection();
  120. $task = new Task($conn);
  121. switch ($method) {
  122. case 'GET':
  123. $projectId = $_GET['project_id'] ?? null;
  124. $result = $task->getAll($projectId);
  125. echo json_encode($result);
  126. break;
  127. case 'POST':
  128. $data = json_decode(file_get_contents('php://input'), true);
  129. $result = $task->create($data);
  130. echo json_encode($result);
  131. break;
  132. case 'PUT':
  133. $data = json_decode(file_get_contents('php://input'), true);
  134. $id = $data['id'];
  135. $result = $task->update($id, $data);
  136. echo json_encode($result);
  137. break;
  138. case 'DELETE':
  139. $id = $_GET['id'];
  140. $result = $task->delete($id);
  141. echo json_encode($result);
  142. break;
  143. default:
  144. echo json_encode([
  145. 'success' => false,
  146. 'message' => 'Invalid request method'
  147. ]);
  148. break;
  149. }
  150. } catch (Exception $e) {
  151. echo json_encode([
  152. 'success' => false,
  153. 'message' => 'Database error: ' . $e->getMessage()
  154. ]);
  155. }
  156. ?>