core-legacy/endpoints/api_v1_controllers_STR.c

194 lines
5.6 KiB
C
Raw Normal View History

#include <cJSON.h>
2020-05-20 23:33:18 +00:00
#include <macros.h>
2020-05-06 23:38:13 +00:00
#include <command.h>
#include <constants.h>
#include <endpoints/api_v1_controllers.h>
#include <logger.h>
#include <models/controller.h>
#include <models/tag.h>
void
2020-05-20 23:33:18 +00:00
api_v1_controllers_STR_GET(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)hm;
uuid_t target_uid;
if(uuid_parse(args[0].value.v_str, target_uid))
{
2020-05-20 23:33:18 +00:00
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
2020-05-30 22:23:57 +00:00
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
controller_t* controller = controller_get_by_uid(target_uid);
if(!controller)
{
2020-05-20 23:33:18 +00:00
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
static const char content[] = "no controller for id found";
2020-05-30 22:23:57 +00:00
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
2020-05-20 23:33:18 +00:00
cJSON *json = controller_to_json(controller);
2020-05-30 22:23:57 +00:00
endpoint_response_json(response, 200, json);
cJSON_Delete(json);
controller_free(controller);
}
void
2020-05-20 23:33:18 +00:00
api_v1_controllers_STR_PUT(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)hm;
uuid_t target_uid;
if(uuid_parse(args[0].value.v_str, target_uid))
{
2020-05-20 23:33:18 +00:00
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
2020-05-30 22:23:57 +00:00
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
controller_t* controller = controller_get_by_uid(target_uid);
if(!controller)
{
2020-05-20 23:33:18 +00:00
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
static const char content[] = "no controller for id found";
2020-05-30 22:23:57 +00:00
endpoint_response_text(response, 404, content, STRLEN(content));
return;
}
cJSON *json = cJSON_ParseWithLength(hm->body.p, hm->body.len);
if(json == NULL)
{
2020-05-20 23:33:18 +00:00
static const char content[] = "no valid json was supplied";
2020-05-30 22:23:57 +00:00
endpoint_response_text(response, 400, content, STRLEN(content));
return;
}
cJSON *json_name = cJSON_GetObjectItemCaseSensitive(json, "name");
if(cJSON_IsString(json_name) && json_name->valuestring)
{
strncpy(controller->name, json_name->valuestring, MAX_NAME_LENGTH);
controller->name[MAX_NAME_LENGTH] = '\0';
}
cJSON *json_ip = cJSON_GetObjectItemCaseSensitive(json, "ip");
if(cJSON_IsString(json_ip) && json_ip->valuestring)
{
strncpy(controller->ip, json_ip->valuestring, IP_LENGTH);
controller->ip[IP_LENGTH] = '\0';
}
if(controller_save(controller))
{
LOG_ERROR("failed to save controller\n");
free(controller);
cJSON_Delete(json);
2020-05-20 23:33:18 +00:00
static const char content[] = "failed to save controller to database";
response->status_code = 500;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
return;
}
cJSON_Delete(json);
json = controller_to_json(controller);
2020-05-20 23:33:18 +00:00
command_set_controller_name(controller);
2020-05-06 23:38:13 +00:00
char *json_str = cJSON_Print(json);
if (json_str == NULL)
{
LOG_ERROR("failed to print controller json\n");
2020-05-20 23:33:18 +00:00
static const char content[] = "failed to print json for controller";
response->status_code = 500;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
}
else
{
2020-05-20 23:33:18 +00:00
response->status_code = 200;
response->content_type = "application/json";
response->content_length = strlen(json_str);
response->content = json_str;
response->alloced_content = true;
}
cJSON_Delete(json);
controller_free(controller);
}
void
2020-05-20 23:33:18 +00:00
api_v1_controllers_STR_DELETE(struct http_message *hm, endpoint_args_t *args, endpoint_response_t *response)
{
(void)hm;
const char *target_uid_str = args[0].value.v_str;
uuid_t target_uid;
if(uuid_parse(target_uid_str, target_uid))
{
2020-05-20 23:33:18 +00:00
LOG_DEBUG("failed to unparse uid\n");
static const char content[] = "given id was invalid";
response->status_code = 400;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
return;
}
controller_t* controller = controller_get_by_uid(target_uid);
if(!controller)
{
2020-05-20 23:33:18 +00:00
LOG_DEBUG("could not find a controller for uid '%s'\n", args[0].value.v_str);
static const char content[] = "no controller for id found";
response->status_code = 404;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
return;
}
if(controller_remove(controller))
{
2020-05-20 23:33:18 +00:00
LOG_ERROR("failed to remove controller from database\n");
static const char content[] = "failed to remove controller from database";
response->status_code = 500;
response->content_type = "text/plain";
response->content_length = STRLEN(content);;
response->content = content;
response->alloced_content = false;
}
else
{
2020-05-20 23:33:18 +00:00
response->status_code = 200;
response->content_type = "application/json";
response->content_length = 0;
response->content = "";
response->alloced_content = false;
}
controller_free(controller);
return;
}