diff --git a/README.md b/README.md index 70ca9565..57c8f953 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,24 @@ ### budowanie i uruchamianie obrazu z bazą danych oraz serwerem: docker compose up +w przypadku zmian w bazie danych, usunąć kontener oraz dołączone do niego volumy + +### stronka administracyjna do bazy danych + +Najpierw wpisujemy w przeglądarce http://localhost:8080/ +Przy logowaniu wpisujemy: +* login - admin@admin.com +* hasło - admin + +Po zalogowaniu się poraz pierwszy od zbudowania obrazu, widzimy stronę główną. Klikamy na niej duży przycisk "Add New Server" i pojawia się okienko z menu dodawania serwera. + +W nazwie można napisać cokolwiek, jednak w zakładce "Connection" piszemy następujące rzeczy: +* host name - db +* login - root +* password - root + +Reszta pozostaje jak była. Po zapisaniu ustawień, mamy już dostęp do narzędzi administratora dla naszej bazy danych. + ### gadanie z endpointami GET http://localhost:8090/ - testowy domowy @@ -16,4 +34,6 @@ GET http://localhost:8090//api/v3/get_movie/ - info o filmie POST http://localhost:8090/api/v3/add// - dodawanie usera +POST http://localhost:8090/api/v3/add/// - dodawanie oceny do filmu przez usera + GET http://localhost:8090/api/v3/ai/ - wyciąganie rekomendacji od AI \ No newline at end of file diff --git a/connector/Include/frontend_AI_connector.py b/connector/Include/frontend_AI_connector.py index e64d45b8..f489ca29 100644 --- a/connector/Include/frontend_AI_connector.py +++ b/connector/Include/frontend_AI_connector.py @@ -10,6 +10,15 @@ db_connector = None conn = None movie_list = None +def error_decorator(fun): + def inner1(*args, **kwargs): + try: + fun(*args, **kwargs) + except psycopg2.DatabaseError: + return jsonify({"status": "Something... unexpected has occured :sweat_smile:"}), 500 + + return inner1 + @app.route("/", methods=["GET"]) def hello(): return jsonify({"response": "Hello there"}), 200 @@ -28,6 +37,7 @@ def add_user(oauth_ID, username): res = cursor.fetchall() if len(res): + cursor.close() return jsonify({"status": "User already exists"}), 500 cursor.execute("INSERT INTO users (username, oauth_ID) VALUES ('{}','{}');".format( @@ -49,6 +59,7 @@ def get_recommendations(oauth_ID): return jsonify({"movies": ["3", "Wiedźmin 3", "Najlepszy."]}), 200 @app.route("/api/v3/get_movie/", methods=["GET"]) +# @error_decorator def get_movie(movie_ID): movie_info = movie_list.loc[movie_list['movie_id'] == movie_ID] if movie_info.empty: @@ -65,6 +76,46 @@ def get_movie(movie_ID): return jsonify(output_json), 200 +@app.route("/api/v3/rate_movie///", methods=["POST"]) +def rate_movie(uID, movie_ID, rating): + movie_info = movie_list.loc[movie_list['movie_id'] == int(movie_ID)] + if movie_info.empty: + return jsonify({"status": "Movie with ID {} doesn't exist".format(movie_ID)} + ), 500 + + if rating < 1 or rating > 5: + return jsonify({"status": "Incorrect rating"}), 500 + + cursor = conn.cursor() + cursor.execute("select * from users where oauth_ID='{}';".format(uID)) + res = cursor.fetchall() + + if not len(res): + cursor.close() + return jsonify({"status": "User doesn't exists"}), 500 + + cursor.execute("select * from ratings where oauth_ID='{}' AND movie_ID='{}';".format(uID, movie_ID)) + res = cursor.fetchall() + + if len(res): + sql = """ UPDATE ratings + SET rating = {}, + rdate = CURRENT_TIMESTAMP + WHERE oauth_ID = '{}' AND + movie_ID = '{}' + """ + cursor.execute(sql.format(rating, uID, movie_ID)) + else: + cursor.execute("INSERT INTO ratings (movie_ID, oauth_ID, rating) VALUES ('{}','{}',{});".format( + movie_ID, uID, rating + )) + + conn.commit() + cursor.close() + + return jsonify({"status": "success"}), 200 + + if __name__ == "__main__": config = ConfigParser() config.read("init_scripts/constants.ini") @@ -78,6 +129,7 @@ if __name__ == "__main__": password=config["postgres"]["password"], port=int(config["postgres"]["port"]) ) + except Exception: print("Trying to connect with database") continue diff --git a/database/init.sql b/database/init.sql index ee122627..b704838f 100644 --- a/database/init.sql +++ b/database/init.sql @@ -4,7 +4,17 @@ CREATE TABLE users ( oauth_ID VARCHAR(255) NOT NULL ); +CREATE TABLE ratings ( + id SERIAL PRIMARY KEY, + movie_ID VARCHAR(255) NOT NULL, + oauth_ID VARCHAR(255) NOT NULL, + rating INTEGER NOT NULL, + rdate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP +); + INSERT INTO users (username, oauth_ID) VALUES ('Mkyong', 40), ('Ali', 28), ('Teoh', 18); + +