diff options
Diffstat (limited to 'src/rest.c')
-rw-r--r-- | src/rest.c | 107 |
1 files changed, 107 insertions, 0 deletions
@@ -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) { |