fix: 504 return code and relay_schedule junction

This commit is contained in:
Tobias Reisinger 2020-05-19 18:03:44 +02:00
parent c370ab6fea
commit 6050f1cc5f
2 changed files with 14 additions and 16 deletions

View file

@ -226,24 +226,18 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *c, endpoint_args_t *
cJSON_Delete(json);
json = relay_to_json(relay);
int result = command_set_relay_schedule(relay);
int status_code = 200;
if(result)
{
status_code = 504;
}
command_set_relay_schedule(relay);
char *json_str = cJSON_Print(json);
if (json_str == NULL)
{
LOG_ERROR("failed to print relay json\n");
mg_send_head(c, status_code, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_send_head(c, 200, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_printf(c, "{}");
}
else
{
mg_send_head(c, status_code, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_send_head(c, 200, strlen(json_str), "Content-Type: application/json\r\n" STANDARD_HEADERS);
mg_printf(c, "%s", json_str);
free(json_str);
}

View file

@ -17,12 +17,6 @@ db_update_insert(relay_t *relay, sqlite3_stmt *stmt)
{
int rc;
junction_relay_schedule_remove_for_relay(relay->id);
for(int i = 0; i < 7; ++i)
{
junction_relay_schedule_insert(i, relay->id, relay->schedules[i]->id);
}
sqlite3_bind_int(stmt, 1, relay->id);
sqlite3_bind_int(stmt, 2, relay->number);
sqlite3_bind_text(stmt, 3, relay->name, -1, SQLITE_STATIC);
@ -142,8 +136,18 @@ relay_save(relay_t *relay)
}
else
{
relay->id = sqlite3_last_insert_rowid(global_database);
if(relay->id == 0)
{
relay->id = sqlite3_last_insert_rowid(global_database);
}
}
junction_relay_schedule_remove_for_relay(relay->id);
for(int i = 0; i < 7; ++i)
{
junction_relay_schedule_insert(i, relay->id, relay->schedules[i]->id);
}
return result;
}