add: OPTIONS method support
add: relay tag endpoint
This commit is contained in:
parent
dab106c1f6
commit
420bdeb2aa
6 changed files with 158 additions and 26 deletions
57
endpoints/api_v1_relays_tag_STR.c
Normal file
57
endpoints/api_v1_relays_tag_STR.c
Normal file
|
@ -0,0 +1,57 @@
|
|||
#include <cJSON.h>
|
||||
#include <constants.h>
|
||||
#include <endpoints/api_v1_relays.h>
|
||||
#include <logger.h>
|
||||
#include <models/junction_tag.h>
|
||||
#include <models/relay.h>
|
||||
#include <models/tag.h>
|
||||
|
||||
void
|
||||
api_v1_relays_tag_STR_GET(struct mg_connection *c, endpoint_args_t *args, struct http_message *hm)
|
||||
{
|
||||
(void)hm;
|
||||
|
||||
int tag_id = tag_get_id(args[0].value.v_str);
|
||||
int *relays_ids = junction_tag_get_relays_for_tag_id(tag_id);
|
||||
if(relays_ids == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print relays json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
return;
|
||||
}
|
||||
|
||||
cJSON *json = cJSON_CreateArray();
|
||||
|
||||
for(int i = 0; relays_ids[i] != 0; ++i)
|
||||
{
|
||||
relay_t* relay = relay_get_by_id(relays_ids[i]);
|
||||
|
||||
if(!relay)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
cJSON *json_relay = relay_to_json(relay);
|
||||
|
||||
cJSON_AddItemToArray(json, json_relay);
|
||||
|
||||
relay_free(relay);
|
||||
}
|
||||
|
||||
char *json_str = cJSON_Print(json);
|
||||
if (json_str == NULL)
|
||||
{
|
||||
LOG_ERROR("failed to print relays json\n");
|
||||
mg_send_head(c, 500, 2, "Content-Type: application/json\r\n" STANDARD_HEADERS);
|
||||
mg_printf(c, "[]");
|
||||
}
|
||||
else
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
cJSON_Delete(json);
|
||||
free(relays_ids);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue