feat(http-delete): adjust HTTP DELETE request
This commit is contained in:
parent
9a6fba3832
commit
e1832cd0f3
|
@ -401,10 +401,111 @@ int callback_get_entities (const struct _u_request * request, struct _u_response
|
||||||
return U_CALLBACK_CONTINUE;
|
return U_CALLBACK_CONTINUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int delete_entity (const PGconn * conn,const char * query,const char *value;){
|
||||||
|
/*declarations*/
|
||||||
|
int res;
|
||||||
|
PGresult *pqRes;
|
||||||
|
char *prepStmEntities;
|
||||||
|
/*define prepared statement*/
|
||||||
|
len=snprintf(NULL,0,query,value);
|
||||||
|
prepStmEntities=malloc((size_t)(len+1));
|
||||||
|
//TODO Release prepStmEntities!
|
||||||
|
snprintf(prepStmEntities,(size_t)(len+1),query,value);
|
||||||
|
printf("delete_entity() prepStmEntities: %s\n",prepStmEntities);
|
||||||
|
/*create prepared statement*/
|
||||||
|
/*conn:connection*/
|
||||||
|
/*stm:statement*/
|
||||||
|
/*0:number of passed parameters*/
|
||||||
|
/*NULL: server figures out parameter type*/
|
||||||
|
/*paramValues:pointer of an array of strings containing parameters*/
|
||||||
|
/*NULL:relevant for binary parameters*/
|
||||||
|
/*NULL:relevant for binary parameters*/
|
||||||
|
/*0:obtain result in text format*/
|
||||||
|
//TODO How to pass params?
|
||||||
|
pqRes = PQexecParams(conn, prepStmEntities, 0, NULL, NULL, NULL, NULL, 0);
|
||||||
|
if(PQresultStatus(pqRes) != PGRES_TUPLES_OK) {
|
||||||
|
/*handle error*/
|
||||||
|
printf("delete_entity() No data retrieved\n");
|
||||||
|
/*clean up result*/
|
||||||
|
PQclear(pqRes);
|
||||||
|
res=1;
|
||||||
|
}else{
|
||||||
|
/*evaluate result*/
|
||||||
|
nbRecords = PQntuples(pqRes);
|
||||||
|
printf("callback_delete_entity() nbRecords: %d\n",nbRecords);
|
||||||
|
/*clean up result*/
|
||||||
|
PQclear(pqRes);
|
||||||
|
res=0;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Callback function to delete a particular entity
|
* Callback function to delete a particular entity
|
||||||
*/
|
*/
|
||||||
int callback_delete_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) {
|
||||||
|
/*declarations*/
|
||||||
|
char *url_params;
|
||||||
|
char *response_body;
|
||||||
|
int r;
|
||||||
|
const char **keys;
|
||||||
|
const char *value;
|
||||||
|
char *line;
|
||||||
|
int len;
|
||||||
|
int i;
|
||||||
|
PGconn *conn;
|
||||||
|
int nbRecords,nbFields;
|
||||||
|
json_t *arrayRecords, *arrayFields, *stringField;
|
||||||
|
|
||||||
|
printf("callback_delete_entity() Started...\n");
|
||||||
|
url_params = print_map(request->map_url);
|
||||||
|
r=asprintf(
|
||||||
|
&response_body,
|
||||||
|
"parameters from the url are \n%s\n\n",
|
||||||
|
url_params);
|
||||||
|
//printf("callback_delete_entity() %d characters generated\n",r);
|
||||||
|
|
||||||
|
if(request->map_url != NULL){
|
||||||
|
keys=u_map_enum_keys(request->map_url);
|
||||||
|
if(keys[0] == NULL){
|
||||||
|
//TODO error
|
||||||
|
printf("callback_delete_entity() param NOT available\n");
|
||||||
|
//TODO Why this casting?
|
||||||
|
(void)(request);
|
||||||
|
(void)(user_data);
|
||||||
|
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/delete route: Param error!");
|
||||||
|
}else{
|
||||||
|
value = u_map_get(request->map_url, keys[0]);
|
||||||
|
printf("callback_delete_entity() key is %s, value is %s\n", keys[0], value);
|
||||||
|
|
||||||
|
conn = (PGconn *)user_data;
|
||||||
|
|
||||||
|
//TODO delete from relation
|
||||||
|
int delRes=delete_entity (conn,"DELETE FROM relations WHERE from_entity=1 RETURNING *;");
|
||||||
|
if(delRes!=0){
|
||||||
|
//TODO Why this casting?
|
||||||
|
(void)(request);
|
||||||
|
(void)(user_data);
|
||||||
|
ulfius_set_string_body_response(response, 500, "This is the /entity/:id/delete route: Dba from_entity error!");
|
||||||
|
}
|
||||||
|
//TODO delete to relation
|
||||||
|
delRes=delete_entity (conn,"DELETE FROM relations WHERE to_entity=1 RETURNING *;");
|
||||||
|
//TODO delete entity
|
||||||
|
delRes=delete_entity (conn,"DELETE FROM entities WHERE id=1 RETURNING *;");
|
||||||
|
/*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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/*clean up remainings*/
|
||||||
|
free(url_params);
|
||||||
|
free(response_body);
|
||||||
|
printf("callback_delete_entity() Done.\n");
|
||||||
|
return U_CALLBACK_CONTINUE;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in New Issue