feat(http-post-update): adjust HTTP POST request

This commit is contained in:
dancingCycle 2023-04-03 15:11:04 +02:00
parent cfd9dfef5a
commit 0f7d1a4b9a
2 changed files with 145 additions and 24 deletions

View File

@ -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);
}

View File

@ -21,7 +21,7 @@ curl -X POST --data "name=Hi from curl!" http://<host>:<port>/entities/create
* read all
```
curl http://<host>:<port>/entities/info
curl -X GET http://<host>:<port>/entities/info
```
* read single
@ -32,9 +32,10 @@ curl http://<host>:<port>/entities/35/info
* update
```
curl --data "name=Hi from curl!" http://<host>:<port>/entities/1/update
curl -X POST --data "name=Update!" http://<host>:<port>/entities/1/update
```
* delete
```
curl --request "DELETE" http://localhost:<port>/entities/1/delete
curl -X DELETE http://localhost:<port>/entities/1/delete
```