add: temporary CORS headers
add: better sql calls with JOIN
This commit is contained in:
		
							parent
							
								
									6050f1cc5f
								
							
						
					
					
						commit
						5a19e99627
					
				
					 7 changed files with 50 additions and 12 deletions
				
			
		| 
						 | 
				
			
			@ -60,11 +60,21 @@ relay_db_select_mapper(sqlite3_stmt *stmt)
 | 
			
		|||
                break;
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    schedule_t **schedules = schedule_get_relay_weekdays(new_relay->id);
 | 
			
		||||
    for(int i = 0; i < 7; ++i)
 | 
			
		||||
    {
 | 
			
		||||
        int schedule_id = junction_relay_schedule_get_schedule_id(i, new_relay->id);
 | 
			
		||||
        new_relay->schedules[i] = schedule_get_by_id_or_off(schedule_id);
 | 
			
		||||
        if(schedules[i] == NULL)
 | 
			
		||||
        {
 | 
			
		||||
            LOG_ERROR("got only %d/7 schedules for relay_id %d\n", i, new_relay->id);
 | 
			
		||||
            relay_free(new_relay);
 | 
			
		||||
            free(schedules);
 | 
			
		||||
            return NULL;
 | 
			
		||||
        }
 | 
			
		||||
        new_relay->schedules[i] = schedules[i];
 | 
			
		||||
    }
 | 
			
		||||
    free(schedules); // don't free list, because contents are kept in relay->schedules
 | 
			
		||||
 | 
			
		||||
    relay_reload_active_schedule(new_relay);
 | 
			
		||||
 | 
			
		||||
    return new_relay;
 | 
			
		||||
| 
						 | 
				
			
			@ -324,6 +334,17 @@ relay_get_all()
 | 
			
		|||
    return relay_db_select(stmt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
relay_t**
 | 
			
		||||
relay_get_with_schedule(int schedule_id)
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT DISTINCT relays.* FROM relays INNER JOIN junction_relay_schedule ON relays.id == junction_relay_schedule.relay_id WHERE junction_relay_schedule.schedule_id = ?1;", -1, &stmt, NULL);
 | 
			
		||||
    sqlite3_bind_int(stmt, 1, schedule_id);
 | 
			
		||||
 | 
			
		||||
    return relay_db_select(stmt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
relay_t*
 | 
			
		||||
relay_get_for_controller(int controller_id, int relay_num)
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -391,6 +391,17 @@ schedule_get_by_uid(uuid_t uid)
 | 
			
		|||
    return result;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
schedule_t**
 | 
			
		||||
schedule_get_relay_weekdays(int relay_id)
 | 
			
		||||
{
 | 
			
		||||
    sqlite3_stmt *stmt;
 | 
			
		||||
 | 
			
		||||
    sqlite3_prepare_v2(global_database, "SELECT schedules.* FROM schedules INNER JOIN junction_relay_schedule ON schedules.id == junction_relay_schedule.schedule_id WHERE junction_relay_schedule.relay_id = ?1 ORDER BY junction_relay_schedule.weekday ASC", -1, &stmt, NULL);
 | 
			
		||||
    sqlite3_bind_int(stmt, 1, relay_id);
 | 
			
		||||
 | 
			
		||||
    return schedule_db_select(stmt);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
schedule_t**
 | 
			
		||||
schedule_get_all()
 | 
			
		||||
{
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue