Skip to content

Commit

Permalink
established database connection with api
Browse files Browse the repository at this point in the history
  • Loading branch information
DaiseyD committed Jul 7, 2024
1 parent e8ff6a6 commit c3e3aa8
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 24 deletions.
3 changes: 3 additions & 0 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
7 changes: 4 additions & 3 deletions backend/api/components/betting/controller.py
Original file line number Diff line number Diff line change
@@ -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")
Expand All @@ -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\"
Expand Down
22 changes: 21 additions & 1 deletion backend/api/model/betting/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
from .main import *
import psycopg2

__all__ = ["insertBet", "removeBet"]

__all__ = ["insertBet", "removeBet"]

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__()
16 changes: 8 additions & 8 deletions backend/api/model/betting/main.py
Original file line number Diff line number Diff line change
@@ -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
20 changes: 13 additions & 7 deletions backend/requirements-combined.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion backend/requirements.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Flask==3.0.3
Flask==3.0.3
Psycopg2==2.9.9
6 changes: 4 additions & 2 deletions backend/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion backend/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)


Expand Down
8 changes: 7 additions & 1 deletion notes.txt
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
to connect to postgres from python: conn = psycopg2.connect(host="db", user="postgres", dbname="[the name of a database]")
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

0 comments on commit c3e3aa8

Please sign in to comment.