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

Commit

Permalink
feat(games): add all backend functionality for frontend features
Browse files Browse the repository at this point in the history
Signed-off-by: CyberFlame <[email protected]>
  • Loading branch information
CyberFlameGO committed Oct 16, 2023
1 parent 49dc84e commit bff8248
Show file tree
Hide file tree
Showing 11 changed files with 277 additions and 173 deletions.
2 changes: 1 addition & 1 deletion .idea/DTSCodingDB.iml

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

16 changes: 16 additions & 0 deletions .idea/jsLinters/jslint.xml

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

2 changes: 1 addition & 1 deletion .idea/misc.xml

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

6 changes: 6 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"trailingComma": "es5",
"tabWidth": 4,
"semi": true,
"singleQuote": true
}
61 changes: 35 additions & 26 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
from typing import Annotated, AsyncGenerator, AsyncIterable, AsyncIterator

import uvicorn
from typing import Annotated

import sentry_sdk
from fastapi import FastAPI, Request, Depends
from fastapi.responses import HTMLResponse, RedirectResponse
from fastapi import Depends, FastAPI, Request, status
from fastapi.responses import HTMLResponse, RedirectResponse, Response
from fastapi.staticfiles import StaticFiles
from fastapi.templating import Jinja2Templates
from sqlalchemy.exc import IntegrityError, NoResultFound
from sqlalchemy.ext.asyncio import AsyncSession

import models
import utils
from utils import Database

sentry_sdk.init(
dsn="https://[email protected]/4504873492480000",
Expand Down Expand Up @@ -88,44 +86,55 @@ async def new_game(request: Request, session: Session):
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)
try:
await db.insert(session, game)
except IntegrityError:
return Response(status_code=status.HTTP_409_CONFLICT)
return RedirectResponse("/games", status_code=status.HTTP_303_SEE_OTHER)


@app.patch("/games/{id}", response_class=HTMLResponse)
async def update_game(request: Request, session: Session):
@app.patch("/games/{game_id}", response_class=HTMLResponse)
async def update_game(request: Request, session: Session, game_id: int):
"""
Update game page
TODO: Fix - adjust JS and create check to see which field was modified for the row.
TODO: Utilise has_existed to return 410 Gone if record was deleted and existed, or 404 if it hasn't existed
:param game_id:
: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):
req_data: dict = await request.json()
print(game_id)
print(req_data)
try:
await db.update(session, models.Game, game_id, req_data)
except IntegrityError as e:
print(e)
return Response(status_code=status.HTTP_409_CONFLICT)
except NoResultFound:
if await db.has_existed(session, models.Game, game_id):
return Response(status_code=status.HTTP_410_GONE)
else:
return Response(status_code=status.HTTP_404_NOT_FOUND)
return Response(status_code=status.HTTP_204_NO_CONTENT)


@app.delete("/games/{game_id}", response_class=HTMLResponse)
async def delete_game(request: Request, session: Session, game_id: int):
"""
Delete game page
Route for game record deletion - does not check if the game exists before deletion
:param game_id:
: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)
await db.remove_record(session, models.Game, game_id)
return Response(status_code=status.HTTP_204_NO_CONTENT)


@app.get("/leaderboard", response_class=HTMLResponse)
Expand Down
Binary file modified data.db
Binary file not shown.
4 changes: 2 additions & 2 deletions models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class Game(Base):
__tablename__: str = "games"

match: Mapped[Set["Match"]] = relationship()
name: Mapped[str] = mapped_column()
description: Mapped[str] = mapped_column()
name: Mapped[str] = mapped_column(nullable=False, unique=True)
description: Mapped[str] = mapped_column(nullable=False)


class User(Base):
Expand Down
Loading

0 comments on commit bff8248

Please sign in to comment.