-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into issue/57-crud-contractes
- Loading branch information
Showing
17 changed files
with
366 additions
and
254 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
-- Insert sample roles (Admin, Manager, Worker) | ||
INSERT INTO roles (role_name) VALUES | ||
INSERT INTO roles (name) VALUES | ||
('Administrador'), | ||
('Gerente'), | ||
('Trabajador'); | ||
|
@@ -8,4 +8,11 @@ INSERT INTO roles (role_name) VALUES | |
INSERT INTO workers (company, name, dni, password, email, role_id, created_at, updated_at, deleted_at) VALUES | ||
('TechCorp', 'Carlos García', '12345678A', 'hashedpassword1', '[email protected]', 1, NOW(), NOW(), NULL), -- Admin | ||
('InnovaTech', 'Ana Martínez', '23456789B', 'hashedpassword2', '[email protected]', 2, NOW(), NOW(), NULL), -- Manager | ||
('DesignWorks', 'José Rodríguez', '34567890C', 'hashedpassword3', '[email protected]', 3, NOW(), NOW(), NULL); -- Worker | ||
('DesignWorks', 'José Rodríguez', '34567890C', 'hashedpassword3', '[email protected]', 3, NOW(), NOW(), NULL); -- Worker | ||
|
||
-- Insert sample tree types | ||
INSERT INTO tree_types (family, genus, species) VALUES | ||
('Fagaceae', 'Quercus', 'Quercus robur'), | ||
('Pinaceae', 'Pinus', 'Pinus sylvestris'), | ||
('Sapindaceae', 'Acer', 'Acer campestre'); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
use App\Core\BaseController; | ||
use App\Core\View; | ||
use App\Models\TreeType; | ||
|
||
class TreeTypeController implements BaseController | ||
{ | ||
public function get() | ||
{ | ||
$tree_types = TreeType::findAll(); | ||
View::render([ | ||
"view" => "TreeType", | ||
"title" => "Tree Types", | ||
"layout" => "MainLayout", | ||
"data" => ["tree_types" => $tree_types] | ||
]); | ||
} | ||
|
||
public function post() {} | ||
public function put() {} | ||
public function delete() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
namespace App\Core; | ||
|
||
interface BaseController | ||
{ | ||
public function get(); | ||
public function post(); | ||
public function put(); | ||
public function delete(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
namespace App\Core; | ||
|
||
abstract class BaseModel | ||
{ | ||
protected int $id; | ||
|
||
// Get the related model in a one-to-one relationship | ||
public function belongsTo($relatedModel, $foreignKey, $ownerKey = 'id') | ||
{ | ||
$relatedTable = $relatedModel::getTableName(); | ||
$foreignKeyValue = $this->{$foreignKey}; | ||
|
||
$query = "SELECT * FROM $relatedTable WHERE $ownerKey = :foreignKeyValue LIMIT 1"; | ||
$results = Database::prepareAndExecute($query, ['foreignKeyValue' => $foreignKeyValue]); | ||
|
||
return !empty($results) ? $relatedModel::mapDataToModel($results[0]) : null; | ||
} | ||
|
||
public function delete() | ||
{ | ||
$table = static::getTableName(); | ||
|
||
if (static::hasSoftDelete()) { | ||
$query = "UPDATE $table SET deleted_at = NOW() WHERE id = :id"; | ||
Database::prepareAndExecute($query, ['id' => $this->id]); | ||
} else { | ||
$query = "DELETE FROM $table WHERE id = :id"; | ||
Database::prepareAndExecute($query, ['id' => $this->id]); | ||
} | ||
} | ||
|
||
public static function find($id) | ||
{ | ||
$table = static::getTableName(); | ||
$query = "SELECT * FROM $table WHERE id = :id LIMIT 1"; | ||
$results = Database::prepareAndExecute($query, ['id' => $id]); | ||
|
||
if (!empty($results)) | ||
return static::mapDataToModel($results[0]); | ||
|
||
return null; | ||
} | ||
|
||
public static function findAll($conditions = []) | ||
{ | ||
$table = static::getTableName(); | ||
$query = "SELECT * FROM $table"; | ||
$params = []; | ||
|
||
if (!empty($conditions)) { | ||
$clauses = []; | ||
foreach ($conditions as $key => $value) { | ||
$clauses[] = "$key = :$key"; | ||
$params[$key] = $value; | ||
} | ||
$query .= " WHERE " . implode(" AND ", $clauses); | ||
} | ||
|
||
$results = Database::prepareAndExecute($query, $params); | ||
|
||
return array_map(fn($row) => static::mapDataToModel($row), $results); | ||
} | ||
|
||
// Fetch all soft deleted records | ||
public static function findSoftDeleted() | ||
{ | ||
if (!static::hasSoftDelete()) | ||
return []; | ||
|
||
$table = static::getTableName(); | ||
$query = "SELECT * FROM $table WHERE deleted_at IS NOT NULL"; | ||
$results = Database::prepareAndExecute($query); | ||
|
||
return array_map(fn($row) => static::mapDataToModel($row), $results); | ||
} | ||
|
||
// Dynamically relationship fetching | ||
public function hasMany($relatedModel, $foreignKey, $localKey = 'id') | ||
{ | ||
$relatedTable = $relatedModel::getTableName(); | ||
$localKeyValue = $this->{$localKey}; | ||
|
||
$query = "SELECT * FROM $relatedTable WHERE $foreignKey = :localKeyValue"; | ||
$results = Database::prepareAndExecute($query, ['localKeyValue' => $localKeyValue]); | ||
|
||
return array_map(fn($row) => $relatedModel::mapDataToModel($row), $results); | ||
} | ||
|
||
// Dynamically check if a table has the deleted_at column | ||
protected static function hasSoftDelete() | ||
{ | ||
static $softDeleteCache = []; | ||
$table = static::getTableName(); | ||
|
||
if (!isset($softDeleteCache[$table])) { | ||
$query = "SHOW COLUMNS FROM $table LIKE 'deleted_at'"; | ||
$result = Database::prepareAndExecute($query); | ||
$softDeleteCache[$table] = !empty($result); // Cache the result | ||
} | ||
|
||
return $softDeleteCache[$table]; | ||
} | ||
|
||
public function restore() | ||
{ | ||
if (static::hasSoftDelete()) { | ||
$table = static::getTableName(); | ||
$query = "UPDATE $table SET deleted_at = NULL WHERE id = :id"; | ||
Database::prepareAndExecute($query, ['id' => $this->id]); | ||
} | ||
} | ||
|
||
public function save() | ||
{ | ||
$table = static::getTableName(); | ||
$properties = get_object_vars($this); | ||
unset($properties['id']); // Avoid saving the id in the data fields | ||
|
||
if ($this->id) { | ||
// Update logic | ||
$fields = []; | ||
foreach ($properties as $key => $value) { | ||
$fields[] = "$key = :$key"; | ||
} | ||
$query = "UPDATE $table SET " . implode(", ", $fields) . " WHERE id = :id"; | ||
$properties['id'] = $this->id; | ||
} else { | ||
// Insert logic | ||
$fields = array_keys($properties); | ||
$placeholders = array_map(fn($field) => ":$field", $fields); | ||
$query = "INSERT INTO $table (" . implode(", ", $fields) . ") VALUES (" . implode(", ", $placeholders) . ")"; | ||
} | ||
|
||
Database::prepareAndExecute($query, $properties); | ||
|
||
if (!$this->id) | ||
$this->id = Database::connect()->lastInsertId(); | ||
} | ||
|
||
//* Abstract methods to enforce subclass implementation | ||
abstract protected static function getTableName(); | ||
abstract protected static function mapDataToModel($data); | ||
|
||
//* Getters and Setters | ||
public function getId() | ||
{ | ||
return $this->id; | ||
} | ||
} |
Oops, something went wrong.