feat(relations): add *distances* endpoint

This commit is contained in:
dancingCycle 2023-09-22 15:02:28 +02:00
parent 009f6d28ed
commit 82756d0ecf
3 changed files with 86 additions and 6 deletions

View File

@ -27,6 +27,8 @@ curl -X POST --data "name=Hi from curl!" http://<host>:<port>/entities/create
* read all
```
curl -X GET http://<host>:<port>/entities/info
curl -X GET http://<host>:<port>/relations/info
curl -X GET http://<host>:<port>/distances/info
```
* read single

View File

@ -1 +1,3 @@
cfg-sib00_rgncycle.txt
*.o
main

View File

@ -25,24 +25,32 @@
#define ROUTE_HELLO "/hello"
#define ROUTE_ENTITIES "/entities"
#define ROUTE_RELATIONS "/relations"
#define ROUTE_DISTANCES "/distances"
pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;
/**
* callback functions declaration
*/
/* callback for Test*/
int callback_get_hello (const struct _u_request * request, struct _u_response * response, void * user_data);
/* callback for Root*/
int callback_default (const struct _u_request * request, struct _u_response * response, void * user_data);
/* callback for Entities*/
int callback_create_entity (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_entities (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_get_entity (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_update_entity (const struct _u_request * request, struct _u_response * response, void * user_data);
int callback_delete_entity (const struct _u_request * request, struct _u_response * response, void * user_data);
/* callback for Relations*/
int callback_get_relations (const struct _u_request * request, struct _u_response * response, void * user_data);
/* callback for Distances*/
int callback_get_distances (const struct _u_request * request, struct _u_response * response, void * user_data);
/**
* decode a u_map into a string
*/
@ -193,6 +201,9 @@ int main(int argc, char *argv[]) {
/* callbacks for Relations*/
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_RELATIONS, "/info", 0, &callback_get_relations, (void *)conn);
/* callbacks for Distances*/
ulfius_add_endpoint_by_val(&instance, "GET", ROUTE_DISTANCES, "/info", 0, &callback_get_distances, (void *)conn);
// default_endpoint declaration
ulfius_set_default_endpoint(&instance, &callback_default, NULL);
@ -279,10 +290,10 @@ int callback_get_entity (const struct _u_request * request, struct _u_response *
conn = (PGconn *)user_data;
/*define prepared statement*/
len=snprintf(NULL,0,"SELECT * FROM entities WHERE entity_id=%s;",value);
len=snprintf(NULL,0,"SELECT * FROM tbl_entities WHERE entity_id=%s;",value);
prepStmEntities=malloc((size_t)(len+1));
//TODO Release prepStmEntities!
snprintf(prepStmEntities,(size_t)(len+1),"SELECT * FROM entities WHERE entity_id=%s;",value);
snprintf(prepStmEntities,(size_t)(len+1),"SELECT * FROM tbl_entities WHERE entity_id=%s;",value);
//printf("callback_get_entity() prepStmEntities: %s\n",prepStmEntities);
//lock PGconn mutex
@ -340,7 +351,7 @@ int callback_get_entity (const struct _u_request * request, struct _u_response *
}
}
}else{
printf("callback_delete_entity() HTTP GET request error!\n");
printf("callback_get_entity() HTTP GET request error!\n");
ulfius_set_string_body_response(response, 400, "This is the /entity/:id/info route: HTTP GET request error!");
}
/*clean up remainings*/
@ -363,7 +374,7 @@ int callback_get_entities (const struct _u_request * request, struct _u_response
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStmEntities = "SELECT * FROM entities;";
prepStmEntities = "SELECT * FROM tbl_entities;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
@ -436,7 +447,7 @@ int callback_get_relations (const struct _u_request * request, struct _u_respons
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStmEntities = "SELECT * FROM relations;";
prepStmEntities = "SELECT * FROM tbl_relations;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
@ -486,6 +497,71 @@ int callback_get_relations (const struct _u_request * request, struct _u_respons
return U_CALLBACK_CONTINUE;
}
/**
* Callback function to reply all distances
*/
int callback_get_distances (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
PGconn *conn;
char *prepStmEntities;
PGresult *pqRes;
int nbRecords,nbFields;
json_t *arrayRecords, *arrayFields, *stringField;
printf("callback_get_distances() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStmEntities = "SELECT * FROM tbl_distances;";
//lock PGconn mutex
pthread_mutex_lock(&mutex);
/*create prepared statement*/
pqRes = PQexecParams(conn, prepStmEntities, 0, NULL, NULL, NULL, NULL, 0);
//unlock PGconn mutex
pthread_mutex_unlock(&mutex);
if (PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
printf("callback_get_distances() 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 /distances route: Dba error!");
printf("callback_get_distances() Done.\n");
return U_CALLBACK_CONTINUE;
}
/*evaluate result*/
nbRecords = PQntuples(pqRes);
nbFields = PQnfields(pqRes);
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_distances() Done.\n");
return U_CALLBACK_CONTINUE;
}
int query_db (const PGconn * conn, const char ** queryStr, const char ** valueStr){
/*declarations*/
int res;
@ -564,7 +640,7 @@ int callback_delete_entity (const struct _u_request * request, struct _u_respons
//printf("callback_delete_entity() key is %s, value is %s\n", keys[0], value);
//TODO delete entity
const char * queryEntities="DELETE FROM entities WHERE entity_id=%s RETURNING *;";
const char * queryEntities="DELETE FROM tbl_entities WHERE entity_id=%s RETURNING *;";
int delRes=query_db((PGconn *)user_data,&queryEntities,&value);
if(delRes!=0){
//TODO Why this casting?