feat(relations): adjust *relations* endpoint

This commit is contained in:
dancingCycle 2023-04-27 11:33:26 +02:00
parent 1982676a69
commit 009f6d28ed
1 changed files with 69 additions and 7 deletions

View File

@ -349,7 +349,7 @@ int callback_get_entity (const struct _u_request * request, struct _u_response *
}
/**
* Callback function to reply all entities
* Callback function to reply all entities
*/
int callback_get_entities (const struct _u_request * request, struct _u_response * response, void * user_data) {
/*declarations*/
@ -401,12 +401,9 @@ int callback_get_entities (const struct _u_request * request, struct _u_response
for(int i=0;i<nbRecords;i++){
arrayFields=json_array();
for(int j=0;j<nbFields;j++){
/*not password, verification_token_id, created_account*/
if(j!=6&&j!=8&&j!=10){
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
stringField = json_string(PQgetvalue(pqRes,i,j));
json_array_append(arrayFields, stringField);
json_decref(stringField);
}
json_array_append(arrayRecords, arrayFields);
json_decref(arrayFields);
@ -424,6 +421,71 @@ int callback_get_entities (const struct _u_request * request, struct _u_response
return U_CALLBACK_CONTINUE;
}
/**
* Callback function to reply all relations
*/
int callback_get_relations (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_relations() Started...\n");
conn = (PGconn *)user_data;
/*define prepared statement*/
prepStmEntities = "SELECT * FROM relations;";
//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_relations() 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 /relations route: Dba error!");
printf("callback_get_relations() 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_relations() Done.\n");
return U_CALLBACK_CONTINUE;
}
int query_db (const PGconn * conn, const char ** queryStr, const char ** valueStr){
/*declarations*/
int res;