Add weekday query parameter for macro execute
This commit is contained in:
parent
ea6b653121
commit
173b9d41e8
7 changed files with 83 additions and 5 deletions
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project(core
|
project(core
|
||||||
VERSION 0.4.4
|
VERSION 0.4.5
|
||||||
LANGUAGES C)
|
LANGUAGES C)
|
||||||
|
|
||||||
add_executable(core src/main.c)
|
add_executable(core src/main.c)
|
||||||
|
|
|
@ -1,14 +1,14 @@
|
||||||
[core]
|
[core]
|
||||||
database = "emgauwa-core.sqlite"
|
database = "emgauwa-core.sqlite"
|
||||||
content-dir = "/usr/share/webapps/emgauwa"
|
content-dir = "/usr/share/webapps/emgauwa"
|
||||||
include = "./emgauwa-core.conf.d/"
|
#include = "./emgauwa-core.conf.d/"
|
||||||
not-found-file = "404.html"
|
not-found-file = "404.html"
|
||||||
not-found-file-mime = "text/html"
|
not-found-file-mime = "text/html"
|
||||||
not-found-content = "404 - NOT FOUND"
|
not-found-content = "404 - NOT FOUND"
|
||||||
not-found-content-type = "text/plain"
|
not-found-content-type = "text/plain"
|
||||||
|
|
||||||
[ports]
|
[ports]
|
||||||
server = 5000
|
server = 4419
|
||||||
# 4422 for testing; 4421 for dev-env; 4420 for testing-env; 4419 for prod-env
|
# 4422 for testing; 4421 for dev-env; 4420 for testing-env; 4419 for prod-env
|
||||||
discovery = 4421
|
discovery = 4421
|
||||||
# 1886 for testing; 1885 for dev-env; 1884 for testing-env; 1883 for prod-env
|
# 1886 for testing; 1885 for dev-env; 1884 for testing-env; 1883 for prod-env
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
#define CORE_HELPERS_H
|
#define CORE_HELPERS_H
|
||||||
|
|
||||||
#include <time.h>
|
#include <time.h>
|
||||||
|
#include <mongoose.h>
|
||||||
#include <config.h>
|
#include <config.h>
|
||||||
|
|
||||||
int
|
int
|
||||||
|
@ -13,4 +14,7 @@ helper_get_weekday(const struct tm *time_struct);
|
||||||
int
|
int
|
||||||
helper_drop_privileges();
|
helper_drop_privileges();
|
||||||
|
|
||||||
|
char*
|
||||||
|
find_query_param(struct mg_str query_mg_str, char* search_key);
|
||||||
|
|
||||||
#endif /* CORE_HELPERS_H */
|
#endif /* CORE_HELPERS_H */
|
||||||
|
|
|
@ -18,6 +18,9 @@ macro_action_delete_for_macro(int macro_id);
|
||||||
macro_action_t**
|
macro_action_t**
|
||||||
macro_action_get_for_macro(int macro_id);
|
macro_action_get_for_macro(int macro_id);
|
||||||
|
|
||||||
|
macro_action_t**
|
||||||
|
macro_action_get_for_macro_and_weekday(int macro_id, int weekday);
|
||||||
|
|
||||||
int
|
int
|
||||||
macro_action_execute(macro_action_t *macro_action);
|
macro_action_execute(macro_action_t *macro_action);
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@
|
||||||
void
|
void
|
||||||
api_v1_macros_STR_execute_PUT(struct mg_connection *nc, struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
|
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;
|
(void)nc;
|
||||||
|
|
||||||
uuid_t target_uid;
|
uuid_t target_uid;
|
||||||
|
@ -30,7 +29,29 @@ api_v1_macros_STR_execute_PUT(struct mg_connection *nc, struct http_message *hm,
|
||||||
return;
|
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_lock lock;
|
||||||
database_transaction_begin(&lock);
|
database_transaction_begin(&lock);
|
||||||
|
|
37
src/helpers/find_query_param.c
Normal file
37
src/helpers/find_query_param.c
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
#include <mongoose.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
|
@ -121,6 +121,19 @@ macro_action_get_for_macro(int macro_id)
|
||||||
return macro_action_db_select(stmt);
|
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
|
int
|
||||||
macro_action_execute(macro_action_t *macro_action)
|
macro_action_execute(macro_action_t *macro_action)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in a new issue