This repository has been archived by the owner on Jul 7, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(games): add all backend functionality for frontend features
Signed-off-by: CyberFlame <[email protected]>
- Loading branch information
1 parent
49dc84e
commit bff8248
Showing
11 changed files
with
277 additions
and
173 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"trailingComma": "es5", | ||
"tabWidth": 4, | ||
"semi": true, | ||
"singleQuote": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
|
@@ -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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.