Add weekday query parameter for macro execute

This commit is contained in:
Tobias Reisinger 2024-04-16 23:07:30 +02:00
parent ea6b653121
commit 173b9d41e8
Signed by: serguzim
GPG key ID: 13AD60C237A28DFE
7 changed files with 83 additions and 5 deletions

View file

@ -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)

View file

@ -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

View file

@ -2,6 +2,7 @@
#define CORE_HELPERS_H
#include <time.h>
#include <mongoose.h>
#include <config.h>
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 */

View file

@ -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);

View file

@ -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);

View 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;
}

View file

@ -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)
{