Skip to content

Commit

Permalink
Merge pull request #8 from DoodleyJC/dev
Browse files Browse the repository at this point in the history
Database connection and betting endpoint update
  • Loading branch information
amine4567 authored Jul 20, 2024
2 parents d91d337 + bdcf172 commit 70d1e57
Show file tree
Hide file tree
Showing 17 changed files with 158 additions and 16 deletions.
13 changes: 11 additions & 2 deletions .devcontainer/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@ services:
dockerfile: Dockerfile
entrypoint: ["tail", "-f", "/dev/null"]
volumes:
- ../..:/workspaces:cached
- ..:/workspaces:cached
command: sleep infinity
network_mode: service:db
#network_mode: service:db
ports:
- "8000:5000"
depends_on:
- db
db:
image: postgres:16-alpine
restart: unless-stopped
volumes:
- postgres-data:/var/lib/postgresql/data
environment:
POSTGRES_HOST_AUTH_METHOD: trust
POSTGRES_DB: retroolympics
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
ports:
- "8001:5432"

volumes:
postgres-data:
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,6 @@ build
next-env.d.ts

# Python
*.pyc
*.pyc

.env
3 changes: 3 additions & 0 deletions backend/api/components/betting/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .controller import betting_api

__all__=["betting_api"]
48 changes: 48 additions & 0 deletions backend/api/components/betting/controller.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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")


@betting_api.route("/", methods=["GET"])
def get_collections():
try:
responseobject = api.model.betting.getAll()
return jsonify(responseobject)
except Exception as e:
print(e)
return Response("something went wrong", 500)



@betting_api.route("/", methods=["POST", "PUT"])
def handle_post():
if request.method == "POST":
try:
content = request.json
userid = content["userid"]
matchid = content["matchid"]
teamid = content["teamid"]
amount = content["amount"]
except Exception as e:
print(e)
return Response(f"""content_type="bad request, please supply the following: \"user\", \"matchid\", \"teamid\"and \"amount\"
in json form with a content-type json header"""
, 400)
try:
print(f"userid = {userid}, matchid={matchid}, teamid{teamid}, amount={amount}")
api.model.betting.insertBet(userid, matchid, teamid, amount)
return Response("inserted", 200)
except Exception as e:
print(e)
return Response(f"""something went wrong with inserting the bet in the database"""
, 500)
else:
return Response("Only posts are allowed right now", 400)

@betting_api.route("/datatest", methods=["GET", "POST", "PUT"])
def handle_test():
pass
Empty file.
8 changes: 8 additions & 0 deletions backend/api/components/betting/templates/notallowed.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>

</head>
<body>
<p> this request type is not allowed, only post</p>
</body>
</html>
8 changes: 8 additions & 0 deletions backend/api/components/betting/templates/placeholder.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<html>
<head>

</head>
<body>
<p> Placeholder</p>
</body>
</html>
3 changes: 2 additions & 1 deletion backend/api/main.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from flask import Flask

from api.components.hello import hello_api

from api.components.betting import betting_api
api: Flask | None = None


Expand All @@ -11,5 +11,6 @@ def get_api() -> Flask:
api = Flask(__name__)

api.register_blueprint(hello_api)
api.register_blueprint(betting_api)

return api
25 changes: 25 additions & 0 deletions backend/api/model/betting/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from .main import *
import psycopg2

__all__ = ["insertBet", "removeBet"]


createTableString = """
CREATE TABLE bettingtest (
id SERIAL PRIMARY KEY,
userid VARCHAR,
matchid INT,
teamid INT,
amount FLOAT
);
"""

def __init__():
with datacon.cursor() as curs:
try:
curs.execute(createTableString)
except psycopg2.errors.DuplicateTable as e:
print("duplicate table encountered")
pass

__init__()
21 changes: 21 additions & 0 deletions backend/api/model/betting/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import psycopg2
import api.model.database as data

datacon = data.bettingconn
datacon.autocommit=True

def insertBet(userid, matchid, teamid, amount):
with datacon.cursor() as curs:
curs.execute(f"""INSERT INTO bettingtest (userid, matchid, teamid, amount)
VALUES ({userid},{matchid},{teamid},{amount});""")

def getAll():
with datacon.cursor() as curs:
curs.execute("""
SELECT * FROM bettingtest;
""")
res = curs.fetchall()
return res

def removeBet():
pass
1 change: 1 addition & 0 deletions backend/api/model/database/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .main import *
5 changes: 5 additions & 0 deletions backend/api/model/database/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import psycopg2
bettingconn = psycopg2.connect(host="db", user="postgres", password="postgres", dbname="retroolympics")
bettingconn.autocommit=True


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
4 changes: 2 additions & 2 deletions backend/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ 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"
)


# Run
@task
def run_back(c):
get_api().run()
get_api().run("0.0.0.0")
2 changes: 2 additions & 0 deletions installscript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pip install invoke
pip install -r backend/requirements-combined.txt

0 comments on commit 70d1e57

Please sign in to comment.