From 0f7d1a4b9af83354612cd13c3a4e6004fbcf82c3 Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Mon, 3 Apr 2023 15:11:04 +0200 Subject: [PATCH] feat(http-post-update): adjust HTTP POST request --- http-post-update/main.c | 164 ++++++++++++++++++++++++++++++++++------ readme.md | 5 +- 2 files changed, 145 insertions(+), 24 deletions(-) diff --git a/http-post-update/main.c b/http-post-update/main.c index e9bb16a..5f6dbe6 100644 --- a/http-post-update/main.c +++ b/http-post-update/main.c @@ -343,7 +343,7 @@ int callback_get_entity (const struct _u_request * request, struct _u_response * } /** - * Callback function for route /entities 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*/ @@ -410,8 +410,128 @@ int callback_get_entities (const struct _u_request * request, struct _u_response return U_CALLBACK_CONTINUE; } -int callback_delete_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) {}; +/** + * Callback function to delete a particular entity + */ +int callback_delete_entity (const struct _u_request * request, struct _u_response * response, void * user_data) { +}; + +/** + * Callback function to update a particular entity + */ +int callback_update_entity (const struct _u_request * request, struct _u_response * response, void * user_data) { + /*declarations*/ + int r; + const char **keys=NULL; + const char **keysUrl=NULL; + const char *value=NULL; + const char *valueUrl=NULL; + char *line=NULL; + int len; + int i; + PGconn *conn=NULL; + char *prepStmEntities=NULL; + PGresult *pqRes=NULL; + + printf("callback_update_entity() Started...\n"); + + //parse url + if(request->map_url != NULL){ + keysUrl=u_map_enum_keys(request->map_url); + if(keysUrl[0] == NULL){ + //TODO error + printf("callback_update_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/update route: Param error!"); + }else{ + valueUrl = u_map_get(request->map_url, keysUrl[0]); + //printf("callback_update_entity() URL: key is %s, value is %s\n", keysUrl[0], valueUrl); + + //parse body + if(request->map_post_body != NULL){ + keys=u_map_enum_keys(request->map_post_body); + if(keys[0] == NULL){ + //TODO error + printf("callback_update_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/update route: HTTP POST data error!"); + }else{ + //printf("callback_update_entity() param available\n"); + value = u_map_get(request->map_post_body, keys[0]); + //printf("callback_update_entity() BODY: key is %s, value is %s\n", keys[0], value); + if(value==NULL){ + //TODO error + printf("callback_update_entity() key NOT available in request map\n"); + //TODO Why this casting? + (void)(request); + (void)(user_data); + ulfius_set_string_body_response(response, 500, "This is the /entity/:id/update route: HTTP POST key error!"); + }else{ + //printf("callback_update_entity() key is %s, value is %s\n", keys[0], value); + conn = (PGconn *)user_data; + /*define prepared statement*/ + len=snprintf(NULL,0, + "UPDATE entities SET name='%s' WHERE id=%s RETURNING *;" + ,value,valueUrl); + prepStmEntities=malloc((size_t)(len+1)); + //TODO Release prepStmEntities! + snprintf(prepStmEntities,(size_t)(len+1), + "UPDATE entities SET name='%s' WHERE id=%s RETURNING *;" + ,value,valueUrl); + //printf("callback_update_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("callback_update_entity() No data retrieved\n"); + ulfius_set_string_body_response(response, 500, "This is the /entity/:id/update route: Dba error!"); + /*clean up result*/ + PQclear(pqRes); + //TODO Why this casting? + }else{ + /*evaluate result*/ + int nbRecords=PQntuples(pqRes); + //printf("callback_update_entity() nbRecords: %d\n",nbRecords); + /*clean up result*/ + PQclear(pqRes); + } + } + } + }else{ + printf("callback_update_entity() HTTP POST request error!\n"); + ulfius_set_string_body_response(response, 400, "This is the /entity/:id/update route: HTTP POST request error!"); + } + } + }else{ + printf("callback_update_entity() HTTP POST request error!\n"); + ulfius_set_string_body_response(response, 400, "This is the /entity/:id/update route: HTTP POST request error!"); + } + + /*clean up remainings*/ + printf("callback_update_entity() Done.\n"); + //TODO Why this casting? + (void)(request); + (void)(user_data); + ulfius_set_string_body_response(response, 200, "Done.\n"); + return U_CALLBACK_CONTINUE; +}; + +/** + * Callback function to create a particular entity + */ int callback_create_entity (const struct _u_request * request, struct _u_response * response, void * user_data) { /*declarations*/ int r; @@ -445,7 +565,7 @@ int callback_create_entity (const struct _u_request * request, struct _u_respons (void)(user_data); ulfius_set_string_body_response(response, 500, "This is the /entity/create route: HTTP POST key error!"); }else{ - printf("callback_create_entity() key is %s, value is %s\n", keys[0], value); + //printf("callback_create_entity() key is %s, value is %s\n", keys[0], value); conn = (PGconn *)user_data; /*define prepared statement*/ len=snprintf(NULL,0, @@ -456,29 +576,29 @@ int callback_create_entity (const struct _u_request * request, struct _u_respons snprintf(prepStmEntities,(size_t)(len+1), "INSERT INTO entities (name) VALUES ('%s') RETURNING *;" ,value); - printf("callback_create_entity() prepStmEntities: %s\n",prepStmEntities); + //printf("callback_create_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? + /*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("callback_create_entity() No data retrieved\n"); - ulfius_set_string_body_response(response, 500, "This is the /entity/create route: Dba error!"); - /*clean up result*/ - PQclear(pqRes); - //TODO Why this casting? + if(PQresultStatus(pqRes) != PGRES_TUPLES_OK){ + /*handle error*/ + printf("callback_create_entity() No data retrieved\n"); + ulfius_set_string_body_response(response, 500, "This is the /entity/create route: Dba error!"); + /*clean up result*/ + PQclear(pqRes); + //TODO Why this casting? }else{ /*evaluate result*/ int nbRecords=PQntuples(pqRes); - printf("callback_get_entity() nbRecords: %d\n",nbRecords); + //printf("callback_create_entity() nbRecords: %d\n",nbRecords); /*clean up result*/ PQclear(pqRes); } diff --git a/readme.md b/readme.md index dfbcdea..5aa1e2b 100644 --- a/readme.md +++ b/readme.md @@ -21,7 +21,7 @@ curl -X POST --data "name=Hi from curl!" http://:/entities/create * read all ``` -curl http://:/entities/info +curl -X GET http://:/entities/info ``` * read single @@ -32,9 +32,10 @@ curl http://:/entities/35/info * update ``` curl --data "name=Hi from curl!" http://:/entities/1/update +curl -X POST --data "name=Update!" http://:/entities/1/update ``` * delete ``` -curl --request "DELETE" http://localhost:/entities/1/delete +curl -X DELETE http://localhost:/entities/1/delete ```