rest: update GET /oglive/list to include oglive data from database

Append oglive info at the end of the array of the "oglive" entry.
Each entry defines the name of the oglive and the creation date.
master
Alejandro Sirgo Rica 2024-11-07 09:41:55 +01:00
parent 55179decb7
commit 892a8fa2e5
2 changed files with 91 additions and 1 deletions

View File

@ -4607,8 +4607,12 @@ static int og_cmd_oglive_list(char *buffer_reply)
struct og_buffer og_buffer = {
.data = buffer_reply
};
const char *msglog, *live_name, *live_datetime;
json_t *root, *live_entry, *oglive_array;
json_error_t json_err;
json_t *root;
struct og_dbi *dbi;
dbi_result result;
root = json_load_file(OG_LIVE_JSON_FILE_PATH, 0, &json_err);
if (!root) {
@ -4617,6 +4621,58 @@ static int og_cmd_oglive_list(char *buffer_reply)
return -1;
}
oglive_array = json_object_get(root, "oglive");
if (!oglive_array || !json_is_array(oglive_array)) {
syslog(LOG_ERR, "Expected 'oglive' to be a JSON array\n");
json_decref(root);
return -1;
}
dbi = og_dbi_open(&ogconfig.db);
if (!dbi) {
syslog(LOG_ERR, "cannot open conection database (%s:%d)\n",
__func__, __LINE__);
json_decref(root);
return -1;
}
result = dbi_conn_queryf(dbi->conn,
"SELECT name, "
"DATE_FORMAT(creation_date, '%%a %%b %%d %%H:%%i:%%s %%Y') AS formatted_date "
"FROM oglive");
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_ERR, "failed to query database (%s:%d) %s\n",
__func__, __LINE__, msglog);
og_dbi_close(dbi);
json_decref(root);
return -1;
}
while (dbi_result_next_row(result) > 0) {
live_name = dbi_result_get_string(result, "name");
live_datetime = dbi_result_get_string(result, "formatted_date");
live_entry = json_object();
if (!live_entry) {
syslog(LOG_ERR, "Cannot allocate JSON object (%s:%d)\n",
__func__, __LINE__);
dbi_result_free(result);
og_dbi_close(dbi);
json_decref(root);
return -1;
}
json_object_set_new(live_entry, "name", json_string(live_name));
json_object_set_new(live_entry, "date", json_string(live_datetime));
json_array_append_new(oglive_array, live_entry);
}
dbi_result_free(result);
og_dbi_close(dbi);
if (json_dump_callback(root, og_json_dump_clients, &og_buffer, JSON_ENSURE_ASCII)) {
json_decref(root);
return -1;

View File

@ -464,6 +464,39 @@ static int og_dbi_schema_v11(struct og_dbi *dbi)
return 0;
}
static int og_dbi_schema_v12(struct og_dbi *dbi)
{
const char *msglog;
dbi_result result;
syslog(LOG_DEBUG, "Creating table oglive\n");
result = dbi_conn_query(dbi->conn, "CREATE TABLE `oglive` ("
"`id` BIGINT NOT NULL AUTO_INCREMENT,"
"`name` VARCHAR(100),"
"`creation_date` DATETIME NOT NULL,"
"`is_default` BOOLEAN NOT NULL,"
"PRIMARY KEY (`id`)"
")");
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_INFO, "Error when creating oglive (%s:%d) %s\n",
__func__, __LINE__, msglog);
return -1;
}
dbi_result_free(result);
result = dbi_conn_query(dbi->conn, "UPDATE version SET version = 12");
if (!result) {
dbi_conn_error(dbi->conn, &msglog);
syslog(LOG_INFO, "Could not update version row (%s:%d) %s\n",
__func__, __LINE__, msglog);
return -1;
}
dbi_result_free(result);
return 0;
}
static struct og_schema_version {
int version;
int (*update)(struct og_dbi *dbi);
@ -479,6 +512,7 @@ static struct og_schema_version {
{ .version = 9, .update = og_dbi_schema_v9, },
{ .version = 10, .update = og_dbi_schema_v10,},
{ .version = 11, .update = og_dbi_schema_v11,},
{ .version = 12, .update = og_dbi_schema_v12,},
{ 0, NULL },
};