add: temporary CORS headers

add: better sql calls with JOIN
This commit is contained in:
Tobias Reisinger 2020-05-20 00:51:16 +02:00
parent 6050f1cc5f
commit 5a19e99627
7 changed files with 50 additions and 12 deletions

View file

@ -156,14 +156,10 @@ api_v1_schedules_STR_PUT(struct mg_connection *c, endpoint_args_t *args, struct
return; return;
} }
int *relays = junction_relay_schedule_get_relays_ids(schedule->id); relay_t **relays = relay_get_with_schedule(schedule->id);
for(int i = 0; relays[i] != 0; ++i) for(int i = 0; relays[i] != NULL; ++i)
{ {
relay_t *relay = relay_get_by_id(relays[i]); command_set_relay_schedule(relays[i]);
if(relay)
{
command_set_relay_schedule(relay);
}
} }
cJSON *json_tag; cJSON *json_tag;

View file

@ -10,7 +10,7 @@ handler_connection(struct mg_connection *c, int ev, void *p)
if (ev == MG_EV_HTTP_REQUEST) if (ev == MG_EV_HTTP_REQUEST)
{ {
struct http_message *hm = (struct http_message *) p; struct http_message *hm = (struct http_message *) p;
LOG_DEBUG("new http request for %.*s\n", hm->uri.len, hm->uri.p); LOG_DEBUG("new http %.*s request for %.*s\n", hm->method.len, hm->method.p, hm->uri.len, hm->uri.p);
endpoint_t *endpoint = router_find_endpoint(hm->uri.p, hm->uri.len, &hm->method); endpoint_t *endpoint = router_find_endpoint(hm->uri.p, hm->uri.len, &hm->method);
@ -33,7 +33,11 @@ handler_connection(struct mg_connection *c, int ev, void *p)
if(endpoint->method == HTTP_METHOD_OPTIONS) if(endpoint->method == HTTP_METHOD_OPTIONS)
{ {
char options_header[256]; // TODO make more generic char options_header[256]; // TODO make more generic
sprintf(options_header, "Allow: OPTIONS%s%s%s%s\r\n" STANDARD_HEADERS, sprintf(options_header, "Allow: OPTIONS%s%s%s%s\r\nAccess-Control-Allow-Methods: OPTIONS%s%s%s%s\r\n" STANDARD_HEADERS,
endpoint->options & HTTP_METHOD_GET ? ", GET" : "",
endpoint->options & HTTP_METHOD_POST ? ", POST" : "",
endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",
endpoint->options & HTTP_METHOD_DELETE ? ", DELETE" : "",
endpoint->options & HTTP_METHOD_GET ? ", GET" : "", endpoint->options & HTTP_METHOD_GET ? ", GET" : "",
endpoint->options & HTTP_METHOD_POST ? ", POST" : "", endpoint->options & HTTP_METHOD_POST ? ", POST" : "",
endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "", endpoint->options & HTTP_METHOD_PUT ? ", PUT" : "",

View file

@ -28,6 +28,6 @@
#define PIFACE_GPIO_BASE 200 #define PIFACE_GPIO_BASE 200
#define STANDARD_HEADERS "Access-Control-Allow-Origin: *" #define STANDARD_HEADERS "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Headers: *"
#endif /* CORE_CONTANTS_H */ #endif /* CORE_CONTANTS_H */

View file

@ -48,6 +48,9 @@ relay_get_by_id(int id);
relay_t* relay_t*
relay_get_for_controller(int controller_id, int relay_num); relay_get_for_controller(int controller_id, int relay_num);
relay_t**
relay_get_with_schedule(int schedule_id);
relay_t** relay_t**
relay_get_all(); relay_get_all();

View file

@ -52,6 +52,9 @@ schedule_get_by_uid_or_off(uuid_t uid);
schedule_t* schedule_t*
schedule_get_by_uid(uuid_t uid); schedule_get_by_uid(uuid_t uid);
schedule_t**
schedule_get_relay_weekdays(int relay_id);
schedule_t** schedule_t**
schedule_get_all(); schedule_get_all();

View file

@ -60,11 +60,21 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
break; break;
} }
} }
schedule_t **schedules = schedule_get_relay_weekdays(new_relay->id);
for(int i = 0; i < 7; ++i) for(int i = 0; i < 7; ++i)
{ {
int schedule_id = junction_relay_schedule_get_schedule_id(i, new_relay->id); if(schedules[i] == NULL)
new_relay->schedules[i] = schedule_get_by_id_or_off(schedule_id); {
LOG_ERROR("got only %d/7 schedules for relay_id %d\n", i, new_relay->id);
relay_free(new_relay);
free(schedules);
return NULL;
}
new_relay->schedules[i] = schedules[i];
} }
free(schedules); // don't free list, because contents are kept in relay->schedules
relay_reload_active_schedule(new_relay); relay_reload_active_schedule(new_relay);
return new_relay; return new_relay;
@ -324,6 +334,17 @@ relay_get_all()
return relay_db_select(stmt); return relay_db_select(stmt);
} }
relay_t**
relay_get_with_schedule(int schedule_id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT DISTINCT relays.* FROM relays INNER JOIN junction_relay_schedule ON relays.id == junction_relay_schedule.relay_id WHERE junction_relay_schedule.schedule_id = ?1;", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, schedule_id);
return relay_db_select(stmt);
}
relay_t* relay_t*
relay_get_for_controller(int controller_id, int relay_num) relay_get_for_controller(int controller_id, int relay_num)
{ {

View file

@ -391,6 +391,17 @@ schedule_get_by_uid(uuid_t uid)
return result; return result;
} }
schedule_t**
schedule_get_relay_weekdays(int relay_id)
{
sqlite3_stmt *stmt;
sqlite3_prepare_v2(global_database, "SELECT schedules.* FROM schedules INNER JOIN junction_relay_schedule ON schedules.id == junction_relay_schedule.schedule_id WHERE junction_relay_schedule.relay_id = ?1 ORDER BY junction_relay_schedule.weekday ASC", -1, &stmt, NULL);
sqlite3_bind_int(stmt, 1, relay_id);
return schedule_db_select(stmt);
}
schedule_t** schedule_t**
schedule_get_all() schedule_get_all()
{ {