diff --git a/CMakeLists.txt b/CMakeLists.txt index cee46c7..39a60be 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required (VERSION 3.7) project(core - VERSION 0.4.4 + VERSION 0.4.5 LANGUAGES C) add_executable(core src/main.c) diff --git a/emgauwa-core.conf b/emgauwa-core.conf index b6ddf1a..d799d82 100644 --- a/emgauwa-core.conf +++ b/emgauwa-core.conf @@ -1,14 +1,14 @@ [core] database = "emgauwa-core.sqlite" content-dir = "/usr/share/webapps/emgauwa" -include = "./emgauwa-core.conf.d/" +#include = "./emgauwa-core.conf.d/" not-found-file = "404.html" not-found-file-mime = "text/html" not-found-content = "404 - NOT FOUND" not-found-content-type = "text/plain" [ports] -server = 5000 +server = 4419 # 4422 for testing; 4421 for dev-env; 4420 for testing-env; 4419 for prod-env discovery = 4421 # 1886 for testing; 1885 for dev-env; 1884 for testing-env; 1883 for prod-env diff --git a/include/helpers.h b/include/helpers.h index fa2cb30..8b322a7 100644 --- a/include/helpers.h +++ b/include/helpers.h @@ -2,6 +2,7 @@ #define CORE_HELPERS_H #include +#include #include int @@ -13,4 +14,7 @@ helper_get_weekday(const struct tm *time_struct); int helper_drop_privileges(); +char* +find_query_param(struct mg_str query_mg_str, char* search_key); + #endif /* CORE_HELPERS_H */ diff --git a/include/models/macro_action.h b/include/models/macro_action.h index bd61d09..a46ed19 100644 --- a/include/models/macro_action.h +++ b/include/models/macro_action.h @@ -18,6 +18,9 @@ macro_action_delete_for_macro(int macro_id); macro_action_t** macro_action_get_for_macro(int macro_id); +macro_action_t** +macro_action_get_for_macro_and_weekday(int macro_id, int weekday); + int macro_action_execute(macro_action_t *macro_action); diff --git a/src/endpoints/api_v1_macros_STR_execute.c b/src/endpoints/api_v1_macros_STR_execute.c index 411277b..5097d85 100644 --- a/src/endpoints/api_v1_macros_STR_execute.c +++ b/src/endpoints/api_v1_macros_STR_execute.c @@ -12,7 +12,6 @@ void api_v1_macros_STR_execute_PUT(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response) { - (void)hm; (void)nc; uuid_t target_uid; @@ -30,7 +29,29 @@ api_v1_macros_STR_execute_PUT(struct mg_connection *nc, struct http_message *hm, return; } - macro_action_t** macro_actions = macro_action_get_for_macro(macro->id); + char *weekday_str = find_query_param(hm->query_string, "weekday"); + + macro_action_t** macro_actions; + + if (weekday_str != NULL) { + errno = 0; + char *end; + long weekday = strtol(weekday_str, &end, 10); + bool weekday_str_is_end = weekday_str == end; + + free(weekday_str); + + if (errno != 0 || weekday_str_is_end || weekday < 0 || weekday >= 7) { + M_RESPONSE_MSG(LOGGER_DEBUG, response, 400, "the query contains an invalid weekday"); + return; + } + + macro_actions = macro_action_get_for_macro_and_weekday(macro->id, weekday); + } + else { + macro_actions = macro_action_get_for_macro(macro->id); + } + database_transaction_lock lock; database_transaction_begin(&lock); diff --git a/src/helpers/find_query_param.c b/src/helpers/find_query_param.c new file mode 100644 index 0000000..3ae6246 --- /dev/null +++ b/src/helpers/find_query_param.c @@ -0,0 +1,37 @@ +#include + +#include + +char* +find_query_param(struct mg_str query_mg_str, char* search_key) +{ + if (query_mg_str.len == 0) { + return NULL; + } + + // Convert query string to null-terminated string + char* query = malloc(sizeof(char) * (query_mg_str.len + 1)); + strncpy(query, query_mg_str.p, query_mg_str.len); + query[query_mg_str.len] = '\0'; + + char* result = NULL; + + // Tokenize query string + char *token = strtok(query, "&"); + while (token != NULL) { + char *key = strtok(token, "="); + char *val = strtok(NULL, "="); + if (key != NULL && val != NULL) { + if (strcmp(key, search_key) == 0) { + size_t val_len = strlen(val); + result = malloc(sizeof(char) * (val_len + 1)); + strncpy(result, val, val_len + 1); + result[val_len] = '\0'; // Ensure null termination + break; + } + } + token = strtok(NULL, "&"); + } + free(query); + return result; +} \ No newline at end of file diff --git a/src/models/macro_action.c b/src/models/macro_action.c index da4c82d..e603c00 100644 --- a/src/models/macro_action.c +++ b/src/models/macro_action.c @@ -121,6 +121,19 @@ macro_action_get_for_macro(int macro_id) return macro_action_db_select(stmt); } +macro_action_t** +macro_action_get_for_macro_and_weekday(int macro_id, int weekday) +{ + LOGGER_DEBUG("getting macro_actions [macro_id=%d weekday=%d] from database\n", macro_id, weekday); + sqlite3_stmt *stmt; + + sqlite3_prepare_v2(global_database, "SELECT * FROM macro_actions WHERE macro_id=? AND weekday=?", -1, &stmt, NULL); + sqlite3_bind_int(stmt, 1, macro_id); + sqlite3_bind_int(stmt, 2, weekday); + + return macro_action_db_select(stmt); +} + int macro_action_execute(macro_action_t *macro_action) {