diff options
Diffstat (limited to 'api')
-rw-r--r-- | api/db.py | 5 | ||||
-rw-r--r-- | api/social/search.py | 26 | ||||
-rwxr-xr-x | api/tests.sh | 37 | ||||
-rw-r--r-- | api/user/info.py | 38 |
4 files changed, 77 insertions, 29 deletions
@@ -5,4 +5,9 @@ import sqlite3 db_path = os.path.abspath("database/database.db") connection = sqlite3.connect(db_path, check_same_thread=False) #! ^^^ Dit is onveilig en goede multithreading support moet nog geïmplementeerd worden + +connection.enable_load_extension(True) +connection.load_extension("./database/levenshtein.sqlext") + cursor = connection.cursor() + diff --git a/api/social/search.py b/api/social/search.py new file mode 100644 index 0000000..712d0b8 --- /dev/null +++ b/api/social/search.py @@ -0,0 +1,26 @@ +from flask import Blueprint, request +from db import cursor +from user.info import format_user +import json + +search = Blueprint('search', __name__) + +@search.route('/search', methods = ['GET', 'POST']) +def index(): + data_string = request.data or "{}" + data = json.loads(data_string) + query = data.get("query") or "" + if not query: return "", 400 + if len(query) < 3: return "", 403 + + results = cursor.execute("select user_id from users where levenshtein(username, ?, 3)", [query]).fetchmany(20); + + formatted = { "results": [] } + + for user in results: + formatted["results"].append(format_user(user[0])) + + return formatted, 200 + +dynamic_route = ["/social", search] + diff --git a/api/tests.sh b/api/tests.sh index ce30af3..5281acf 100755 --- a/api/tests.sh +++ b/api/tests.sh @@ -72,22 +72,31 @@ # localhost:5000/game/new # } -random_game_1 () { - curl -X POST \ - -H "Content-Type: application/json" \ - -d '{ "user_id": "e6162c82-3e60-4479-ac96-a1af508e49c4" }' \ - localhost:2080/api/game/random -} +# random_game_1 () { +# curl -X POST \ +# -H "Content-Type: application/json" \ +# -d '{ "user_id": "e6162c82-3e60-4479-ac96-a1af508e49c4" }' \ +# localhost:2080/api/game/random +# } + +# random_game_2 () { +# curl -X POST \ +# -H "Content-Type: application/json" \ +# -d '{ "user_id": "de960155-7d58-46b3-a4f6-7d33aa034ad9" }' \ +# localhost:2080/api/game/random +# } + +# sleep 3 +# random_game_1 +# sleep 10 +# random_game_2 -random_game_2 () { - curl -X POST \ +search () { + curl \ -H "Content-Type: application/json" \ - -d '{ "user_id": "de960155-7d58-46b3-a4f6-7d33aa034ad9" }' \ - localhost:2080/api/game/random + -d "{ \"query\": \"$1\" }" \ + localhost:2080/api/social/search } -sleep 3 -random_game_1 -sleep 10 -random_game_2 +search loekaars diff --git a/api/user/info.py b/api/user/info.py index edb7743..3d4f4fb 100644 --- a/api/user/info.py +++ b/api/user/info.py @@ -3,6 +3,17 @@ from db import cursor from auth.login_token import token_login import json +def format_user(user_id): + user = cursor.execute("select username, user_id, country, type, registered, avatar from users where user_id = ?", [user_id]).fetchone() + return { + "username": user[0], + "id": user[1], + "country": user[2], + "type": user[3], + "registered": user[4], + "avatar": user[5], + } + info = Blueprint('info', __name__) @info.route('/info', methods = ['GET', 'POST']) @@ -11,29 +22,26 @@ def index(): data = json.loads(data_string) username = data.get("username") or "" - id = data.get("id") or "" + user_id = data.get("id") or "" token = request.cookies.get("token") or "" if not username and \ - not id and \ + not user_id and \ not token: return "", 400 - if token: id = token_login(token) + if token: + user_id = token_login(token) + elif username: + temp_user_id = cursor.execute("select user_id from users where username = ?", [username]).fetchone() + if temp_user_id: + user_id = temp_user_id[0] - if username: - user = cursor.execute("select username, user_id, country, type, registered, avatar from users where username = ?", [username]).fetchone() - else: - user = cursor.execute("select username, user_id, country, type, registered, avatar from users where user_id = ?", [id]).fetchone() + user = format_user(user_id) + + if not user: return "", 403 #TODO: rating uitrekenen zodra er game functionaliteit is - return { - "username": user[0], - "id": user[1], - "country": user[2], - "type": user[3], - "registered": user[4], - "avatar": user[5], - } + return user dynamic_route = ["/user", info] |