mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-11-11 14:07:39 +01:00
oceny i update instrukcji
This commit is contained in:
parent
2071bd7a5a
commit
8e095a47ec
20
README.md
20
README.md
@ -6,6 +6,24 @@
|
|||||||
### budowanie i uruchamianie obrazu z bazą danych oraz serwerem:
|
### budowanie i uruchamianie obrazu z bazą danych oraz serwerem:
|
||||||
docker compose up
|
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
|
### gadanie z endpointami
|
||||||
|
|
||||||
GET http://localhost:8090/ - testowy domowy
|
GET http://localhost:8090/ - testowy domowy
|
||||||
@ -16,4 +34,6 @@ GET http://localhost:8090//api/v3/get_movie/<int:movie_ID> - info o filmie
|
|||||||
|
|
||||||
POST http://localhost:8090/api/v3/add/<string:oauth_ID>/<string:username> - dodawanie usera
|
POST http://localhost:8090/api/v3/add/<string:oauth_ID>/<string:username> - dodawanie usera
|
||||||
|
|
||||||
|
POST http://localhost:8090/api/v3/add/<string:oauth_ID>/<string:movie_ID>/<int:rating> - dodawanie oceny do filmu przez usera
|
||||||
|
|
||||||
GET http://localhost:8090/api/v3/ai/<string:oauth_ID> - wyciąganie rekomendacji od AI
|
GET http://localhost:8090/api/v3/ai/<string:oauth_ID> - wyciąganie rekomendacji od AI
|
||||||
@ -10,6 +10,15 @@ db_connector = None
|
|||||||
conn = None
|
conn = None
|
||||||
movie_list = 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"])
|
@app.route("/", methods=["GET"])
|
||||||
def hello():
|
def hello():
|
||||||
return jsonify({"response": "Hello there"}), 200
|
return jsonify({"response": "Hello there"}), 200
|
||||||
@ -28,6 +37,7 @@ def add_user(oauth_ID, username):
|
|||||||
res = cursor.fetchall()
|
res = cursor.fetchall()
|
||||||
|
|
||||||
if len(res):
|
if len(res):
|
||||||
|
cursor.close()
|
||||||
return jsonify({"status": "User already exists"}), 500
|
return jsonify({"status": "User already exists"}), 500
|
||||||
|
|
||||||
cursor.execute("INSERT INTO users (username, oauth_ID) VALUES ('{}','{}');".format(
|
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
|
return jsonify({"movies": ["3", "Wiedźmin 3", "Najlepszy."]}), 200
|
||||||
|
|
||||||
@app.route("/api/v3/get_movie/<int:movie_ID>", methods=["GET"])
|
@app.route("/api/v3/get_movie/<int:movie_ID>", methods=["GET"])
|
||||||
|
# @error_decorator
|
||||||
def get_movie(movie_ID):
|
def get_movie(movie_ID):
|
||||||
movie_info = movie_list.loc[movie_list['movie_id'] == movie_ID]
|
movie_info = movie_list.loc[movie_list['movie_id'] == movie_ID]
|
||||||
if movie_info.empty:
|
if movie_info.empty:
|
||||||
@ -65,6 +76,46 @@ def get_movie(movie_ID):
|
|||||||
|
|
||||||
return jsonify(output_json), 200
|
return jsonify(output_json), 200
|
||||||
|
|
||||||
|
@app.route("/api/v3/rate_movie/<string:uID>/<string:movie_ID>/<int:rating>", 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__":
|
if __name__ == "__main__":
|
||||||
config = ConfigParser()
|
config = ConfigParser()
|
||||||
config.read("init_scripts/constants.ini")
|
config.read("init_scripts/constants.ini")
|
||||||
@ -78,6 +129,7 @@ if __name__ == "__main__":
|
|||||||
password=config["postgres"]["password"],
|
password=config["postgres"]["password"],
|
||||||
port=int(config["postgres"]["port"])
|
port=int(config["postgres"]["port"])
|
||||||
)
|
)
|
||||||
|
|
||||||
except Exception:
|
except Exception:
|
||||||
print("Trying to connect with database")
|
print("Trying to connect with database")
|
||||||
continue
|
continue
|
||||||
|
|||||||
@ -4,7 +4,17 @@ CREATE TABLE users (
|
|||||||
oauth_ID VARCHAR(255) NOT NULL
|
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
|
INSERT INTO users (username, oauth_ID) VALUES
|
||||||
('Mkyong', 40),
|
('Mkyong', 40),
|
||||||
('Ali', 28),
|
('Ali', 28),
|
||||||
('Teoh', 18);
|
('Teoh', 18);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user