commented READ action handler
This commit is contained in:
@@ -1,23 +1,23 @@
|
|||||||
<?php
|
<?php
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
// Contributions API Endpoint
|
// Contributions API Endpoint
|
||||||
// Handles all CRUD Operations for Contributions (Points, Lines, Polygons)
|
// Handles CRUD Operations for Contributions (Points, Lines, Polygons)
|
||||||
// and Voting. Single Entry Point — Action is determined by the 'action'
|
// and Voting. Actions are determined by the 'action' Parameter in
|
||||||
// Parameter in the Request.
|
// the Request.
|
||||||
//
|
//
|
||||||
// Supported Actions:
|
// Supported Actions:
|
||||||
// read — Load all approved Contributions for a Municipality
|
// read — Load approved Contributions
|
||||||
// create — Insert a new Contribution
|
// create — Insert Contributions
|
||||||
// update — Update an existing Contribution
|
// update — Update Contributions
|
||||||
// delete — Delete a Contribution
|
// delete — Delete Contributions
|
||||||
// vote — Cast a Like or Dislike
|
// vote — Like or Dislike Contributions
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
|
|
||||||
require_once __DIR__ . '/db.php';
|
require_once __DIR__ . '/db.php';
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Read Action Parameter and Route to the correct Handler
|
// Read Action Parameter and Route to correct Handler
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
$input = get_input();
|
$input = get_input();
|
||||||
$action = $input['action'] ?? '';
|
$action = $input['action'] ?? '';
|
||||||
@@ -39,7 +39,7 @@ switch ($action) {
|
|||||||
handle_vote($input);
|
handle_vote($input);
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
error_response('Unknown or missing Action. Supported: read, create, update, delete, vote.');
|
error_response('Unknown Action. Supported Actions are read, create, update, delete, vote.');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -49,9 +49,9 @@ switch ($action) {
|
|||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// READ — Load all approved Contributions as GeoJSON FeatureCollection
|
// READ: Loads approved Contributions as GeoJSON FeatureCollection
|
||||||
// Required: municipality_id
|
// Required: municipality_id
|
||||||
// Optional: category (Filter by Category)
|
// Optional: category
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
function handle_read($input) {
|
function handle_read($input) {
|
||||||
$pdo = get_db();
|
$pdo = get_db();
|
||||||
@@ -64,12 +64,13 @@ function handle_read($input) {
|
|||||||
|
|
||||||
$municipality_id = $input['municipality_id'];
|
$municipality_id = $input['municipality_id'];
|
||||||
|
|
||||||
// Build Query — optionally filter by Category
|
// Builds SQL Query
|
||||||
$sql = "SELECT *, ST_AsGeoJSON(geom) AS geojson
|
$sql = "SELECT *, ST_AsGeoJSON(geom) AS geojson
|
||||||
FROM contributions
|
FROM contributions
|
||||||
WHERE municipality_id = :mid AND status = 'approved'";
|
WHERE municipality_id = :mid AND status = 'approved'";
|
||||||
$params = [':mid' => $municipality_id];
|
$params = [':mid' => $municipality_id];
|
||||||
|
|
||||||
|
// Optional: Filters by Category
|
||||||
if (!empty($input['category'])) {
|
if (!empty($input['category'])) {
|
||||||
$sql .= " AND category = :cat";
|
$sql .= " AND category = :cat";
|
||||||
$params[':cat'] = $input['category'];
|
$params[':cat'] = $input['category'];
|
||||||
@@ -85,13 +86,13 @@ function handle_read($input) {
|
|||||||
error_response('Database Error: ' . $e->getMessage(), 500);
|
error_response('Database Error: ' . $e->getMessage(), 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Build GeoJSON FeatureCollection
|
// Builds GeoJSON FeatureCollection
|
||||||
$features = [];
|
$features = [];
|
||||||
|
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
$geometry = json_decode($row['geojson']);
|
$geometry = json_decode($row['geojson']);
|
||||||
|
|
||||||
// Remove raw Geometry Columns from Properties
|
// Removes raw Geometry Columns from Properties
|
||||||
unset($row['geom']);
|
unset($row['geom']);
|
||||||
unset($row['geojson']);
|
unset($row['geojson']);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user