Skip to content
This repository has been archived by the owner on Jul 7, 2024. It is now read-only.

Commit

Permalink
feat(routes): add functionality to routes and further develop templates
Browse files Browse the repository at this point in the history
Signed-off-by: CyberFlame <[email protected]>
  • Loading branch information
CyberFlameGO committed Oct 12, 2023
1 parent f2488df commit 49dc84e
Show file tree
Hide file tree
Showing 16 changed files with 525 additions and 144 deletions.
2 changes: 1 addition & 1 deletion .idea/dataSources.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .idea/jsLibraryMappings.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/jsLinters/eslint.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

85 changes: 85 additions & 0 deletions .idea/jsLinters/jshint.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions .idea/prettier.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esversion": 11
}
79 changes: 77 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from typing import Annotated, AsyncGenerator, AsyncIterable, AsyncIterator

import uvicorn

import sentry_sdk
from fastapi import FastAPI, Request, Depends
from fastapi.responses import HTMLResponse
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from sqlalchemy.ext.asyncio import AsyncSession
Expand Down Expand Up @@ -55,13 +57,86 @@ async def home(request: Request):
)


@app.get("/games", response_class=HTMLResponse)
async def games_list(request: Request, session: Session):
"""
Games page
:param session:
:param request:
:return:
"""
games = await db.dump_all(session, models.Game)
return templates.TemplateResponse(
"games.html",
{
"request": request,
"games": games,
"can_mutate_games": True,
},
)


@app.post("/games", response_class=HTMLResponse)
async def new_game(request: Request, session: Session):
"""
New game page
:param request:
:param session:
:return:
"""
# This code gets the form data from the request
form = await request.form()
print(form)
game = models.Game(name=form.get("name"), description=form.get("description"))
await db.insert(session, game)
return RedirectResponse("/games", status_code=303)


@app.patch("/games/{id}", response_class=HTMLResponse)
async def update_game(request: Request, session: Session):
"""
Update game page
TODO: Fix - adjust JS and create check to see which field was modified for the row.
:param request:
:param session:
:return:
"""
# This code gets the form data from the request
form = await request.form()
print(form)
game = await db.retrieve(session, models.Game, int(form.get("id")))
game.name = form.get("name")
game.description = form.get("description")
await db.update(
session, models.Game, int(form.get("id")), {"name": form.get("name"), "description": form.get("description")}
)
return RedirectResponse("/games", status_code=303)


@app.delete("/games", response_class=HTMLResponse)
async def delete_game(request: Request, session: Session):
"""
Delete game page
:param request:
:param session:
:return:
"""
# This code gets the form data from the request
form = await request.form()
print(form)
await db.delete(session, models.Game, int(form.get("id")))
return RedirectResponse("/games", status_code=303)


@app.get("/leaderboard", response_class=HTMLResponse)
async def leaderboard(request: Request):
async def leaderboard(request: Request, session: Session):
"""
Leaderboard page
:param session:
:param request:
:return:
"""
rankings = await db.dump_by_field_descending(session, models.MatchResult.won_id, "wins", 10)
return templates.TemplateResponse(
"leaderboard.html",
{"request": request, "users": utils.get_users()},
Expand Down
Binary file modified data.db
Binary file not shown.
7 changes: 1 addition & 6 deletions models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ class Base(AsyncAttrs, DeclarativeBase):
"""

__abstract__: bool = True
id: Mapped[int] = mapped_column(autoincrement=True, nullable=False, unique=True, primary_key=True)


class Game(Base):
__tablename__: str = "games"

id: Mapped[int] = mapped_column(autoincrement=True, nullable=False, unique=True, primary_key=True)
match: Mapped[Set["Match"]] = relationship()
name: Mapped[str] = mapped_column()
description: Mapped[str] = mapped_column()
Expand All @@ -29,7 +29,6 @@ class Game(Base):
class User(Base):
__tablename__: str = "users"

id: Mapped[int] = mapped_column(autoincrement=True, nullable=False, unique=True, primary_key=True)
email: Mapped[str] = mapped_column(unique=True, nullable=False)
username: Mapped[str] = mapped_column(unique=True, nullable=False)
password: Mapped[str] = mapped_column(unique=True, nullable=False)
Expand All @@ -45,7 +44,6 @@ class User(Base):
class Match(Base):
__tablename__: str = "matches"

id: Mapped[int] = mapped_column(autoincrement=True, nullable=False, unique=True, primary_key=True)
game_id: Mapped[int] = mapped_column(ForeignKey("games.id"), nullable=False)
played_at: Mapped[datetime] = mapped_column(default=datetime.utcnow)

Expand All @@ -58,8 +56,6 @@ class Match(Base):
class MatchPlayers(Base):
__tablename__: str = "matchplayers"

id: Mapped[int] = mapped_column(autoincrement=True, nullable=False, unique=True, primary_key=True)

match: Mapped["Match"] = relationship(back_populates="players")
match_id: Mapped[int] = mapped_column(ForeignKey("matches.id"), nullable=False)
player: Mapped["User"] = relationship(back_populates="player")
Expand All @@ -69,7 +65,6 @@ class MatchPlayers(Base):
class MatchResult(Base):
__tablename__: str = "matchresults"

id: Mapped[int] = mapped_column(autoincrement=True, nullable=False, unique=True, primary_key=True)
match: Mapped["Match"] = relationship(back_populates="results")
match_id: Mapped[int] = mapped_column(ForeignKey("matches.id"), unique=True, nullable=False)

Expand Down
Loading

0 comments on commit 49dc84e

Please sign in to comment.