Replace old structure with new structure

This commit is contained in:
MosLaptop\Not.Reda
2025-08-28 09:28:59 +02:00
parent ea58af4fcc
commit 69dd02eb91
28 changed files with 552 additions and 62 deletions

View File

@@ -1,20 +1,47 @@
<?php
class Database {
private $host = "mysql.pb.bib.de";
private $db_name = "media_locale";
private $username = "vpr_pbat3h23a";
private $password = "SBHwIWNqmMevnlqt";
public $conn;
public function getConnection() {
$this->conn = null;
namespace App\Config;
use ppb\Library\Msg;
abstract class Database {
/**
* Zugangsdaten für die Datenbank
*/
private $dbName = "vpr_pbat3h23a"; //Datenbankname
private $linkName = "mysql.pb.bib.de"; //Datenbank-Server
private $user = "vpr_pbat3h23a"; //Benutzername
private $pw = "SBHwIWNqmMevnlqt"; //Passwort
/**
* Stellt eine Verbindung zur Datenbank her
*
* @return \PDO Gibt eine Datenbankverbindung zurueck
*/
public function linkDB() {
try {
$this->conn = new PDO("mysql:host={$this->host};dbname={$this->db_name}",
$this->username, $this->password);
$this->conn->exec("set names utf8");
} catch (PDOException $exception) {
echo "Connection error: " . $exception->getMessage();
$pdo = new \PDO("mysql:dbname=$this->dbName;host=$this->linkName"
, $this->user
, $this->pw
, [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION]);
return $pdo;
} catch (\PDOException $e) {
new Msg(true, null, $e);
throw $e;
}
return $this->conn;
}
}
/**
* Zum serverseitigen generieren einer UUID
*
* @return string Liefert eine UUID
*/
public function createUUID()
{
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
}

View File

@@ -0,0 +1,65 @@
<?php
namespace App\Controller;
use App\Model\ExampleModel;
use Exception;
class ExampleController
{
private $model;
public function __construct()
{
// Initialize the ExampleModel
$this->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.");
}
}
}

View File

@@ -0,0 +1,47 @@
<?php
namespace App\Controller;
use App\Model\ProjectModel;
class ProjectController {
public function __construct()
{
}
// Method to get all projects
// This method is called by the router
// and is used to get all projects from the database
// and return them as JSON
public function getProject(): void
{
echo "This is the Table Project";
echo "<br>";
$model = new ProjectModel();
$data = $model->readProject();
echo "<br>";
// Display the data in a table format
echo '<table border="1" cellpadding="5" cellspacing="0">';
// Table header
echo '<tr>';
foreach (array_keys($data[0]) as $header) {
echo '<th>' . htmlspecialchars($header) . '</th>';
}
echo '</tr>';
// Table rows
foreach ($data as $row) {
echo '<tr>';
foreach ($row as $cell) {
echo '<td>' . htmlspecialchars($cell) . '</td>';
}
echo '</tr>';
}
echo '</table>';
}
}

View File

@@ -1,14 +0,0 @@
<?php
require_once __DIR__ . '/../Models/Project.php';
class ProjectController {
private $project;
public function __construct($db) {
$this->project = new Project($db);
}
public function getAllProjects() {
return $this->project->fetchAll();
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace App\Model;
class ExampleModel
{
private $records = [];
public function __construct()
{
// Initialize with some example data
$this->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;
});
}
}

View File

@@ -0,0 +1,30 @@
<?php
namespace App\Model;
use App\Config\Database;
use ppb\Library\Msg;
class ProjectModel extends Database
{
public function readProject()
{
$pdo = $this->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;
}
}

View File

@@ -1,16 +0,0 @@
<?php
class Project {
private $conn;
private $table = "projects";
public function __construct($db) {
$this->conn = $db;
}
public function fetchAll() {
$query = "SELECT * FROM " . $this->table;
$stmt = $this->conn->prepare($query);
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
}