Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(tree-type): add TreeType #70

Merged
merged 18 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion docker/database/start-scripts/0-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,24 @@ create table zones (
foreign key (point_id) references points(id)
);

create table tree_types (
id int auto_increment primary key,
family varchar(255) not null,
genus varchar(255) not null,
species varchar(255) not null unique,
constraint UC_TreeType unique (family, genus, species)
);

create table elements (
id int auto_increment primary key,
name varchar(255),
latitude decimal,
longitude decimal,
tree_types_id int,
created_at timestamp,
deleted_at timestamp,
updated_at timestamp
updated_at timestamp,
foreign key (tree_types_id) references tree_types(id)
);

create table inventory (
Expand Down
8 changes: 7 additions & 1 deletion docker/database/start-scripts/1-seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,10 @@ 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');
20 changes: 20 additions & 0 deletions src/app/Controllers/TreeTypeController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace App\Controllers;

use App\Core\View;
use App\Models\TreeType;

class TreeTypeController
{
public function index()
{
$tree_types = TreeType::getAll();
View::render([
"view" => "TreeType",
"title" => "Tree Types",
"layout" => "MainLayout",
"data" => ["tree_types" => $tree_types]
]);
}
}
120 changes: 120 additions & 0 deletions src/app/Models/TreeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
<?php

Namespace App\Models;

use App\Core\Database;
use PDO;
use PDOException;

class TreeType
{
private int $id;
private string $family;
private string $genus;
private string $species;

// Constructor para inicializar propiedades
public function __construct($data = [])
{
foreach ($data as $key => $value) {
$this->$key = $value;
}
}

// Método estático para recuperar todos los tipos de árboles
public static function getAll()
{
$query = "SELECT * FROM tree_types";
$results = Database::prepareAndExecute($query);
foreach ($results as $key => $value) {
$results[$key] = new self($value);
}
return $results;
}

// Método estático para encontrar un tipo de árbol por ID
public static function findById($id)
{
$query = "SELECT * FROM tree_types WHERE id = :id";
$results = Database::prepareAndExecute($query, ['id' => $id]);

return $results ? new self($results[0]) : null;
}

// Método estático para encontrar un tipo de árbol por familia
public static function findByFamily($family)
{
$query = "SELECT * FROM tree_types WHERE family = :family";
$results = Database::prepareAndExecute($query, ['family' => $family]);

return $results ? new self($results[0]) : null;
}
0x1026 marked this conversation as resolved.
Show resolved Hide resolved


public static function findByGenus($genus)
{
$query = "SELECT * FROM tree_types WHERE genus = :genus";
$results = Database::prepareAndExecute($query, ['genus' => $genus]);

return $results ? new self($results[0]) : null;
}


public static function findBySpecies($species)
{
$query = "SELECT * FROM tree_types WHERE species = :species";
$results = Database::prepareAndExecute($query, ['species' => $species]);
return $results ? new self($results[0]) : null;
}


// Método para guardar un nuevo tipo de árbol
public function save()
{
$query = "INSERT INTO tree_types (family, genus, species)
VALUES (:family, :genus, :species)";
$params = [
'family' => $this->family,
'genus' => $this->genus,
'species' => $this->species
];

return Database::prepareAndExecute($query, $params);
}

// Getters y setters
0x1026 marked this conversation as resolved.
Show resolved Hide resolved
public function getId()
{
return $this->id;
}

public function getFamily()
{
return $this->family;
}

public function getGenus()
{
return $this->genus;
}

public function getSpecies()
{
return $this->species;
}

public function setFamily($family)
{
$this->family = $family;
}

public function setGenus($genus)
{
$this->genus = $genus;
}

public function setSpecies($species)
{
$this->species = $species;
}
}
23 changes: 23 additions & 0 deletions src/app/Views/TreeType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<div class="overflow-x-auto">
<table class="min-w-full bg-white border border-gray-300 rounded-lg shadow-md">
<thead>
<tr class="bg-gray-100 text-left">
<th class="px-4 py-2 border-b">ID</th>
<th class="px-4 py-2 border-b">Family</th>
<th class="px-4 py-2 border-b">Genus</th>
<th class="px-4 py-2 border-b">Species</th>
</tr>
</thead>
<tbody>
<?php foreach ($tree_types as $tree_type): ?>
<tr class="hover:bg-gray-50">
<td class="px-4 py-2 border-b"><?php echo $tree_type->getId(); ?></td>
<td class="px-4 py-2 border-b"><?php echo $tree_type->getFamily(); ?></td>
<td class="px-4 py-2 border-b"><?php echo $tree_type->getGenus(); ?></td>
<td class="px-4 py-2 border-b"><?php echo $tree_type->getSpecies(); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>