fix: from the courses view it is now possible to click on game and be

redirected to that games page.
This commit is contained in:
iritmaximus
2023-03-04 13:33:37 +02:00
parent eb09216f27
commit 1a04254684
3 changed files with 29 additions and 5 deletions
+3 -3
View File
@@ -17,9 +17,9 @@ table, tr, td {
</tr> </tr>
{% for item in courses %} {% for item in courses %}
<tr> <tr>
<td><a href="/courses/{{item[0]}}">{{item[0]}}</a></td> <td><a href="/courses/{{item[0].replace(' ', '%20')}}">{{item[0]}}</a></td>
<td><a href="/games/{{item[1]}}">{{item[1]}}</a></td> <td><a href="/api/game/{{item[1].replace(' ', '%20')}}">{{item[1]}}</a></td>
<td><a href="/cups/{{item[2]}}">{{item[2]}}</a></td> <td><a href="/cups/{{item[2].replace(' ', '%20')}}">{{item[2]}}</a></td>
</tr> </tr>
{% endfor %} {% endfor %}
{% endif %} {% endif %}
+9
View File
@@ -7,6 +7,15 @@ def query_games():
return result return result
def query_game_by_name(game_name: str):
sql = "SELECT id FROM games WHERE games.name=:game_name"
result = db.session.execute(sql, {"game_name": game_name}).fetchone()
if result:
return result[0]
else:
raise ValueError(f"No game with name {game_name} found")
def get_game_name(id: int): def get_game_name(id: int):
sql = f"SELECT name FROM games WHERE id=:id" sql = f"SELECT name FROM games WHERE id=:id"
result = db.session.execute(sql, {"id": id}).fetchone() result = db.session.execute(sql, {"id": id}).fetchone()
+17 -2
View File
@@ -1,6 +1,11 @@
from flask import render_template from flask import render_template, redirect
from flaskr import app from flaskr import app
from flaskr.utils.games import query_games, get_game_name, get_all_times_in_game from flaskr.utils.games import (
query_games,
get_game_name,
get_all_times_in_game,
query_game_by_name,
)
@app.route("/games") @app.route("/games")
@@ -22,3 +27,13 @@ def get_game_by_id(game_id):
) )
times = get_all_times_in_game(game_id) times = get_all_times_in_game(game_id)
return render_template("game_id.html", game_name=game_name, times=times) return render_template("game_id.html", game_name=game_name, times=times)
@app.route("/api/game/<string:game_name>")
def get_game_by_name(game_name: str):
try:
game_name = str(game_name).replace("%20", " ")
game_id = query_game_by_name(game_name)
return redirect(f"/games/{game_id}")
except ValueError as e:
return render_template("error.html", message=f"Error fetching game name, {e}")