diff --git a/Controller/MediaController.php b/Controller/MediaController.php new file mode 100644 index 0000000..ecfdb0a --- /dev/null +++ b/Controller/MediaController.php @@ -0,0 +1,35 @@ +getAll(); + header('Content-Type: application/json'); + echo json_encode($result); + } + + // ADD new media + public function add($data): void + { + $model = new MediaModel(); + $result = $model->insert($data); + header('Content-Type: application/json'); + echo json_encode($result); + } + + // REMOVE media by id + public function remove($data): void + { + $model = new MediaModel(); + $result = $model->remove($data); + header('Content-Type: application/json'); + echo json_encode($result); + } +} diff --git a/app/Controller/ProjectController.php b/Controller/ProjectController.php similarity index 84% rename from app/Controller/ProjectController.php rename to Controller/ProjectController.php index b8572df..61cfcd3 100644 --- a/app/Controller/ProjectController.php +++ b/Controller/ProjectController.php @@ -1,8 +1,8 @@ '; } + public function addNewMediaType($data): void + { + $model = new ProjectModel(); + $model->insertNewMediaType($data); + } } diff --git a/Controller/TaskController.php b/Controller/TaskController.php new file mode 100644 index 0000000..8497a17 --- /dev/null +++ b/Controller/TaskController.php @@ -0,0 +1,20 @@ +insertTask($data); + } +} diff --git a/Controller/UserController.php b/Controller/UserController.php new file mode 100644 index 0000000..ac8dc88 --- /dev/null +++ b/Controller/UserController.php @@ -0,0 +1,35 @@ +getAll(); + header('Content-Type: application/json'); + echo json_encode($result); + } + + // CREATE new user + public function create($data): void + { + $model = new UserModel(); + $result = $model->insert($data); + header('Content-Type: application/json'); + echo json_encode($result); + } + + // UPDATE user info + public function set($data): void + { + $model = new UserModel(); + $result = $model->update($data); + header('Content-Type: application/json'); + echo json_encode($result); + } +} diff --git a/Model/MediaModel.php b/Model/MediaModel.php new file mode 100644 index 0000000..007160a --- /dev/null +++ b/Model/MediaModel.php @@ -0,0 +1,71 @@ +linkDB(); + $sql = "SELECT * FROM media"; + + try { + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + $sth = $pdo->prepare($sql); + $sth->execute(); + + $result = $sth->fetchAll(\PDO::FETCH_ASSOC); + + $sth->closeCursor(); + $pdo = null; + + return $result; + } catch (\PDOException $e) { + return ["error" => $e->getMessage()]; + } + } + + public function insert($data) + { + $pdo = $this->linkDB(); + $sql = "INSERT INTO media (uid, tid, name, length) + VALUES (:uid, :tid, :name, :length)"; + + try { + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + $sth = $pdo->prepare($sql); + $sth->bindValue(':uid', $data['uid']); + $sth->bindValue(':tid', $data['tid']); + $sth->bindValue(':name', $data['name']); + $sth->bindValue(':length', $data['length'] ?? null); + $sth->execute(); + + $sth->closeCursor(); + $pdo = null; + + return ["success" => true, "message" => "Media inserted"]; + } catch (\PDOException $e) { + return ["error" => $e->getMessage()]; + } + } + + public function remove($data) + { + $pdo = $this->linkDB(); + $sql = "DELETE FROM media WHERE mid = :mid"; + + try { + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + $sth = $pdo->prepare($sql); + $sth->bindValue(':mid', $data['mid']); + $sth->execute(); + + $sth->closeCursor(); + $pdo = null; + + return ["success" => true, "message" => "Media removed"]; + } catch (\PDOException $e) { + return ["error" => $e->getMessage()]; + } + } +} diff --git a/Model/ProjectModel.php b/Model/ProjectModel.php new file mode 100644 index 0000000..a544c42 --- /dev/null +++ b/Model/ProjectModel.php @@ -0,0 +1,49 @@ +linkDB(); + $sql = "SELECT * FROM users"; + + try { + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + } catch (\PDOException $e) { + new Msg(true, null, $e); + } + + $sth = $pdo->prepare($sql); + $sth->execute(); + + $result = $sth->fetchAll(\PDO::FETCH_ASSOC); + $sth->closeCursor(); + $pdo = null; + // fetch all + return $result; + + } + public function insertNewMediaType($data) + { + $pdo = $this->linkDB(); + + try { + // it should add an new type and it is Files + $sql = "INSERT INTO media_types (id, type) + VALUES (:id, :type)"; + $sth = $pdo->prepare($sql); + $sth-> execute([ + ':id' => $this->createUUID(), + ':type' => $data['Files'] + ]); + + $pdo = null; + + new Msg(false, "Medientyp erfolgreich eingefügt."); + } catch (\PDOException $e) { + new Msg(true, 'Fehler beim Einfügen des Medientyps', $e); + } + } +} \ No newline at end of file diff --git a/Model/TaskModel.php b/Model/TaskModel.php new file mode 100644 index 0000000..e5be218 --- /dev/null +++ b/Model/TaskModel.php @@ -0,0 +1,37 @@ +linkDB(); + + try { + + $sql = "INSERT INTO task (id, userId, projectId, title, expense, dueDate, priorityId, done ) + VALUES (:id,:userId,:projectId, :title, :expense, :dueDate, :priorityId, :done)"; + $sth = $pdo->prepare($sql); + $sth-> execute([ + ':id' => $this->createUUID(), + ':userId' => '4f141df7-3c0a-11e8-b046-2c4d544f8fe0', + ':projectId' => $data['projectId'], + ':title' => $data['title'], + ':expense' => str_replace("," , "." ,$data['expense']), + ':dueDate' => date("Y-m-d", strtotime($data['dueDate'])), + ':priorityId' => $data['priorityId'], + ':done' => 0 + ]); + + + $pdo = null; + + new Msg(false, "Task erfolgreich eingefügt."); + } catch (\PDOException $e) { + new Msg(true, 'Fehler beim Einfügen des Tasks', $e); + } + + } +} diff --git a/Model/UserModel.php b/Model/UserModel.php new file mode 100644 index 0000000..5dbc745 --- /dev/null +++ b/Model/UserModel.php @@ -0,0 +1,76 @@ +linkDB(); + $sql = "SELECT uid, username, email, created_at FROM users"; // don’t expose password hash + + try { + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + $sth = $pdo->prepare($sql); + $sth->execute(); + + $result = $sth->fetchAll(\PDO::FETCH_ASSOC); + + $sth->closeCursor(); + $pdo = null; + + return $result; + } catch (\PDOException $e) { + return ["error" => $e->getMessage()]; + } + } + + public function insert($data) + { + $pdo = $this->linkDB(); + $sql = "INSERT INTO users (username, email, password_hash) + VALUES (:username, :email, :password_hash)"; + + try { + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + $sth = $pdo->prepare($sql); + $sth->bindValue(':username', $data['username']); + $sth->bindValue(':email', $data['email']); + $sth->bindValue(':password_hash', password_hash($data['password'], PASSWORD_BCRYPT)); + $sth->execute(); + + $sth->closeCursor(); + $pdo = null; + + return ["success" => true, "message" => "User created"]; + } catch (\PDOException $e) { + return ["error" => $e->getMessage()]; + } + } + + public function update($data) + { + $pdo = $this->linkDB(); + $sql = "UPDATE users + SET username = :username, + email = :email + WHERE uid = :uid"; + + try { + $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); + $sth = $pdo->prepare($sql); + $sth->bindValue(':uid', $data['uid']); + $sth->bindValue(':username', $data['username']); + $sth->bindValue(':email', $data['email']); + $sth->execute(); + + $sth->closeCursor(); + $pdo = null; + + return ["success" => true, "message" => "User updated"]; + } catch (\PDOException $e) { + return ["error" => $e->getMessage()]; + } + } +} diff --git a/app/Config/database.php b/Model/database.php similarity index 98% rename from app/Config/database.php rename to Model/database.php index 9a76c8b..c418786 100644 --- a/app/Config/database.php +++ b/Model/database.php @@ -1,6 +1,5 @@ linkDB(); - - // If the connection is successful - echo "Connected successfully using PDO"; -} catch (Exception $e) { - // Handle connection error - error_log($e->getMessage()); // Log the error message - echo "An error occurred while connecting to the database. Please try again later."; -} diff --git a/Tests/controllersTest.php b/Tests/controllersTest.php deleted file mode 100644 index eedc486..0000000 --- a/Tests/controllersTest.php +++ /dev/null @@ -1,40 +0,0 @@ -testController($controllerPath, $methodName, $params); -echo '
'; -print_r($result); -echo ''; \ No newline at end of file diff --git a/app/Controller/ExampleController.php b/app/Controller/ExampleController.php deleted file mode 100644 index 3be9149..0000000 --- a/app/Controller/ExampleController.php +++ /dev/null @@ -1,65 +0,0 @@ -model = new ExampleModel(); - } - - /** - * Write a new record to the database - * - * @param array $data - * @return void - */ - public function writeRecord(array $data): void - { - try { - $this->model->insertRecord($data); - } catch (Exception $e) { - // Log the error or handle it as needed - error_log("Error writing record: " . $e->getMessage()); - throw new Exception("Failed to write record."); - } - } - - /** - * Get all records from the database - * - * @return array - */ - public function getAllRecords(): array - { - try { - return $this->model->fetchAllRecords(); - } catch (Exception $e) { - // Log the error or handle it as needed - error_log("Error fetching records: " . $e->getMessage()); - return []; - } - } - - /** - * Delete a record by ID - * - * @param int $id - * @return void - */ - public function deleteRecord(int $id): void - { - try { - $this->model->removeRecord($id); - } catch (Exception $e) { - // Log the error or handle it as needed - error_log("Error deleting record: " . $e->getMessage()); - throw new Exception("Failed to delete record."); - } - } -} diff --git a/app/Model/ExampleModel.php b/app/Model/ExampleModel.php deleted file mode 100644 index d1e6ba6..0000000 --- a/app/Model/ExampleModel.php +++ /dev/null @@ -1,55 +0,0 @@ -records = [ - ['id' => 1, 'name' => 'Example 1', 'value' => 100], - ['id' => 2, 'name' => 'Example 2', 'value' => 200], - ['id' => 3, 'name' => 'Example 3', 'value' => 300], - ]; - } - - /** - * Insert a new record into the example data - * - * @param array $data - * @return void - */ - public function insertRecord(array $data): void - { - $this->records[] = [ - 'id' => count($this->records) + 1, - 'name' => $data['name'] ?? 'Unnamed', - 'value' => $data['value'] ?? 0, - ]; - } - - /** - * Fetch all records from the example data - * - * @return array - */ - public function fetchAllRecords(): array - { - return $this->records; - } - - /** - * Remove a record by ID from the example data - * - * @param int $id - * @return void - */ - public function removeRecord(int $id): void - { - $this->records = array_filter($this->records, function ($record) use ($id) { - return $record['id'] !== $id; - }); - } -} diff --git a/app/Model/ProjectModel.php b/app/Model/ProjectModel.php deleted file mode 100644 index 10fa40f..0000000 --- a/app/Model/ProjectModel.php +++ /dev/null @@ -1,30 +0,0 @@ -linkDB(); - $sql = "SELECT id, name FROM project"; - - try { - $pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION); - } catch (\PDOException $e) { - new Msg(true, null, $e); - } - - $sth = $pdo->prepare($sql); - $sth->execute(); - - $result = $sth->fetchAll(\PDO::FETCH_ASSOC); - $sth->closeCursor(); - $pdo = null; - // fetch all - return $result; - - } -} \ No newline at end of file diff --git a/app/Services/.gitkeep b/app/Services/.gitkeep deleted file mode 100644 index e69de29..0000000 diff --git a/index.php b/index.php deleted file mode 100644 index e69de29..0000000 diff --git a/Tests/test.php b/test.php similarity index 86% rename from Tests/test.php rename to test.php index 3c71f64..8fad048 100644 --- a/Tests/test.php +++ b/test.php @@ -1,18 +1,25 @@ $url . '/project', + // $defaults = [ + // CURLOPT_URL => "{$url}/project", + // // CURLOPT_COOKIEFILE => $filepath . 'cookie.txt', // set cookie file to given file + // // CURLOPT_COOKIEJAR => $filepath . 'cookie.txt', // set same file as cookie jar + // CURLOPT_CUSTOMREQUEST => "GET" + + // ]; +// Options for get the userController + $defaults = [ + CURLOPT_URL => "{$url}/user", // CURLOPT_COOKIEFILE => $filepath . 'cookie.txt', // set cookie file to given file // CURLOPT_COOKIEJAR => $filepath . 'cookie.txt', // set same file as cookie jar CURLOPT_CUSTOMREQUEST => "GET" - - ); + ]; /** * Options for get all tasks @@ -114,15 +121,12 @@ // ); // session_write_close(); - $ch = curl_init(); - curl_setopt_array($ch, ($defaults)); + $ch = curl_init(); + curl_setopt_array($ch, $defaults); curl_exec($ch); if(curl_error($ch)) { print(curl_error($ch)); } curl_close($ch); - - // session_start(); - -?> \ No newline at end of file +// session_start(); \ No newline at end of file