commented READ action handler

This commit is contained in:
2026-04-17 19:59:23 +02:00
parent 241ec75323
commit 4707e73421

View File

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