summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/rest.c122
-rw-r--r--src/rest.h1
2 files changed, 123 insertions, 0 deletions
diff --git a/src/rest.c b/src/rest.c
index f2224bc..b0440ae 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -81,6 +81,7 @@ struct ev_loop *og_loop;
#define OG_REST_PARAM_CENTER (1UL << 43)
#define OG_REST_PARAM_BACKUP (1UL << 44)
#define OG_REST_PARAM_ROOM (1UL << 45)
+#define OG_REST_PARAM_GATEWAY (1UL << 46)
static LIST_HEAD(client_list);
static LIST_HEAD(client_wol_list);
@@ -6076,6 +6077,113 @@ static int og_cmd_post_repository_delete(json_t *element,
return 0;
}
+static int og_cmd_post_room_update(json_t *element,
+ struct og_msg_params *params)
+{
+ struct og_room room = {};
+ 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, "name")) {
+ err = og_json_parse_string_copy(value, room.name,
+ sizeof(room.name));
+ params->flags |= OG_REST_PARAM_NAME;
+ } else if (!strcmp(key, "gateway")) {
+ err = og_json_parse_string_copy(value, room.gateway,
+ sizeof(room.gateway));
+ params->flags |= OG_REST_PARAM_GATEWAY;
+ } else if (!strcmp(key, "netmask")) {
+ err = og_json_parse_string_copy(value, room.netmask,
+ sizeof(room.netmask));
+ params->flags |= OG_REST_PARAM_NETMASK;
+ } else if (!strcmp(key, "id")) {
+ err = og_json_parse_uint(value, &room.id);
+ params->flags |= OG_REST_PARAM_ROOM;
+ }
+
+ if (err < 0)
+ return err;
+ }
+
+ if (!og_msg_params_validate(params, OG_REST_PARAM_ROOM |
+ OG_REST_PARAM_NAME |
+ OG_REST_PARAM_NETMASK |
+ OG_REST_PARAM_GATEWAY))
+ 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,
+ "SELECT idaula, idcentro FROM aulas WHERE idaula='%u'",
+ room.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_next_row(result)) {
+ syslog(LOG_ERR, "could not find a room with that id: %u\n",
+ room.id);
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+ return -1;
+ }
+ room.center = dbi_result_get_uint(result, "idcentro");
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(dbi->conn,
+ "SELECT nombreaula FROM aulas "
+ "WHERE nombreaula='%s' AND idcentro=%d",
+ room.name, room.center);
+
+ 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, "Room with name %s already exists in the "
+ "center with id %d\n",
+ room.name, room.center);
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(dbi->conn,
+ "UPDATE aulas"
+ " SET nombreaula='%s',"
+ " netmask='%s',"
+ " router='%s'"
+ " WHERE idaula='%u';",
+ room.name, room.netmask, room.gateway, room.id);
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR, "failed to update room 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_room_add(json_t *element,
struct og_msg_params *params)
{
@@ -7000,6 +7108,7 @@ struct {
[OG_URI_CENTER_UPDATE] = { "center/update", },
[OG_URI_CENTER_DELETE] = { "center/delete", },
[OG_URI_ROOM_ADD] = { "room/add", },
+ [OG_URI_ROOM_UPDATE] = { "room/update", },
[OG_URI_ROOM_DELETE] = { "room/delete", },
[OG_URI_ROOM_INFO] = { "room/info", },
[OG_URI_PROC_ADD] = { "procedure/add", },
@@ -7694,6 +7803,19 @@ int og_client_state_process_payload_rest(struct og_client *cli)
goto err_process_rest_payload;
}
err = og_cmd_post_room_add(root, &params);
+ } else if (!strncmp(cmd, "room/update",
+ strlen("room/update"))) {
+ if (method != OG_METHOD_POST) {
+ err = og_client_method_not_found(cli);
+ goto err_process_rest_payload;
+ }
+
+ if (!root) {
+ syslog(LOG_ERR, "command task with no payload\n");
+ err = og_client_bad_request(cli);
+ goto err_process_rest_payload;
+ }
+ err = og_cmd_post_room_update(root, &params);
} else if (!strncmp(cmd, "room/delete", strlen("room/delete"))) {
if (method != OG_METHOD_POST) {
err = og_client_method_not_found(cli);
diff --git a/src/rest.h b/src/rest.h
index c939bba..926ac8c 100644
--- a/src/rest.h
+++ b/src/rest.h
@@ -129,6 +129,7 @@ enum og_rest_uri {
OG_URI_CENTER_UPDATE,
OG_URI_CENTER_DELETE,
OG_URI_ROOM_ADD,
+ OG_URI_ROOM_UPDATE,
OG_URI_ROOM_DELETE,
OG_URI_ROOM_INFO,
OG_URI_PROC_ADD,