diff options
author | Javier Sánchez Parra <jsanchez@soleta.eu> | 2021-05-10 11:46:34 +0200 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2021-05-10 12:07:53 +0200 |
commit | b48707333ff46c14e2c6d1e438d6a830b5526f1a (patch) | |
tree | ef2dbc91246dc9f8176a962628428b5cd4a4fe07 /src/rest.c | |
parent | d3e9b5727ae28885f1af1c8bd5a40a1ed238120d (diff) |
#942 Add REST POST /center/delete method
This method deletes a center from the DB and deletes on cascade
rooms/labs, computers and computers partitions.
Note: if the center id do not exists in the database, ogserver still
tries to delete it and replies with 200 OK.
Request:
POST /center/delete
{
"id": "1"
}
Response:
200 OK
Diffstat (limited to 'src/rest.c')
-rw-r--r-- | src/rest.c | 59 |
1 files changed, 59 insertions, 0 deletions
@@ -4004,6 +4004,52 @@ static int og_cmd_post_center_add(json_t *element, return 0; } +static int og_cmd_post_center_delete(json_t *element, + struct og_msg_params *params) +{ + const char *key, *msglog; + struct og_dbi *dbi; + dbi_result result; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "id")) { + err = og_json_parse_string(value, ¶ms->id); + params->flags |= OG_REST_PARAM_ID; + } + if (err < 0) + return err; + } + + if (!og_msg_params_validate(params, OG_REST_PARAM_ID)) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + + result = dbi_conn_queryf(dbi->conn, + "DELETE FROM centros WHERE idcentro=%s", + params->id); + + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + og_dbi_close(dbi); + return -1; + } + + dbi_result_free(result); + + og_dbi_close(dbi); + return 0; +} + static int og_cmd_post_room_add(json_t *element, struct og_msg_params *params) { @@ -4602,6 +4648,19 @@ int og_client_state_process_payload_rest(struct og_client *cli) } err = og_cmd_post_center_add(root, ¶ms, buf_reply); + } else if (!strncmp(cmd, "center/delete", strlen("center/delete"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, + "command center delete with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + err = og_cmd_post_center_delete(root, ¶ms); } else if (!strncmp(cmd, "room/add", strlen("room/add"))) { if (method != OG_METHOD_POST) { |