diff --git a/db.sqlite3 b/db.sqlite3 index 2323ffb..59f2cb0 100755 Binary files a/db.sqlite3 and b/db.sqlite3 differ diff --git a/mocha_server.py b/mocha_server.py index 7e27ef6..f324350 100755 --- a/mocha_server.py +++ b/mocha_server.py @@ -161,6 +161,37 @@ def update(user_id, score): return '[]' +def set_avatar(user_id, new_avatar): + conn = sqlite3.connect(DATABASE) + conn.row_factory = sqlite3.Row + cursor = conn.cursor() + cursor.execute('UPDATE users SET avatar = (?) WHERE user_id = (?)', + (new_avatar, user_id)) + conn.commit() + conn.close() + + return '[]' + + +def get_avatar(user_id): + conn = sqlite3.connect(DATABASE) + conn.row_factory = sqlite3.Row + cursor = conn.cursor() + cursor.execute( + 'SELECT avatar FROM users WHERE user_id = (?)', + (user_id)) + + output = cursor.fetchall() + output = json.dumps([dict(row) for row in output]) + conn.commit() + conn.close() + + if output == '[]': + output = None + + return output + + def process_request(uri): ''' Handles the API endpoint. @@ -215,10 +246,8 @@ def application(environ, start_response): if __name__ == '__main__': - new_score = random.randint(1, 100) - print(fetch_user(1)) - print(new_score) - update(1, new_score) - print(fetch_user(1)) + print(get_avatar('1')) + set_avatar('1', 'newavatar') + print(get_avatar('1')) # vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab