Skip to content

Commit 3666675

Browse files
committed
setup the matches table and api access to it
1 parent ac90eba commit 3666675

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed
+40-2
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,55 @@
11
from flask import Blueprint, Response, jsonify, request
22

33
from api.consts import COMMON_API_PREFIX
4-
from api.model.matches import getAllMatches, getMatch, getMatchesByTeam
4+
from api.model.matches import getAllMatches, getMatch, getMatchesByTeam, insertMatch
55

66
matches_api = Blueprint(
77
"matches_api", __name__, url_prefix= COMMON_API_PREFIX + "/matches"
88
)
99

1010
@matches_api.route("/", methods=["GET"])
11-
def get_all():
11+
def api_get_all():
1212
try:
1313
responseObject = getAllMatches()
14+
return responseObject
1415
except Exception as e:
1516
print(e)
1617
return Response("smth went wrong", 500)
1718

19+
@matches_api.route("/" , methods=["POST", "PUT"])
20+
def api_insertMatch():
21+
if request.method == "POST":
22+
try:
23+
content = request.json
24+
teamid1 = content["teamid1"]
25+
teamid2 = content["teamid2"]
26+
except Exception as e:
27+
print(e)
28+
return Response("Bad reequest", 400)
29+
30+
try:
31+
insertMatch(teamid1, teamid2)
32+
return Response("done", 200)
33+
except Exception as e:
34+
print(e)
35+
return Response("something went wrong xxx", 500)
36+
37+
@matches_api.route("/match/<matchid>")
38+
def api_getByMatchId(matchid):
39+
try:
40+
responseObject = getMatch(matchid)
41+
return responseObject
42+
except Exception as e:
43+
print(e)
44+
return Response("smth went wrong", 500)
45+
46+
@matches_api.route("/team/<teamid>")
47+
def api_getByTeamId(teamid):
48+
try:
49+
responseObject = getMatchesByTeam(teamid)
50+
return responseObject
51+
except Exception as e:
52+
print(e)
53+
return Response("smth went wrong", 500)
54+
55+

backend/api/main.py

+2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from api.components.betting import betting_api
44
from api.components.hello import hello_api
5+
from api.components.matches import matches_api
56

67
api: Flask | None = None
78

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

1415
api.register_blueprint(hello_api)
1516
api.register_blueprint(betting_api)
17+
api.register_blueprint(matches_api)
1618

1719
return api

backend/api/model/matches/__init__.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
from .main import datacon, getAllMatches, getMatch, getMatchesByTeam
1+
from .main import datacon, getAllMatches, getMatch, getMatchesByTeam, insertMatch
22
import psycopg2
33

4-
__all__=["getAllMatches", "getMatch", "getMatchesByTeam"]
4+
__all__=["getAllMatches", "getMatch", "getMatchesByTeam", "insertMatch"]
55

66

77
createMatchTableString = """
88
CREATE TABLE matches (
99
matchid SERIAL PRIMARY KEY,
1010
teamid1 INT,
11-
teamid2 INT,
11+
teamid2 INT
1212
);
1313
"""
1414

backend/api/model/matches/main.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def getMatchesByTeam(teamid):
3636
def insertMatch(teamid1, teamid2):
3737
with datacon.cursor() as curs:
3838
curs.execute(
39-
f"""
40-
INSERT INTO matches (teamid1, teamid2) VALUES({teamid1}, {teamid2});
41-
"""
42-
)
39+
f"""
40+
INSERT INTO matches (teamid1, teamid2) VALUES({teamid1}, {teamid2});
41+
"""
42+
)

0 commit comments

Comments
 (0)