feat(api-ulfius): add routes bike-ride, taxi, ticket-office, ticket-machine

This commit is contained in:
dancingCycle 2023-07-31 19:18:39 +02:00
parent 44d3f294fd
commit c9182b959b
1 changed files with 562 additions and 3 deletions

View File

@ -23,7 +23,11 @@
#include <pthread.h>
#define ROUTE_HELLO "/hello"
#define ROUTE_BIKE_RIDE "/bike-ride"
#define ROUTE_BUS_STOP "/bus-stop"
#define ROUTE_TAXI "/taxi"
#define ROUTE_TICKET_OFFICE "/ticket-office"
#define ROUTE_TICKET_MACHINE "/ticket-machine"
#define ROUTE_TRAIN_STATION "/train-station"
#define ROUTE_PARK_RIDE "/park-ride"
@ -35,9 +39,24 @@ pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
int callback_get_hello (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_default (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_bus_stop_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_bike_ride_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_bike_ride_info (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_taxi_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_taxi_info (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_ticket_office_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_ticket_office_info (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_ticket_machine_count (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_ticket_machine_info (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);
@ -176,9 +195,23 @@ int main(int argc, char *argv[]) {
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_HELLO, NULL, 0, &callback_get_hello, NULL);
//user_data: pass pq connection information to callback
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_BIKE_RIDE, "/count", 0, &callback_get_bike_ride_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_BIKE_RIDE, "/info", 0, &callback_get_bike_ride_info, (void *)conn);
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_TAXI, "/count", 0, &callback_get_taxi_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TAXI, "/info", 0, &callback_get_taxi_info, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TICKET_OFFICE, "/count", 0, &callback_get_ticket_office_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TICKET_OFFICE, "/info", 0, &callback_get_ticket_office_info, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TICKET_MACHINE, "/count", 0, &callback_get_ticket_machine_count, (void *)conn);
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_TICKET_MACHINE, "/info", 0, &callback_get_ticket_machine_info, (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);
@ -302,7 +335,6 @@ int callback_get_bus_stop_count (const struct _u_request * request, struct _u_re
}
/**
* Callback function to reply number of all train stations
*/
int callback_get_train_station_count (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
@ -369,7 +401,6 @@ int callback_get_train_station_count (const struct _u_request * request, struct
}
/**
* 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*/
@ -503,7 +534,6 @@ int callback_get_train_station_info (const struct _u_request * request, struct _
}
/**
* 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*/
@ -568,3 +598,532 @@ int callback_get_park_ride_info (const struct _u_request * request, struct _u_re
printf("callback_get_park_ride_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_bike_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_bike_ride_info() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT osm_id AS id, bike_ride AS tag, name, st_x AS lon, st_y AS lat FROM rvb.tbl_bike_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_bike_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_bike_ride_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_bike_ride_info() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_bike_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_bike_ride_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_bike_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_bike_ride_count() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_bike_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_bike_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_bike_ride_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_bike_ride_count() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_bike_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_bike_ride_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_taxi_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_taxi_info() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT osm_id AS id, amenity AS tag, name, st_x AS lon, st_y AS lat FROM rvb.tbl_taxi;";
//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_taxi_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_taxi_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_taxi_info() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_taxi_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_taxi_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_ticket_office_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_ticket_office_count() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_shop_ticket;";
//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_ticket_office_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_ticket_office_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_ticket_office_count() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_ticket_office_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_ticket_office_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_ticket_machine_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_ticket_machine_count() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_vending_machine;";
//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_ticket_machine_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_ticket_machine_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_ticket_machine_count() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_ticket_machine_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_ticket_machine_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_ticket_office_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_ticket_office_info() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT osm_id AS id, shop AS tag, name, st_x AS lon, st_y AS lat FROM rvb.tbl_shop_ticket;";
//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_ticket_office_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_ticket_office_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_ticket_office_info() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_ticket_office_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_ticket_office_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_ticket_machine_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_ticket_machine_info() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT osm_id AS id, amenity AS tag, name, st_x AS lon, st_y AS lat FROM rvb.tbl_vending_machine;";
//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_ticket_machine_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_ticket_machine_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_ticket_machine_info() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_ticket_machine_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_ticket_machine_info() Done.\n");
return U_CALLBACK_CONTINUE;
}
/**
*/
int callback_get_taxi_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_taxi_count() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStatement = "SELECT count(DISTINCT osm_id) FROM rvb.tbl_taxi;";
//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_taxi_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_taxi_count() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
//printf("callback_get_taxi_count() nbRecords: %d\n",nbRecords);
nbFields = PQnfields(pqRes);
//printf("callback_get_taxi_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_taxi_count() Done.\n");
return U_CALLBACK_CONTINUE;
}