summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/rest.c90
-rw-r--r--tests/units/test_0033_post_procedure_add.py39
2 files changed, 129 insertions, 0 deletions
diff --git a/src/rest.c b/src/rest.c
index 9fa1772..e9eb7ca 100644
--- a/src/rest.c
+++ b/src/rest.c
@@ -4050,6 +4050,83 @@ static int og_cmd_post_center_delete(json_t *element,
return 0;
}
+static int og_cmd_post_procedure_add(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, "center")) {
+ err = og_json_parse_string(value, &params->id);
+ params->flags |= OG_REST_PARAM_ID;
+ } else if (!strcmp(key, "name")) {
+ err = og_json_parse_string(value, &params->name);
+ params->flags |= OG_REST_PARAM_NAME;
+ } else if (!strcmp(key, "description"))
+ err = og_json_parse_string(value, &params->comment);
+
+ if (err < 0)
+ return err;
+ }
+
+ if (!og_msg_params_validate(params, OG_REST_PARAM_ID |
+ OG_REST_PARAM_NAME))
+ 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 descripcion FROM procedimientos "
+ "WHERE descripcion='%s' AND idcentro=%s",
+ params->name, 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;
+ }
+
+ if (dbi_result_get_numrows(result) > 0) {
+ syslog(LOG_ERR, "Procedure with name %s already exists in the "
+ "center with id %s\n",
+ params->name, params->id);
+ dbi_result_free(result);
+ og_dbi_close(dbi);
+ return -1;
+ }
+ dbi_result_free(result);
+
+ result = dbi_conn_queryf(dbi->conn,
+ "INSERT INTO procedimientos("
+ "idcentro, descripcion, comentarios) "
+ "VALUES (%s, '%s', '%s')",
+ params->id, params->name, params->comment);
+
+ if (!result) {
+ dbi_conn_error(dbi->conn, &msglog);
+ syslog(LOG_ERR,
+ "failed to add procedure to 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_cmd_post_room_add(json_t *element,
struct og_msg_params *params)
{
@@ -4733,6 +4810,19 @@ int og_client_state_process_payload_rest(struct og_client *cli)
goto err_process_rest_payload;
}
err = og_cmd_post_room_delete(root, &params);
+ } else if (!strncmp(cmd, "procedure/add", strlen("procedure/add"))) {
+ if (method != OG_METHOD_POST) {
+ err = og_client_method_not_found(cli);
+ goto err_process_rest_payload;
+ }
+
+ if (!root) {
+ syslog(LOG_ERR,
+ "command procedure add with no payload\n");
+ err = og_client_bad_request(cli);
+ goto err_process_rest_payload;
+ }
+ err = og_cmd_post_procedure_add(root, &params);
} else {
syslog(LOG_ERR, "unknown command: %.32s ...\n", cmd);
err = og_client_not_found(cli);
diff --git a/tests/units/test_0033_post_procedure_add.py b/tests/units/test_0033_post_procedure_add.py
new file mode 100644
index 0000000..b31ac70
--- /dev/null
+++ b/tests/units/test_0033_post_procedure_add.py
@@ -0,0 +1,39 @@
+import requests
+import unittest
+
+class TestPostProcedureAddMethods(unittest.TestCase):
+
+ def setUp(self):
+ self.url = 'http://localhost:8888/procedure/add'
+ self.headers = {'Authorization' : '07b3bfe728954619b58f0107ad73acc1'}
+ self.full_json = { "center": "1",
+ "name": "procedure1",
+ "description": "procedure test" }
+ self.minimal_json = { "center": "1",
+ "name": "procedure2" }
+ self.duplicated_procedure_json = { "center": "1",
+ "name": "repeated_procedure" }
+
+ def test_post(self):
+ returned = requests.post(self.url, headers=self.headers,
+ json=self.full_json)
+ self.assertEqual(returned.status_code, 200)
+
+ def test_post_only_required_fields(self):
+ returned = requests.post(self.url, headers=self.headers,
+ json=self.minimal_json)
+ self.assertEqual(returned.status_code, 200)
+
+ def test_post_duplicated_procedure(self):
+ requests.post(self.url, headers=self.headers,
+ json=self.duplicated_procedure_json)
+ returned = requests.post(self.url, headers=self.headers,
+ json=self.duplicated_procedure_json)
+ self.assertEqual(returned.status_code, 400)
+
+ def test_get(self):
+ returned = requests.get(self.url, headers=self.headers)
+ self.assertEqual(returned.status_code, 405)
+
+if __name__ == '__main__':
+ unittest.main()