From 857704b0e6a6c95571f4360acb18d2e38fb136e3 Mon Sep 17 00:00:00 2001 From: XavierChao Date: Wed, 13 Nov 2024 16:51:50 +0100 Subject: [PATCH 1/4] feat(database-sql): add pruning_type table --- docker/database/start-scripts/0-init.sql | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docker/database/start-scripts/0-init.sql b/docker/database/start-scripts/0-init.sql index 8626380d..d749cf12 100644 --- a/docker/database/start-scripts/0-init.sql +++ b/docker/database/start-scripts/0-init.sql @@ -138,3 +138,9 @@ create table sensor_history ( created_at timestamp default current_timestamp, foreign key (sensor_id) references sensors(id) ); + +create table pruning_type ( + id int auto_increment primary key, + name varchar(20), + description varchar(255) +); From ada3389b20efdd8ceb4d45e0b3abd2d5065ac9d5 Mon Sep 17 00:00:00 2001 From: XavierChao Date: Wed, 13 Nov 2024 16:54:18 +0100 Subject: [PATCH 2/4] feat(pruning-type): add PruningType MVC --- src/app/Controllers/PruningTypeController.php | 20 ++++ src/app/Models/PruningType.php | 110 ++++++++++++++++++ src/app/Views/PruningType.php | 21 ++++ 3 files changed, 151 insertions(+) create mode 100644 src/app/Controllers/PruningTypeController.php create mode 100644 src/app/Models/PruningType.php create mode 100644 src/app/Views/PruningType.php diff --git a/src/app/Controllers/PruningTypeController.php b/src/app/Controllers/PruningTypeController.php new file mode 100644 index 00000000..d2641857 --- /dev/null +++ b/src/app/Controllers/PruningTypeController.php @@ -0,0 +1,20 @@ + "PruningType", + "title" => "Pruning Types", + "layout" => "MainLayout", + "data" => ["pruning_types" => $pruning_types] + ]); + } +} \ No newline at end of file diff --git a/src/app/Models/PruningType.php b/src/app/Models/PruningType.php new file mode 100644 index 00000000..66954362 --- /dev/null +++ b/src/app/Models/PruningType.php @@ -0,0 +1,110 @@ + $value) { + $this->$key = $value; + } + } + + // Static method to retrieve all pruning_type + public static function getAll() + { + $query = "SELECT * FROM pruning_type"; + $results = Database::prepareAndExecute($query); + foreach ($results as $key => $value) { + $results[$key] = new self($value); + } + return $results; + } + + // Static method to find a pruning type by ID + public static function findById($id) + { + $query = "SELECT * FROM pruning_type WHERE id = :id AND deleted_at IS NULL"; + $results = Database::prepareAndExecute($query, ['id' => $id]); + + return $results ? new self($results[0]) : null; + } + + // Static method to find a pruning type by Name + public static function findByName($name) + { + $query = "SELECT * FROM pruning_type WHERE name = :name AND deleted_at IS NULL"; + $results = Database::prepareAndExecute($query, ['name' => $name]); + + return $results ? new self($results[0]) : null; + } + + // Method to save a new pruning type + public function save() + { + $query = "INSERT INTO pruning_type (name, description) + VALUES (:name, :description)"; + $params = [ + 'name' => $this->name, + 'description' => $this->description, + ]; + + return Database::prepareAndExecute($query, $params); + } + + // Method to update an existing pruning type + public function update() + { + $query = "UPDATE pruning_type SET name = :name, description = :description + WHERE id = :id AND deleted_at IS NULL"; + $params = [ + 'id' => $this->id, + 'name' => $this->name, + 'description' => $this->description, + ]; + + return Database::prepareAndExecute($query, $params); + } + + // Getters and setters (you can add more as needed) + public function getId() + { + return $this->id; + } + + public function getName() + { + return $this->name; + } + + public function getDescription() + { + return $this->description; + } + + public function setId($id) + { + $this->id = $id; + } + + public function setName($name) + { + $this->name = $name; + } + + public function setDescription($description) + { + $this->description = $description; + } + +} diff --git a/src/app/Views/PruningType.php b/src/app/Views/PruningType.php new file mode 100644 index 00000000..a5f74d85 --- /dev/null +++ b/src/app/Views/PruningType.php @@ -0,0 +1,21 @@ +
+ + + + + + + + + + + + + + + + + + +
IDNameDescription
getId(); ?>getName(); ?>getDescription(); ?>
+
\ No newline at end of file From e6e6091b0c4ba6913d9d8bdaefdb92d1d6faddec Mon Sep 17 00:00:00 2001 From: XavierChao Date: Wed, 13 Nov 2024 17:02:44 +0100 Subject: [PATCH 3/4] fix(database-sql): change pruning_type table name to plural --- docker/database/start-scripts/0-init.sql | 2 +- src/app/Models/PruningType.php | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docker/database/start-scripts/0-init.sql b/docker/database/start-scripts/0-init.sql index d749cf12..6fd618ee 100644 --- a/docker/database/start-scripts/0-init.sql +++ b/docker/database/start-scripts/0-init.sql @@ -139,7 +139,7 @@ create table sensor_history ( foreign key (sensor_id) references sensors(id) ); -create table pruning_type ( +create table pruning_types ( id int auto_increment primary key, name varchar(20), description varchar(255) diff --git a/src/app/Models/PruningType.php b/src/app/Models/PruningType.php index 66954362..ddecf45b 100644 --- a/src/app/Models/PruningType.php +++ b/src/app/Models/PruningType.php @@ -20,10 +20,10 @@ public function __construct($data = []) } } - // Static method to retrieve all pruning_type + // Static method to retrieve all pruning_types public static function getAll() { - $query = "SELECT * FROM pruning_type"; + $query = "SELECT * FROM pruning_types"; $results = Database::prepareAndExecute($query); foreach ($results as $key => $value) { $results[$key] = new self($value); @@ -34,7 +34,7 @@ public static function getAll() // Static method to find a pruning type by ID public static function findById($id) { - $query = "SELECT * FROM pruning_type WHERE id = :id AND deleted_at IS NULL"; + $query = "SELECT * FROM pruning_types WHERE id = :id AND deleted_at IS NULL"; $results = Database::prepareAndExecute($query, ['id' => $id]); return $results ? new self($results[0]) : null; @@ -43,7 +43,7 @@ public static function findById($id) // Static method to find a pruning type by Name public static function findByName($name) { - $query = "SELECT * FROM pruning_type WHERE name = :name AND deleted_at IS NULL"; + $query = "SELECT * FROM pruning_types WHERE name = :name AND deleted_at IS NULL"; $results = Database::prepareAndExecute($query, ['name' => $name]); return $results ? new self($results[0]) : null; @@ -52,7 +52,7 @@ public static function findByName($name) // Method to save a new pruning type public function save() { - $query = "INSERT INTO pruning_type (name, description) + $query = "INSERT INTO pruning_types (name, description) VALUES (:name, :description)"; $params = [ 'name' => $this->name, @@ -65,7 +65,7 @@ public function save() // Method to update an existing pruning type public function update() { - $query = "UPDATE pruning_type SET name = :name, description = :description + $query = "UPDATE pruning_types SET name = :name, description = :description WHERE id = :id AND deleted_at IS NULL"; $params = [ 'id' => $this->id, From edd91db03ef30699944830d6de822982c800760d Mon Sep 17 00:00:00 2001 From: 0x1026 <69076992+0x1026@users.noreply.github.com> Date: Sat, 16 Nov 2024 18:04:44 +0100 Subject: [PATCH 4/4] fix(pruning-type): update changes and add seed samples --- docker/database/start-scripts/1-seed.sql | 6 + src/app/Controllers/PruningTypeController.php | 11 +- src/app/Models/PruningType.php | 108 ++---------------- src/app/Views/PruningType.php | 4 +- 4 files changed, 28 insertions(+), 101 deletions(-) diff --git a/docker/database/start-scripts/1-seed.sql b/docker/database/start-scripts/1-seed.sql index eb418368..9c3ca232 100644 --- a/docker/database/start-scripts/1-seed.sql +++ b/docker/database/start-scripts/1-seed.sql @@ -27,3 +27,9 @@ INSERT INTO task_types (name) VALUES ('Abono arbustos'), ('Podar setos'), ('Abono setos'); + +-- Insert sample pruning types +INSERT INTO pruning_types (name, description) VALUES +('A', 'Poda de mantenimiento en árbol tipo A, caduco, de p.c. entre 41/80 cm.'), +('B', 'Poda de mantenimiento en árbol tipo B, caduco, de p.c. mayor de 81 cm.'), +('C', 'Poda de mantenimiento en árbol tipo C, perenne, de p.c. entre 41/60 cm.'); \ No newline at end of file diff --git a/src/app/Controllers/PruningTypeController.php b/src/app/Controllers/PruningTypeController.php index d2641857..d3569d39 100644 --- a/src/app/Controllers/PruningTypeController.php +++ b/src/app/Controllers/PruningTypeController.php @@ -2,14 +2,15 @@ namespace App\Controllers; +use App\Core\BaseController; use App\Core\View; use App\Models\PruningType; -class PruningTypeController +class PruningTypeController implements BaseController { - public function index() + public function get() { - $pruning_types = PruningType::getAll(); + $pruning_types = PruningType::findAll(); View::render([ "view" => "PruningType", "title" => "Pruning Types", @@ -17,4 +18,8 @@ public function index() "data" => ["pruning_types" => $pruning_types] ]); } + + public function post() {} + public function put() {} + public function delete() {} } \ No newline at end of file diff --git a/src/app/Models/PruningType.php b/src/app/Models/PruningType.php index ddecf45b..bbb2d33e 100644 --- a/src/app/Models/PruningType.php +++ b/src/app/Models/PruningType.php @@ -2,109 +2,25 @@ namespace App\Models; +use App\Core\BaseModel; use App\Core\Database; -use PDO; -use PDOException; -class PruningType +class PruningType extends BaseModel { - private $id; - private $name; - private $description; + public string $name; + public ?string $description; - // Constructor to initialize properties (optional) - public function __construct($data = []) + protected static function getTableName() { - foreach ($data as $key => $value) { - $this->$key = $value; - } + return 'pruning_types'; } - // Static method to retrieve all pruning_types - public static function getAll() + protected static function mapDataToModel($data) { - $query = "SELECT * FROM pruning_types"; - $results = Database::prepareAndExecute($query); - foreach ($results as $key => $value) { - $results[$key] = new self($value); - } - return $results; + $pruning_type = new PruningType(); + $pruning_type->id = $data['id']; + $pruning_type->name = $data['name']; + $pruning_type->description = $data['description']; + return $pruning_type; } - - // Static method to find a pruning type by ID - public static function findById($id) - { - $query = "SELECT * FROM pruning_types WHERE id = :id AND deleted_at IS NULL"; - $results = Database::prepareAndExecute($query, ['id' => $id]); - - return $results ? new self($results[0]) : null; - } - - // Static method to find a pruning type by Name - public static function findByName($name) - { - $query = "SELECT * FROM pruning_types WHERE name = :name AND deleted_at IS NULL"; - $results = Database::prepareAndExecute($query, ['name' => $name]); - - return $results ? new self($results[0]) : null; - } - - // Method to save a new pruning type - public function save() - { - $query = "INSERT INTO pruning_types (name, description) - VALUES (:name, :description)"; - $params = [ - 'name' => $this->name, - 'description' => $this->description, - ]; - - return Database::prepareAndExecute($query, $params); - } - - // Method to update an existing pruning type - public function update() - { - $query = "UPDATE pruning_types SET name = :name, description = :description - WHERE id = :id AND deleted_at IS NULL"; - $params = [ - 'id' => $this->id, - 'name' => $this->name, - 'description' => $this->description, - ]; - - return Database::prepareAndExecute($query, $params); - } - - // Getters and setters (you can add more as needed) - public function getId() - { - return $this->id; - } - - public function getName() - { - return $this->name; - } - - public function getDescription() - { - return $this->description; - } - - public function setId($id) - { - $this->id = $id; - } - - public function setName($name) - { - $this->name = $name; - } - - public function setDescription($description) - { - $this->description = $description; - } - } diff --git a/src/app/Views/PruningType.php b/src/app/Views/PruningType.php index a5f74d85..6d9b3de5 100644 --- a/src/app/Views/PruningType.php +++ b/src/app/Views/PruningType.php @@ -12,8 +12,8 @@ getId(); ?> - getName(); ?> - getDescription(); ?> + name; ?> + description; ?>