add: functions to insert list (for junctions)

This commit is contained in:
Tobias Reisinger 2020-08-18 13:35:15 +02:00
parent 9fb525530f
commit d23655eb61
7 changed files with 155 additions and 26 deletions

View file

@ -226,33 +226,40 @@ api_v1_controllers_STR_relays_INT_PUT(struct mg_connection *nc, struct http_mess
{
LOGGER_DEBUG("cleaning tags");
junction_tag_remove_for_relay(relay->id);
}
cJSON_ArrayForEach(json_tag, json_tags)
{
if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
{
LOGGER_DEBUG("invalid tag in tags\n");
if(opened_transaction)
int json_tags_count = cJSON_GetArraySize(json_tags);
int *tag_ids = malloc(sizeof(int) * json_tags_count);
int i = 0;
cJSON_ArrayForEach(json_tag, json_tags)
{
if(!cJSON_IsString(json_tag) || (json_tag->valuestring == NULL))
{
database_transaction_rollback();
LOGGER_DEBUG("invalid tag in tags\n");
if(opened_transaction)
{
database_transaction_rollback();
}
relay_free(relay);
cJSON_Delete(json);
free(tag_ids);
static const char content[] = "invalid tag in tags";
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
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;
int tag_id = tag_get_id(tag);
if(tag_id == 0)
{
tag_save(tag_id, tag);
tag_id = tag_get_id(tag);
}
tag_ids[i++] = tag_id;
}
const char *tag = json_tag->valuestring;
int tag_id = tag_get_id(tag);
if(tag_id == 0)
{
tag_save(tag_id, tag);
tag_id = tag_get_id(tag);
}
junction_tag_insert(tag_id, relay->id, 0);
junction_tag_insert_list(tag_ids, relay->id, 0, json_tags_count);
}
if(opened_transaction)