Skip to content

Commit

Permalink
feat(zone): add Zone MVC
Browse files Browse the repository at this point in the history
  • Loading branch information
0x1026 committed Nov 16, 2024
1 parent 3552a12 commit 589a4d1
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 5 deletions.
10 changes: 5 additions & 5 deletions docker/database/start-scripts/0-init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ create table points (

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
6 changes: 6 additions & 0 deletions docker/database/start-scripts/1-seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ 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() {}
}
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 589a4d1

Please sign in to comment.