diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/dbi.h | 7 | ||||
-rw-r--r-- | src/rest.c | 151 |
2 files changed, 158 insertions, 0 deletions
@@ -24,6 +24,7 @@ void og_dbi_close(struct og_dbi *db); #define OG_DB_COMPUTER_NAME_MAXLEN 100 #define OG_DB_CENTER_NAME_MAXLEN 100 #define OG_DB_ROOM_NAME_MAXLEN 100 +#define OG_DB_FOLDER_NAME_MAXLEN 100 #define OG_DB_ROOM_LOC_MAXLEN 255 #define OG_DB_SERIAL_NUMBER_MAXLEN 25 #define OG_DB_IMAGE_DESCRIPTION_MAXLEN 250 @@ -108,6 +109,12 @@ struct og_room { bool remote; }; +struct og_folder { + unsigned int room; + unsigned int center; + char name[OG_DB_FOLDER_NAME_MAXLEN + 1]; +}; + struct og_repository { unsigned int center; char name[OG_DB_REPO_NAME_MAXLEN]; @@ -80,6 +80,7 @@ struct ev_loop *og_loop; #define OG_REST_PARAM_MODE (1UL << 42) #define OG_REST_PARAM_CENTER (1UL << 43) #define OG_REST_PARAM_BACKUP (1UL << 44) +#define OG_REST_PARAM_ROOM (1UL << 45) static LIST_HEAD(client_list); static LIST_HEAD(client_wol_list); @@ -1859,6 +1860,141 @@ static int og_cmd_post_client_update(json_t *element, return 0; } +static int add_room_folder(struct og_dbi *dbi, struct og_folder *folder) +{ + dbi_result result; + const char *msglog; + + result = dbi_conn_queryf(dbi->conn, + "SELECT nombregrupo FROM grupos WHERE nombregrupo='%s'", + folder->name); + + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + if (dbi_result_get_numrows(result) > 0) { + syslog(LOG_ERR, "room-folder with that name already exists: %s\n", + folder->name); + dbi_result_free(result); + return -1; + } + dbi_result_free(result); + result = dbi_conn_queryf(dbi->conn, + "INSERT INTO grupos(" + " nombregrupo," + " grupoid," + " tipo," + " idcentro," + " iduniversidad" + ") VALUES ('%s', 0, 2, %u, 0)", + folder->name, folder->center); + + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to add room-folder to database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + dbi_result_free(result); + + return 0; +} + +static int add_computer_folder(struct og_dbi *dbi, struct og_folder *folder) +{ + dbi_result result; + const char *msglog; + + result = dbi_conn_queryf(dbi->conn, + "SELECT nombregrupoordenador FROM gruposordenadores WHERE nombregrupoordenador='%s'", + folder->name); + + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to query database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + if (dbi_result_get_numrows(result) > 0) { + syslog(LOG_ERR, "computer-folder with that name already exists: %s\n", + folder->name); + dbi_result_free(result); + return -1; + } + dbi_result_free(result); + result = dbi_conn_queryf(dbi->conn, + "INSERT INTO gruposordenadores(" + " nombregrupoordenador," + " idaula," + " grupoid" + ") VALUES ('%s', %u, 0)", + folder->name, folder->room); + + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to add computer-folder to database (%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + + dbi_result_free(result); + return 0; +} + +static int og_cmd_post_folder_add(json_t *element, + struct og_msg_params *params) +{ + const char *key; + json_t *value; + struct og_folder folder = {}; + struct og_dbi *dbi; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "center")) { + params->flags |= OG_REST_PARAM_CENTER; + err = og_json_parse_uint(value, &folder.center); + } else if (!strcmp(key, "room")) { + params->flags |= OG_REST_PARAM_ROOM; + err = og_json_parse_uint(value, &folder.room); + } else if (!strcmp(key, "name")) { + params->flags |= OG_REST_PARAM_NAME; + err = og_json_parse_string_copy(value, + folder.name, + sizeof(folder.name)); + } + + if (err < 0) + return err; + } + if (!og_msg_params_validate(params, OG_REST_PARAM_NAME)) + return -1; + if (og_msg_params_validate(params, OG_REST_PARAM_ROOM | OG_REST_PARAM_CENTER)) + return -1; + if (!og_msg_params_validate(params, OG_REST_PARAM_ROOM) && !og_msg_params_validate(params, OG_REST_PARAM_CENTER)) + return -1; + + dbi = og_dbi_open(&ogconfig.db); + if (!dbi) { + syslog(LOG_ERR, "cannot open conection database (%s:%d)\n", + __func__, __LINE__); + return -1; + } + if (params->flags & OG_REST_PARAM_ROOM) + err = add_computer_folder(dbi, &folder); + else if (params->flags & OG_REST_PARAM_CENTER) + err = add_room_folder(dbi, &folder); + else + err = -1; + + og_dbi_close(dbi); + + return err; +} + static int og_cmd_post_client_add(json_t *element, struct og_msg_params *params, char *buffer_reply) @@ -6854,6 +6990,21 @@ int og_client_state_process_payload_rest(struct og_client *cli) } err = og_cmd_post_client_delete(root, ¶ms); + } else if (!strncmp(cmd, "folder/add", strlen("folder/add"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, + "command folder add with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + + err = og_cmd_post_folder_add(root, ¶ms); + } else if (!strncmp(cmd, "wol", strlen("wol"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); |