add: transaction one layer higher
This commit is contained in:
parent
f49c760e97
commit
9fb525530f
2 changed files with 27 additions and 4 deletions
|
@ -1,6 +1,6 @@
|
||||||
cmake_minimum_required (VERSION 3.7)
|
cmake_minimum_required (VERSION 3.7)
|
||||||
project(core
|
project(core
|
||||||
VERSION 0.2.14
|
VERSION 0.2.15
|
||||||
LANGUAGES C)
|
LANGUAGES C)
|
||||||
|
|
||||||
add_executable(core src/main.c)
|
add_executable(core src/main.c)
|
||||||
|
|
|
@ -117,6 +117,7 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
relay->active_schedule = relay->schedules[helper_get_weekday(time_struct)];
|
relay->active_schedule = relay->schedules[helper_get_weekday(time_struct)];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
controller_free(controller);
|
||||||
LOGGER_DEBUG("overwriting relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
|
LOGGER_DEBUG("overwriting relay %d for controller %s\n", args[1].value.v_int, args[0].value.v_str);
|
||||||
|
|
||||||
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
|
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
|
||||||
|
@ -201,10 +202,17 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int opened_transaction = database_transaction_begin();
|
||||||
|
|
||||||
if(relay_save(relay))
|
if(relay_save(relay))
|
||||||
{
|
{
|
||||||
LOGGER_ERR("failed to save relay\n");
|
LOGGER_ERR("failed to save relay\n");
|
||||||
free(controller);
|
|
||||||
|
if(opened_transaction)
|
||||||
|
{
|
||||||
|
database_transaction_rollback();
|
||||||
|
}
|
||||||
|
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
|
|
||||||
static const char content[] = "failed to save relay to database";
|
static const char content[] = "failed to save relay to database";
|
||||||
|
@ -224,7 +232,18 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
|
if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
|
||||||
{
|
{
|
||||||
LOGGER_DEBUG("invalid tag in tags\n");
|
LOGGER_DEBUG("invalid tag in tags\n");
|
||||||
continue;
|
|
||||||
|
if(opened_transaction)
|
||||||
|
{
|
||||||
|
database_transaction_rollback();
|
||||||
|
}
|
||||||
|
|
||||||
|
relay_free(relay);
|
||||||
|
cJSON_Delete(json);
|
||||||
|
|
||||||
|
static const char content[] = "invalid tag in tags";
|
||||||
|
endpoint_response_text(response, 400, content, STRLEN(content));
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
const char *tag = json_tag->valuestring;
|
const char *tag = json_tag->valuestring;
|
||||||
int tag_id = tag_get_id(tag);
|
int tag_id = tag_get_id(tag);
|
||||||
|
@ -236,6 +255,11 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
junction_tag_insert(tag_id, relay->id, 0);
|
junction_tag_insert(tag_id, relay->id, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(opened_transaction)
|
||||||
|
{
|
||||||
|
database_transaction_commit();
|
||||||
|
}
|
||||||
|
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
json = relay_to_json(relay);
|
json = relay_to_json(relay);
|
||||||
|
|
||||||
|
@ -245,5 +269,4 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
|
||||||
endpoint_response_json(response, 200, json);
|
endpoint_response_json(response, 200, json);
|
||||||
cJSON_Delete(json);
|
cJSON_Delete(json);
|
||||||
relay_free(relay);
|
relay_free(relay);
|
||||||
controller_free(controller);
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue