diff options
author | Javier Hernandez <jhernandez@soleta.eu> | 2024-01-09 11:57:29 +0100 |
---|---|---|
committer | OpenGnSys Support Team <soporte-og@soleta.eu> | 2024-01-09 12:57:28 +0100 |
commit | 1fb425694c6d9fbf5b9c0ad7282a68fa588fa506 (patch) | |
tree | 85bf5b5197cc69473f2b9c9c4431b7eef3404586 /src/rest.c | |
parent | 28ab2aa9b0fe22c4d4658f37799e2735b7370ef2 (diff) |
rest: Add support for adding folders
Add support for adding computer folders and room folders.
Diffstat (limited to 'src/rest.c')
-rw-r--r-- | src/rest.c | 151 |
1 files changed, 151 insertions, 0 deletions
@@ -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); |