diff options
-rw-r--r-- | python/README.md | 2 | ||||
-rw-r--r-- | python/database.py | 62 | ||||
-rw-r--r-- | python/main.py | 8 | ||||
-rw-r--r-- | python/routes.py | 47 |
4 files changed, 80 insertions, 39 deletions
diff --git a/python/README.md b/python/README.md index 4a30035..d95b60d 100644 --- a/python/README.md +++ b/python/README.md @@ -1,5 +1,5 @@ [X] - Flask return json [X] - Get parameters [X] - Create database -[ ] - Interact with database +[X] - Interact with database [ ] - diff --git a/python/database.py b/python/database.py index 5bc8b5f..06f5d61 100644 --- a/python/database.py +++ b/python/database.py @@ -1,20 +1,58 @@ import mysql.connector -def getConnector(): - return mysql.connector.connect( - host="localhost", - user="root", +connector = mysql.connector.connect( + host="localhost", + user="root", + database="balalaika", +) + +cursor = connector.cursor() + + +def get_song(id, name): + if id is None: + id = "%" + + if name is None: + name = "%" + + cursor.execute( + """ + select * from song + where id like %(id)s + and name like %(name)s; + """, + { + 'id': id, + 'name': name, + } ) + return cursor.fetchall() + -def getCursor(connector): - return connector.cursor() +def get_filtered_songs(song, album, artist): + if artist is None: + artist = "%" + if album is None: + album = "%" + + cursor.execute( + """ + select song.name, album.name, artist.name + from song + inner join album on song.album_id=album.id + inner join artist on album.artist_id=artist.id + where artist.name like %(artist)s + and album.name like %(album)s; + """, + { + 'artist': artist, + 'album': album, + 'song': song, + } + ) -def runQuery(connector, query): - cursor = getCursor(connector) - cursor.execute(query) - result = cursor.fetchall() - cursor.close() - return result + return cursor.fetchall() diff --git a/python/main.py b/python/main.py index c778f8f..d7eb1b6 100644 --- a/python/main.py +++ b/python/main.py @@ -1,10 +1,4 @@ import routes -import database -connector = database.getConnector() -database.runQuery(connector, "SHOW DATABASES") -app = routes.createApp() -routes.createRoutes(app, connector) - -app.run() +routes.app.run() diff --git a/python/routes.py b/python/routes.py index a60584d..9567318 100644 --- a/python/routes.py +++ b/python/routes.py @@ -2,23 +2,32 @@ from flask import Flask, jsonify, request import database -def createApp(): - return Flask(__name__) - - -def createRoutes(app, connector): - @app.route("/", methods=['GET']) - def hello_world(): - arg1 = request.args.get('arg1') - arg2 = request.args.get('arg2') - return jsonify({'arg1': arg1, - 'arg2': arg2}) - - @app.route("/db", methods=['GET']) - def database_hello(): - return jsonify( - database.runQuery( - connector, - "SHOW DATABASES" - ) +app = Flask(__name__) + + +def run(): + app.run() + + +@app.route("/", methods=['GET']) +def hello_world(): + return "not here" + + +@app.route("/song", methods=['GET']) +def get_song(): + id = request.args.get('id') + name = request.args.get('name') + return jsonify(database.get_song(id=id, name=name)) + + +@app.route("/songs", methods=['GET']) +def get_filtered_songs(): + song = request.args.get('song') + album = request.args.get('album') + artist = request.args.get('artist') + return jsonify( + database.get_filtered_songs( + song=song, album=album, artist=artist ) + ) |