diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml index 1d447c5..129d383 100644 --- a/.devcontainer/docker-compose.yml +++ b/.devcontainer/docker-compose.yml @@ -21,6 +21,9 @@ services: - postgres-data:/var/lib/postgresql/data environment: POSTGRES_HOST_AUTH_METHOD: trust + POSTGRES_DB: betting + POSTGRES_USER: postgres + POSTGRES_PASSWORD: postgres ports: - "8001:5432" diff --git a/backend/api/components/betting/controller.py b/backend/api/components/betting/controller.py index 20f4cdf..84c6eb0 100644 --- a/backend/api/components/betting/controller.py +++ b/backend/api/components/betting/controller.py @@ -1,6 +1,7 @@ from flask import Blueprint, jsonify, request, render_template, Response from api.consts import COMMON_API_PREFIX +import api.model.betting betting_api = Blueprint("betting_api", __name__, url_prefix=COMMON_API_PREFIX + "/betting", template_folder="templates") @@ -17,13 +18,13 @@ def handle_post(): if request.method == "POST": try: content = request.json - userid = content["user"] - print(f"userid = {userid}") + userid = content["userid"] matchid = content["matchid"] teamid = content["teamid"] amount = content["amount"] print(f"userid = {userid}, matchid={matchid}, teamid{teamid}, amount={amount}") - return render_template("placeholder.html") + api.model.betting.insertBet(userid, matchid, teamid, amount) + return Response("inserted", 200) except Exception as e: print(e) return Response(f"""content_type="bad request, please supply the following: \"user\", \"matchid\", \"teamid\"and \"amount\" diff --git a/backend/api/model/betting/__init__.py b/backend/api/model/betting/__init__.py index 813cd71..a9e5cf9 100644 --- a/backend/api/model/betting/__init__.py +++ b/backend/api/model/betting/__init__.py @@ -1,4 +1,24 @@ from .main import * +import psycopg2 +__all__ = ["insertBet", "removeBet"] -__all__ = ["insertBet", "removeBet"] \ No newline at end of file + +createTableString = """ + CREATE TABLE bettingtest ( + id SERIAL PRIMARY KEY, + userid VARCHAR, + matchid INT, + teamid INT, + amount FLOAT + ); + """ + +def __init__(): + with bettingconn.cursor() as curs: + try: + curs.execute(createTableString) + except psycopg2.errors.DuplicateTable as e: + print("duplicate table encountered") + pass +__init__() \ No newline at end of file diff --git a/backend/api/model/betting/main.py b/backend/api/model/betting/main.py index fe2d499..b1d17e7 100644 --- a/backend/api/model/betting/main.py +++ b/backend/api/model/betting/main.py @@ -1,13 +1,13 @@ +import psycopg2 +bettingconn = psycopg2.connect(host="db", user="postgres", password="postgres", dbname="betting") +bettingconn.autocommit=True - - - - - - -def insertBet(): - pass +def insertBet(userid, matchid, teamid, amount): + with bettingconn.cursor() as curs: + curs.execute(f"""INSERT INTO bettingtest (userid, matchid, teamid, amount) + VALUES ({userid},{matchid},{teamid},{amount});""") + def removeBet(): pass diff --git a/backend/requirements-combined.txt b/backend/requirements-combined.txt index 159ca8b..01df805 100644 --- a/backend/requirements-combined.txt +++ b/backend/requirements-combined.txt @@ -2,10 +2,10 @@ # This file is autogenerated by pip-compile with Python 3.12 # by the following command: # -# pip-compile --output-file=backend/requirements-combined.txt backend/requirements-dev.in backend/requirements.in +# pip-compile --output-file=requirements-combined.txt requirements-dev.in requirements.in # black==24.4.2 - # via -r backend/requirements-dev.in + # via -r requirements-dev.in blinker==1.8.2 # via flask build==1.2.1 @@ -15,12 +15,16 @@ click==8.1.7 # black # flask # pip-tools +colorama==0.4.6 + # via + # build + # click flask==3.0.3 - # via -r backend/requirements.in + # via -r requirements.in invoke==2.2.0 - # via -r backend/requirements-dev.in + # via -r requirements-dev.in isort==5.13.2 - # via -r backend/requirements-dev.in + # via -r requirements-dev.in itsdangerous==2.2.0 # via flask jinja2==3.1.4 @@ -38,15 +42,17 @@ packaging==24.1 pathspec==0.12.1 # via black pip-tools==7.4.1 - # via -r backend/requirements-dev.in + # via -r requirements-dev.in platformdirs==4.2.2 # via black +psycopg2==2.9.9 + # via -r requirements.in pyproject-hooks==1.1.0 # via # build # pip-tools ruff==0.4.9 - # via -r backend/requirements-dev.in + # via -r requirements-dev.in werkzeug==3.0.3 # via flask wheel==0.43.0 diff --git a/backend/requirements.in b/backend/requirements.in index a993b8d..ff4b2c5 100644 --- a/backend/requirements.in +++ b/backend/requirements.in @@ -1 +1,2 @@ -Flask==3.0.3 \ No newline at end of file +Flask==3.0.3 +Psycopg2==2.9.9 \ No newline at end of file diff --git a/backend/requirements.txt b/backend/requirements.txt index 8793ec9..6a81d6e 100644 --- a/backend/requirements.txt +++ b/backend/requirements.txt @@ -2,14 +2,16 @@ # This file is autogenerated by pip-compile with Python 3.12 # by the following command: # -# pip-compile backend/requirements.in +# pip-compile requirements.in # blinker==1.8.2 # via flask click==8.1.7 # via flask +colorama==0.4.6 + # via click flask==3.0.3 - # via -r backend/requirements.in + # via -r requirements.in itsdangerous==2.2.0 # via flask jinja2==3.1.4 diff --git a/backend/tasks.py b/backend/tasks.py index 1bf0de1..0b3f2ae 100644 --- a/backend/tasks.py +++ b/backend/tasks.py @@ -12,7 +12,7 @@ def update_app_reqs(c): @task def update_combined_reqs(c): c.run( - "pip-compile backend/requirements.in backend/requirements-dev.in -o backend/requirements-combined.txt" + "pip-compile requirements.in requirements-dev.in -o requirements-combined.txt" ) diff --git a/notes.txt b/notes.txt index 4e4840a..7ee63e3 100644 --- a/notes.txt +++ b/notes.txt @@ -1 +1,7 @@ -to connect to postgres from python: conn = psycopg2.connect(host="db", user="postgres", dbname="[the name of a database]") \ No newline at end of file +to connect to postgres from python: conn = psycopg2.connect(host="db", user="postgres", dbname="[the name of a database]") + + +psql info: \l to list databases + \c [database_name] to go into database + \dt to list all tables in current database + sql statements when u are in the database \ No newline at end of file