diff --git a/mocha_server.py b/mocha_server.py index b991de8..80c5aad 100755 --- a/mocha_server.py +++ b/mocha_server.py @@ -149,14 +149,27 @@ def fetch_top_n(num): return output +def update(user_id, score): + conn = sqlite3.connect(DATABASE) + conn.row_factory = sqlite3.Row + cursor = conn.cursor() + cursor.execute('UPDATE users SET score = (?) WHERE user_id = (?)', + (score, user_id)) + conn.commit() + conn.close() + + return [] + + def process_request(uri): ''' Handles the API endpoint. Currently supports: - - /mocha/users/{user_id} Returns JSON of the specified user - - /mocha/users/* Returns JSON list of all users - - /mocha/top/n Returns JSON list of top n users by score - - /mocha/add/{username} Returns JSON of token for new user + - /mocha/users/{user_id} Returns JSON of the specified user + - /mocha/users/* Returns JSON list of all users + - /mocha/top/n Returns JSON list of top n users by score + - /mocha/register/{username} Returns JSON of token for new user + - /mocha/update/{user_id}/{score} ''' parts = uri.split('/')[1:] assert parts[0] == 'mocha' @@ -170,8 +183,10 @@ def process_request(uri): output = fetch_user(parts[2]) elif parts[1] == 'top' and len(parts) > 2: output = fetch_top_n(parts[2]) - elif parts[1] == 'add' and len(parts) > 2: + elif parts[1] == 'register' and len(parts) > 2: output = make_new_account(parts[2]) + elif parts[1] == 'update' and len(parts) > 3: + output = update(parts[2], parts[3]) else: output = None @@ -200,6 +215,10 @@ def application(environ, start_response): if __name__ == '__main__': - print(fetch_top_n(2)) + new_score = random.randint(1, 100) + print(fetch_user(1)) + print(new_score) + update(1, new_score) + print(fetch_user(1)) # vim: tabstop=4 shiftwidth=4 softtabstop=4 expandtab