feat(api-ulfius): add routes park-ride/*

This commit is contained in:
dancingCycle 2023-06-16 12:54:48 +02:00
parent ce8d3e5eab
commit 6581ab8877
1 changed files with 142 additions and 3 deletions

View File

@ -25,6 +25,7 @@
#define ROUTE_HELLO "/hello"
#define ROUTE_BUS_STOP "/bus-stop"
#define ROUTE_TRAIN_STATION "/train-station"
#define ROUTE_PARK_RIDE "/park-ride"
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
@ -37,6 +38,8 @@ int callback_default (const struct _u_request * request, struct _u_response * re
int callback_get_bus_stop_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_train_station_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_train_station_info (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_park_ride_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_park_ride_info (const struct _u_request * request, struct _u_response * response, void * user_data);
/**
* decode a u_map into a string
@ -176,6 +179,8 @@ int main(int argc, char *argv[]) {
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_BUS_STOP, "/count", 0, &callback_get_bus_stop_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TRAIN_STATION, "/count", 0, &callback_get_train_station_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TRAIN_STATION, "/info", 0, &callback_get_train_station_info, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_PARK_RIDE, "/count", 0, &callback_get_park_ride_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_PARK_RIDE, "/info", 0, &callback_get_park_ride_info, (void *)conn);
// default_endpoint declaration
ulfius_set_default_endpoint(&instance, &callback_default, NULL);
@ -244,7 +249,7 @@ int callback_get_bus_stop_count (const struct _u_request * request, struct _u_re
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(id) FROM rvb.tbl_bus_stop;";
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_bus_stop;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
@ -311,7 +316,7 @@ int callback_get_train_station_count (const struct _u_request * request, struct
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(id) FROM rvb.tbl_railway;";
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_railway;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
@ -363,6 +368,73 @@ int callback_get_train_station_count (const struct _u_request * request, struct
return U_CALLBACK_CONTINUE;
}
/**
* Callback function to reply number of all train stations
*/
int callback_get_park_ride_count (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_park_ride_count() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_park_ride;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_park_ride_count() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_park_ride_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_park_ride_count() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_park_ride_count() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_park_ride_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
* Callback function to reply all train stations
*/
@ -378,7 +450,7 @@ int callback_get_train_station_info (const struct _u_request * request, struct _
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT osm_id, railway, name, st_x, st_y FROM rvb.tbl_railway;";
prepStatement = "SELECT osm_id AS id, railway AS tag, name, st_x AS lon, st_y AS lat FROM rvb.tbl_railway;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
@ -429,3 +501,70 @@ int callback_get_train_station_info (const struct _u_request * request, struct _
printf("callback_get_train_station_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
* Callback function to reply all train stations
*/
int callback_get_park_ride_info (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStatement;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_park_ride_info() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT osm_id AS id, park_ride AS tag, name, st_x AS lon, st_y AS lat FROM rvb.tbl_park_ride;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStatement, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_park_ride_info() No data retrieved\n");
/*clean up result*/
PQclear(pqRes);
//TODO Why this casting?
(void)(request);
(void)(user_data);
ulfius_set_string_body_response(response, 500, "This is the /entities route: Dba error!");
printf("callback_get_park_ride_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_park_ride_info() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_park_ride_info() nbFields: %d\n",nbFields);
arrayRecords=json_array();
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
}
/*clean up result*/
PQclear(pqRes);
/*set response body*/
ulfius_set_json_body_response(response, 200, arrayRecords);
/*clean up JSON*/
json_decref(arrayRecords);
//TODO Why this casting?
(void)(request);
(void)(user_data);
printf("callback_get_park_ride_info() Done.\n");
return U_CALLBACK_CONTINUE;
}