summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJavier Sánchez Parra <jsanchez@soleta.eu>2021-05-10 13:02:48 +0200
committerOpenGnSys Support Team <soporte-og@soleta.eu>2021-05-10 13:39:38 +0200
commitbc9424724d5e8a2b3f252005a99fe9adf003cfcd (patch)
treeeebfba0d7baf308a9f1c9557df0f1495d31511a6
parentb48707333ff46c14e2c6d1e438d6a830b5526f1a (diff)
Add REST POST /room/delete method
This method deletes a room (lab) from the DB and deletes on cascade computers and computers partitions. Note: if the room id do not exists in the database, ogserver still tries to delete it and replies with 200 OK. Request: POST /room/delete { "id": "1" } Response: 200 OK
-rw-r--r--src/rest.c59
1 files changed, 59 insertions, 0 deletions
diff --git a/src/rest.c b/src/rest.c
index d5e5aca..9fa1772 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -4159,6 +4159,52 @@ static int og_cmd_post_room_add(json_t *element,
return 0;
}
+static int og_cmd_post_room_delete(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;
+
+ json_object_foreach(element, key, value) {
+ if (!strcmp(key, "id")) {
+ err = og_json_parse_string(value, &params->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;
+ }
+
+ result = dbi_conn_queryf(dbi->conn,
+ "DELETE FROM aulas WHERE idaula=%s",
+ params->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;
+ }
+
+ dbi_result_free(result);
+
+ og_dbi_close(dbi);
+ return 0;
+}
+
static int og_client_method_not_found(struct og_client *cli)
{
/* To meet RFC 7231, this function MUST generate an Allow header field
@@ -4674,6 +4720,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/delete", strlen("room/delete"))) {
+ if (method != OG_METHOD_POST) {
+ err = og_client_method_not_found(cli);
+ goto err_process_rest_payload;
+ }
+
+ if (!root) {
+ syslog(LOG_ERR,
+ "command room delete with no payload\n");
+ err = og_client_bad_request(cli);
+ goto err_process_rest_payload;
+ }
+ err = og_cmd_post_room_delete(root, &params);
} else {
syslog(LOG_ERR, "unknown command: %.32s ...\n", cmd);
err = og_client_not_found(cli);