From 5e2b562330c63b8dbd1cea5aada5aa9b86a7f31a Mon Sep 17 00:00:00 2001 From: Javier Hernandez Date: Wed, 31 Jan 2024 13:10:15 +0100 Subject: rest: Add uri to update center Add uri to allow the change of a center's properties such as 'name' or 'comment' Refuse to update center's name if that name is already in use by another center. This is because a center should not share name with another one. --- src/rest.c | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/rest.h | 1 + 2 files changed, 108 insertions(+) (limited to 'src') diff --git a/src/rest.c b/src/rest.c index 8ba9837..f2224bc 100644 --- a/src/rest.c +++ b/src/rest.c @@ -1733,6 +1733,99 @@ static int og_cmd_get_client_info(json_t *element, return 0; } +static int og_cmd_post_center_update(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; + unsigned int center_id; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "name")) { + err = og_json_parse_string(value, ¶ms->name); + params->flags |= OG_REST_PARAM_NAME; + } else if (!strcmp(key, "comment")) { + err = og_json_parse_string(value, ¶ms->comment); + } else if (!strcmp(key, "id")) { + err = og_json_parse_uint(value, ¢er_id); + params->flags |= OG_REST_PARAM_CENTER; + } + + if (err < 0) + return err; + } + + if (!og_msg_params_validate(params, OG_REST_PARAM_CENTER | OG_REST_PARAM_NAME)) + return -1; + if (!params->comment) + params->comment = ""; + + 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, + "SELECT idcentro FROM centros WHERE idcentro='%u'", + center_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; + } + if (dbi_result_get_numrows(result) == 0) { + syslog(LOG_ERR, "could not find a center with that id: %u\n", + center_id); + dbi_result_free(result); + og_dbi_close(dbi); + return -1; + } + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "SELECT nombrecentro FROM centros WHERE nombrecentro='%s'", + params->name); + 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; + } + if (dbi_result_get_numrows(result) != 0) { + syslog(LOG_ERR, "a center with that name already exists: %s\n", + params->name); + dbi_result_free(result); + og_dbi_close(dbi); + return -1; + } + dbi_result_free(result); + + result = dbi_conn_queryf(dbi->conn, + "UPDATE centros" + " SET nombrecentro='%s'," + " comentarios='%s'" + " WHERE idcentro='%u';", + params->name, params->comment, center_id); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to update center in database (%s:%d) %s\n", + __func__, __LINE__, msglog); + og_dbi_close(dbi); + return -1; + } + dbi_result_free(result); + + return 0; +} + static int og_cmd_post_client_update(json_t *element, struct og_msg_params *params, char *buffer_reply) @@ -6904,6 +6997,7 @@ struct { [OG_URI_OGLIVE_LIST] = { "oglive/list", }, [OG_URI_OGLIVE_SET] = { "oglive/set", }, [OG_URI_CENTER_ADD] = { "center/add", }, + [OG_URI_CENTER_UPDATE] = { "center/update", }, [OG_URI_CENTER_DELETE] = { "center/delete", }, [OG_URI_ROOM_ADD] = { "room/add", }, [OG_URI_ROOM_DELETE] = { "room/delete", }, @@ -7574,6 +7668,19 @@ int og_client_state_process_payload_rest(struct og_client *cli) goto err_process_rest_payload; } err = og_cmd_post_center_delete(root, ¶ms); + } else if (!strncmp(cmd, "center/update", strlen("center/update"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, + "command center update with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + err = og_cmd_post_center_update(root, ¶ms); } else if (!strncmp(cmd, "room/add", strlen("room/add"))) { if (method != OG_METHOD_POST) { diff --git a/src/rest.h b/src/rest.h index faa380f..c939bba 100644 --- a/src/rest.h +++ b/src/rest.h @@ -126,6 +126,7 @@ enum og_rest_uri { OG_URI_OGLIVE_LIST, OG_URI_OGLIVE_SET, OG_URI_CENTER_ADD, + OG_URI_CENTER_UPDATE, OG_URI_CENTER_DELETE, OG_URI_ROOM_ADD, OG_URI_ROOM_DELETE, -- cgit v1.2.3-18-g5258