add: schedule tags endpoint
fix: memory leak for string args
This commit is contained in:
		
							parent
							
								
									a127a68e31
								
							
						
					
					
						commit
						f040cd8b21
					
				
					 6 changed files with 71 additions and 2 deletions
				
			
		| 
						 | 
				
			
			@ -195,7 +195,7 @@ api_v1_schedules_STR_DELETE(struct mg_connection *c, endpoint_args_t *args, stru
 | 
			
		|||
{
 | 
			
		||||
    (void)hm;
 | 
			
		||||
 | 
			
		||||
    char *target_uid_str = args[0].value.v_str;
 | 
			
		||||
    const char *target_uid_str = args[0].value.v_str;
 | 
			
		||||
 | 
			
		||||
    uuid_t target_uid;
 | 
			
		||||
    if(schedule_uid_parse(target_uid_str, target_uid))
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
							
								
								
									
										57
									
								
								endpoints/api_v1_schedules_tags_STR.c
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										57
									
								
								endpoints/api_v1_schedules_tags_STR.c
									
										
									
									
									
										Normal file
									
								
							| 
						 | 
				
			
			@ -0,0 +1,57 @@
 | 
			
		|||
#include <cJSON.h>
 | 
			
		||||
#include <constants.h>
 | 
			
		||||
#include <endpoints/api_v1_schedules.h>
 | 
			
		||||
#include <logger.h>
 | 
			
		||||
#include <models/junction_tag.h>
 | 
			
		||||
#include <models/schedule.h>
 | 
			
		||||
#include <models/tag.h>
 | 
			
		||||
 | 
			
		||||
void
 | 
			
		||||
api_v1_schedules_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 *schedules_ids = junction_tag_get_schedules_for_tag_id(tag_id);
 | 
			
		||||
    if(schedules_ids == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR("failed to print schedules 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; schedules_ids[i] != 0; ++i)
 | 
			
		||||
    {
 | 
			
		||||
        schedule_t* schedule = schedule_get_by_id(schedules_ids[i]);
 | 
			
		||||
 | 
			
		||||
        if(!schedule)
 | 
			
		||||
        {
 | 
			
		||||
            continue;
 | 
			
		||||
        }
 | 
			
		||||
        cJSON *json_schedule = schedule_to_json(schedule);
 | 
			
		||||
 | 
			
		||||
        cJSON_AddItemToArray(json, json_schedule);
 | 
			
		||||
 | 
			
		||||
        schedule_free(schedule);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    char *json_str = cJSON_Print(json);
 | 
			
		||||
    if (json_str == NULL)
 | 
			
		||||
    {
 | 
			
		||||
        LOG_ERROR("failed to print schedules 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(schedules_ids);
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue