From 903a4ccf05835961f1d0b508d9a61076129e8f9a Mon Sep 17 00:00:00 2001 From: Hubert Dwornik Date: Sun, 12 May 2024 12:10:38 +0200 Subject: [PATCH] postgres baza --- ai_front_connector/Dockerfile | 1 + ai_front_connector/README.md | 18 ++++++++++++++ ai_front_connector/docker-compose.yml | 26 +++++++++++++++++++++ ai_front_connector/frontend_AI_connector.py | 26 ++++++++++++++++++--- ai_front_connector/init_scripts/init.sql | 10 ++++++++ 5 files changed, 78 insertions(+), 3 deletions(-) create mode 100644 ai_front_connector/Dockerfile create mode 100644 ai_front_connector/README.md create mode 100644 ai_front_connector/docker-compose.yml create mode 100644 ai_front_connector/init_scripts/init.sql diff --git a/ai_front_connector/Dockerfile b/ai_front_connector/Dockerfile new file mode 100644 index 00000000..6002bff0 --- /dev/null +++ b/ai_front_connector/Dockerfile @@ -0,0 +1 @@ +FROM python:3.10.4-slim-bullseye \ No newline at end of file diff --git a/ai_front_connector/README.md b/ai_front_connector/README.md new file mode 100644 index 00000000..1a1d3ba7 --- /dev/null +++ b/ai_front_connector/README.md @@ -0,0 +1,18 @@ +# instalacja: +pip install -r requirements.txt + +# uruchomienie: +(Z directory macierzystego) + +python3 ai_front_connector/frontend_AI_connector.py + +# gadanie z endpointami + +GET http://localhost:8080/ - testowy domowy + +GET http://localhost:8080//api/v3/get/ - info o userach + +POST http://localhost:8080/api/v3/add// - dodawanie usera + +GET http://localhost:8080/api/v3/ai/ - wyciąganie rekomendacji od AI + diff --git a/ai_front_connector/docker-compose.yml b/ai_front_connector/docker-compose.yml new file mode 100644 index 00000000..b8234ac6 --- /dev/null +++ b/ai_front_connector/docker-compose.yml @@ -0,0 +1,26 @@ +version: "3.8" + +services: + db: + container_name: db_container + image: postgres:13 + environment: + POSTGRES_USER: root + POSTGRES_PASSWORD: root + POSTGRES_DB: test_db + ports: + - 5432:5432 + volumes: + - ./init_scripts/init.sql:/docker-entrypoint-initdb.d/init.sql + - postgres_data:/var/lib/postgresql/data/ + pgadmin: + container_name: admin_container + image: dpage/pgadmin4 + ports: + - 8080:80 + environment: + PGADMIN_DEFAULT_EMAIL: admin@admin.com + PGADMIN_DEFAULT_PASSWORD: admin + +volumes: + postgres_data: \ No newline at end of file diff --git a/ai_front_connector/frontend_AI_connector.py b/ai_front_connector/frontend_AI_connector.py index bcfd9c1c..6c882962 100644 --- a/ai_front_connector/frontend_AI_connector.py +++ b/ai_front_connector/frontend_AI_connector.py @@ -1,5 +1,6 @@ from flask import Flask, request, jsonify import tinydb +import psycopg2 files = [] @@ -7,6 +8,7 @@ DB_PATH = "ai_front_connector/mock_db/db.json" app = Flask(__name__) db_connector = None +conn = None @app.route("/", methods=["GET"]) def hello(): @@ -21,12 +23,23 @@ def access_user(username): #id z oautha oraz login @app.route("/api/v3/add//", methods=["POST"]) def add_user(oauth_ID, username): - res = db_connector.search(tinydb.where('username') == username) + # res = db_connector.search(tinydb.where('username') == username) + + cursor = conn.cursor() + cursor.execute("select * from users where username='{}';".format(username)) + res = cursor.fetchall() if len(res): return jsonify({"status": "User already exists"}), 500 - db_connector.insert({"ID": oauth_ID, "username": username}) + # db_connector.insert({"ID": oauth_ID, "username": username}) + cursor.execute("INSERT INTO users (username, oauth_ID) VALUES ('{}','{}');".format( + oauth_ID, username + )) + + conn.commit() + cursor.close() + return jsonify({"status": "success"}), 200 @@ -40,7 +53,14 @@ def get_recommendations(oauth_ID): if __name__ == "__main__": db_connector = tinydb.TinyDB(DB_PATH) + conn = psycopg2.connect( + host="localhost", + database="test_db", + user="root", + password="root", + port=5432 + ) app.run(port=8080, debug=True) - + conn.close() diff --git a/ai_front_connector/init_scripts/init.sql b/ai_front_connector/init_scripts/init.sql new file mode 100644 index 00000000..d555228e --- /dev/null +++ b/ai_front_connector/init_scripts/init.sql @@ -0,0 +1,10 @@ +CREATE TABLE users ( + id SERIAL PRIMARY KEY, + username VARCHAR(255) NOT NULL, + oauth_ID VARCHAR(255) NOT NULL +); + +INSERT INTO users (username, oauth_ID) VALUES + ('Mkyong', 40), + ('Ali', 28), + ('Teoh', 18); \ No newline at end of file