mirror of
https://github.com/kuhyx/WUT_Computer_Science.git
synced 2026-07-04 18:43:15 +02:00
postgres baza
This commit is contained in:
parent
a7812d4037
commit
903a4ccf05
1
ai_front_connector/Dockerfile
Normal file
1
ai_front_connector/Dockerfile
Normal file
@ -0,0 +1 @@
|
||||
FROM python:3.10.4-slim-bullseye
|
||||
18
ai_front_connector/README.md
Normal file
18
ai_front_connector/README.md
Normal file
@ -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/<string:username> - info o userach
|
||||
|
||||
POST http://localhost:8080/api/v3/add/<string:oauth_ID>/<string:username> - dodawanie usera
|
||||
|
||||
GET http://localhost:8080/api/v3/ai/<string:oauth_ID> - wyciąganie rekomendacji od AI
|
||||
|
||||
26
ai_front_connector/docker-compose.yml
Normal file
26
ai_front_connector/docker-compose.yml
Normal file
@ -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:
|
||||
@ -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/<string:oauth_ID>/<string:username>", 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()
|
||||
|
||||
|
||||
10
ai_front_connector/init_scripts/init.sql
Normal file
10
ai_front_connector/init_scripts/init.sql
Normal file
@ -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);
|
||||
Loading…
Reference in New Issue
Block a user