Skip to content

Commit

Permalink
setup the matches table and api access to it
Browse files Browse the repository at this point in the history
  • Loading branch information
DaiseyD committed Aug 1, 2024
1 parent ac90eba commit 3666675
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 9 deletions.
42 changes: 40 additions & 2 deletions backend/api/components/matches/controller.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,55 @@
from flask import Blueprint, Response, jsonify, request

from api.consts import COMMON_API_PREFIX
from api.model.matches import getAllMatches, getMatch, getMatchesByTeam
from api.model.matches import getAllMatches, getMatch, getMatchesByTeam, insertMatch

matches_api = Blueprint(
"matches_api", __name__, url_prefix= COMMON_API_PREFIX + "/matches"
)

@matches_api.route("/", methods=["GET"])
def get_all():
def api_get_all():
try:
responseObject = getAllMatches()
return responseObject
except Exception as e:
print(e)
return Response("smth went wrong", 500)

@matches_api.route("/" , methods=["POST", "PUT"])
def api_insertMatch():
if request.method == "POST":
try:
content = request.json
teamid1 = content["teamid1"]
teamid2 = content["teamid2"]
except Exception as e:
print(e)
return Response("Bad reequest", 400)

try:
insertMatch(teamid1, teamid2)
return Response("done", 200)
except Exception as e:
print(e)
return Response("something went wrong xxx", 500)

@matches_api.route("/match/<matchid>")
def api_getByMatchId(matchid):
try:
responseObject = getMatch(matchid)
return responseObject
except Exception as e:
print(e)
return Response("smth went wrong", 500)

@matches_api.route("/team/<teamid>")
def api_getByTeamId(teamid):
try:
responseObject = getMatchesByTeam(teamid)
return responseObject
except Exception as e:
print(e)
return Response("smth went wrong", 500)


2 changes: 2 additions & 0 deletions backend/api/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from api.components.betting import betting_api
from api.components.hello import hello_api
from api.components.matches import matches_api

api: Flask | None = None

Expand All @@ -13,5 +14,6 @@ def get_api() -> Flask:

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

return api
6 changes: 3 additions & 3 deletions backend/api/model/matches/__init__.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from .main import datacon, getAllMatches, getMatch, getMatchesByTeam
from .main import datacon, getAllMatches, getMatch, getMatchesByTeam, insertMatch
import psycopg2

__all__=["getAllMatches", "getMatch", "getMatchesByTeam"]
__all__=["getAllMatches", "getMatch", "getMatchesByTeam", "insertMatch"]


createMatchTableString = """
CREATE TABLE matches (
matchid SERIAL PRIMARY KEY,
teamid1 INT,
teamid2 INT,
teamid2 INT
);
"""

Expand Down
8 changes: 4 additions & 4 deletions backend/api/model/matches/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def getMatchesByTeam(teamid):
def insertMatch(teamid1, teamid2):
with datacon.cursor() as curs:
curs.execute(
f"""
INSERT INTO matches (teamid1, teamid2) VALUES({teamid1}, {teamid2});
"""
)
f"""
INSERT INTO matches (teamid1, teamid2) VALUES({teamid1}, {teamid2});
"""
)

0 comments on commit 3666675

Please sign in to comment.