From bc787a9a68de88cd534f8cb8541ecec0861a2053 Mon Sep 17 00:00:00 2001 From: Javier Hernandez Date: Wed, 10 Jan 2024 11:47:51 +0100 Subject: rest: Add support for deleting folders Add support for deleting folders containing rooms. Delete rooms that are in folder. Computers inside rooms are automatically deleted in cascade when room is deleted (implemented in sql) Add support for deleting folders containing clients. Delete clients that are in folder. --- src/rest.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) (limited to 'src') diff --git a/src/rest.c b/src/rest.c index 51121fc..bf2e1e5 100644 --- a/src/rest.c +++ b/src/rest.c @@ -1995,6 +1995,123 @@ static int og_cmd_post_folder_add(json_t *element, return err; } +static int og_delete_computer_folder(struct og_dbi *dbi, uint32_t folder_id) +{ + const char *msglog; + dbi_result result; + + folder_id = folder_id & 0x0000ffff; + + result = dbi_conn_queryf(dbi->conn, + "DELETE FROM gruposordenadores " + "WHERE idgrupo=%u", + folder_id); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to delete computer-group from database " + "(%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + if (dbi_result_get_numrows_affected(result) < 1) { + syslog(LOG_ERR, "delete did not modify any row (%s:%d)\n", + __func__, __LINE__); + dbi_result_free(result); + return -1; + } + + result = dbi_conn_queryf(dbi->conn, + "DELETE FROM ordenadores " + "WHERE grupoid=%u", + folder_id); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to delete computers from database " + "(%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + dbi_result_free(result); + + return 0; +} + +static int og_delete_room_folder(struct og_dbi *dbi, uint32_t folder_id) +{ + const char *msglog; + dbi_result result; + result = dbi_conn_queryf(dbi->conn, + "DELETE FROM grupos " + "WHERE idgrupo=%u", + folder_id); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to delete room-group from database " + "(%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + if (dbi_result_get_numrows_affected(result) < 1) { + syslog(LOG_ERR, "delete did not modify any row (%s:%d)\n", + __func__, __LINE__); + dbi_result_free(result); + return -1; + } + dbi_result_free(result); + result = dbi_conn_queryf(dbi->conn, + "DELETE FROM aulas " + "WHERE grupoid=%u", + folder_id); + if (!result) { + dbi_conn_error(dbi->conn, &msglog); + syslog(LOG_ERR, "failed to delete rooms from database " + "(%s:%d) %s\n", + __func__, __LINE__, msglog); + return -1; + } + dbi_result_free(result); + + return 0; +} + +static int og_cmd_post_folder_delete(json_t *element, + struct og_msg_params *params) +{ + struct og_dbi *dbi; + uint32_t folder_id; + const char *key; + json_t *value; + int err = 0; + + json_object_foreach(element, key, value) { + if (!strcmp(key, "id")) { + err = og_json_parse_uint(value, &folder_id); + params->flags |= OG_REST_PARAM_ID; + } + if (err < 0) + return err; + } + + if (!og_msg_params_validate(params, OG_REST_PARAM_ID)) + 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 (folder_id & 0x00010000) { + err = og_delete_computer_folder(dbi, folder_id); + } else { + err = og_delete_room_folder(dbi, folder_id); + } + + og_dbi_close(dbi); + return err; +} + static int og_cmd_post_client_add(json_t *element, struct og_msg_params *params, char *buffer_reply) @@ -7005,6 +7122,21 @@ int og_client_state_process_payload_rest(struct og_client *cli) err = og_cmd_post_folder_add(root, ¶ms); + } else if (!strncmp(cmd, "folder/delete", strlen("folder/delete"))) { + if (method != OG_METHOD_POST) { + err = og_client_method_not_found(cli); + goto err_process_rest_payload; + } + + if (!root) { + syslog(LOG_ERR, + "command folder delete with no payload\n"); + err = og_client_bad_request(cli); + goto err_process_rest_payload; + } + + err = og_cmd_post_folder_delete(root, ¶ms); + } else if (!strncmp(cmd, "wol", strlen("wol"))) { if (method != OG_METHOD_POST) { err = og_client_method_not_found(cli); -- cgit v1.2.3-18-g5258