def user_exists(username): return get_db().execute('SELECT id FROM user where username = ?', (username, )).fetchone() is not None
def create_user(username, password, coach_id): db = get_db() db.execute( 'INSERT INTO user (username, password, coach_id) VALUES (?,?,?)', (username, generate_password_hash(password), coach_id)) db.commit()
def fetch_user_by_name(username): return get_db().execute('SELECT * FROM user WHERE username = ?', (username, )).fetchone()
def fetch_user_by_id(user_id): return get_db().execute('SELECT * FROM user WHERE id =?', (user_id, )).fetchone()
def get_coach(coach_id): return get_db().execute('SELECT * FROM coach WHERE id = ?', (coach_id, )).fetchone()
def get_all_coaches(): return get_db().execute('SELECT * FROM coach').fetchall()
def fetch_appointments_for_user(user_id): return get_db().execute( 'SELECT * FROM appointment WHERE user_id = ? ORDER BY start_time DESC', (user_id, )).fetchall()
def create_appointment(date, coach_id, user_id): db = get_db() db.execute( 'INSERT INTO appointment (start_time, coach_id, user_id) VALUES (?, ?, ?)', (date, coach_id, user_id)) db.commit()
def appointment_exists(coach_id, date): return get_db().execute( 'SELECT * FROM appointment WHERE coach_id = ? AND start_time = ?', (coach_id, date)).fetchone() is not None