Skip to content

Commit

Permalink
feat(zone): add Zone (#74)
Browse files Browse the repository at this point in the history
Introduce Point and Zone models along with their respective controllers
and views, establishing a foundational structure for managing geographic
data.
  • Loading branch information
0x1026 authored Nov 16, 2024
2 parents 12463b2 + 589a4d1 commit 5fdfb49
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 8 deletions.
15 changes: 8 additions & 7 deletions docker/database/start-scripts/0-init.sql
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
create table points (
id int auto_increment primary key,
latitude decimal,
longitude decimal
latitude decimal(10, 7) not null,
longitude decimal(10, 7) not null,
constraint UC_Point unique (latitude, longitude)
);

create table zones (
id int auto_increment primary key,
name varchar(255),
quantity int,
postal_code int,
point_id int,
foreign key (point_id) references points(id)
name varchar(255) not null,
postal_code int not null,
point_id int not null unique,
foreign key (point_id) references points(id),
constraint UC_Zone unique (name, postal_code)
);

create table tree_types (
Expand Down
14 changes: 13 additions & 1 deletion docker/database/start-scripts/1-seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@ INSERT INTO task_types (name) VALUES
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.');
('C', 'Poda de mantenimiento en árbol tipo C, perenne, de p.c. entre 41/60 cm.');

-- Insert sample points
INSERT INTO points (latitude, longitude) VALUES
(40.416775, -3.703790),
(40.416776, -3.703795),
(40.416777, -3.703800);

-- Insert sample zones
INSERT INTO zones (name, postal_code, point_id) VALUES
('Zona 1', '46001', 1),
('Zona 2', '46002', 2),
('Zona 3', '46003', 3);
25 changes: 25 additions & 0 deletions src/app/Controllers/ZoneController.php
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\Zone;

class ZoneController implements BaseController
{
public function get()
{
$zones = Zone::findAll();
View::render([
"view" => "Zone",
"title" => "Zones",
"layout" => "MainLayout",
"data" => ["zones" => $zones]
]);
}

public function post() {}
public function put() {}
public function delete() {}
}
25 changes: 25 additions & 0 deletions src/app/Models/Point.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Models;

use App\Core\BaseModel;

class Point extends BaseModel
{
public $latitude;
public $longitude;

protected static function getTableName()
{
return 'points';
}

protected static function mapDataToModel($data)
{
$point = new Point();
$point->id = $data['id'];
$point->latitude = $data['latitude'];
$point->longitude = $data['longitude'];
return $point;
}
}
33 changes: 33 additions & 0 deletions src/app/Models/Zone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Models;

use App\Core\BaseModel;

class Zone extends BaseModel
{
public $name;
public $postal_code;
public $point_id;

protected static function getTableName()
{
return 'zones';
}

protected static function mapDataToModel($data)
{
$zone = new Zone();
$zone->id = $data['id'];
$zone->name = $data['name'];
$zone->postal_code = $data['postal_code'];
$zone->point_id = $data['point_id'];
return $zone;
}

// Fetch the zone's point
public function point()
{
return $this->belongsTo(Point::class, 'point_id', 'id');
}
}
25 changes: 25 additions & 0 deletions src/app/Views/Zone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<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">Name</th>
<th class="px-4 py-2 border-b">Postal Code</th>
<th class="px-4 py-2 border-b">Latitude</th>
<th class="px-4 py-2 border-b">Longitude</th>

</tr>
</thead>
<tbody>
<?php foreach ($zones as $zone): ?>
<tr class="hover:bg-gray-50">
<td class="px-4 py-2 border-b"><?php echo $zone->getId(); ?></td>
<td class="px-4 py-2 border-b"><?php echo $zone->name; ?></td>
<td class="px-4 py-2 border-b"><?php echo $zone->postal_code; ?></td>
<td class="px-4 py-2 border-b"><?php echo $zone->point()->latitude; ?></td>
<td class="px-4 py-2 border-b"><?php echo $zone->point()->longitude; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>

0 comments on commit 5fdfb49

Please sign in to comment.