|
| 1 | +import uuid |
| 2 | +from datetime import UTC, datetime |
| 3 | + |
| 4 | +from fastapi import Depends, HTTPException |
| 5 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 6 | + |
| 7 | +from app.core import models_core |
| 8 | +from app.core.groups.groups_type import GroupType |
| 9 | +from app.core.module import Module |
| 10 | +from app.dependencies import get_db, is_user_a_member |
| 11 | +from app.modules.flappybird import ( |
| 12 | + cruds_flappybird, |
| 13 | + models_flappybird, |
| 14 | + schemas_flappybird, |
| 15 | +) |
| 16 | + |
| 17 | +module = Module( |
| 18 | + root="flappybird", |
| 19 | + tag="Flappy Bird", |
| 20 | + default_allowed_groups_ids=[GroupType.student], |
| 21 | +) |
| 22 | + |
| 23 | + |
| 24 | +@module.router.get( |
| 25 | + "/flappybird/scores", |
| 26 | + response_model=list[schemas_flappybird.FlappyBirdScoreInDB], |
| 27 | + status_code=200, |
| 28 | +) |
| 29 | +async def get_flappybird_score( |
| 30 | + skip: int = 0, |
| 31 | + limit: int = 10, |
| 32 | + db: AsyncSession = Depends(get_db), |
| 33 | +): |
| 34 | + """Return the leaderboard score of the skip...limit""" |
| 35 | + leaderboard = await cruds_flappybird.get_flappybird_score_leaderboard( |
| 36 | + db=db, |
| 37 | + skip=skip, |
| 38 | + limit=limit, |
| 39 | + ) |
| 40 | + return leaderboard |
| 41 | + |
| 42 | + |
| 43 | +@module.router.get( |
| 44 | + "/flappybird/scores/me", |
| 45 | + status_code=200, |
| 46 | + response_model=schemas_flappybird.FlappyBirdScoreCompleteFeedBack, |
| 47 | +) |
| 48 | +async def get_current_user_flappybird_personal_best( |
| 49 | + db: AsyncSession = Depends(get_db), |
| 50 | + user: models_core.CoreUser = Depends(is_user_a_member), |
| 51 | +): |
| 52 | + user_personal_best_table = ( |
| 53 | + await cruds_flappybird.get_flappybird_personal_best_by_user_id( |
| 54 | + db=db, |
| 55 | + user_id=user.id, |
| 56 | + ) |
| 57 | + ) |
| 58 | + |
| 59 | + if user_personal_best_table is None: |
| 60 | + raise HTTPException( |
| 61 | + status_code=404, |
| 62 | + detail="Not found", |
| 63 | + ) |
| 64 | + |
| 65 | + position = await cruds_flappybird.get_flappybird_score_position( |
| 66 | + db=db, |
| 67 | + score_value=user_personal_best_table.value, |
| 68 | + ) |
| 69 | + if position is None: |
| 70 | + raise HTTPException( |
| 71 | + status_code=404, |
| 72 | + detail="Not found", |
| 73 | + ) |
| 74 | + user_personal_best = schemas_flappybird.FlappyBirdScoreCompleteFeedBack( |
| 75 | + value=user_personal_best_table.value, |
| 76 | + user=user_personal_best_table.user, |
| 77 | + creation_time=user_personal_best_table.creation_time, |
| 78 | + position=position, |
| 79 | + ) |
| 80 | + |
| 81 | + return user_personal_best |
| 82 | + |
| 83 | + |
| 84 | +@module.router.post( |
| 85 | + "/flappybird/scores", |
| 86 | + response_model=schemas_flappybird.FlappyBirdScoreBase, |
| 87 | + status_code=201, |
| 88 | +) |
| 89 | +async def create_flappybird_score( |
| 90 | + flappybird_score: schemas_flappybird.FlappyBirdScoreBase, |
| 91 | + user: models_core.CoreUser = Depends(is_user_a_member), |
| 92 | + db: AsyncSession = Depends(get_db), |
| 93 | +): |
| 94 | + # Currently, flappybird_score is a schema instance |
| 95 | + # To add it to the database, we need to create a model |
| 96 | + |
| 97 | + # We need to generate a new UUID for the score |
| 98 | + score_id = uuid.uuid4() |
| 99 | + # And get the current date and time |
| 100 | + creation_time = datetime.now(UTC) |
| 101 | + |
| 102 | + db_flappybird_score = models_flappybird.FlappyBirdScore( |
| 103 | + id=score_id, |
| 104 | + user_id=user.id, |
| 105 | + value=flappybird_score.value, |
| 106 | + creation_time=creation_time, |
| 107 | + # We add all informations contained in the schema |
| 108 | + ) |
| 109 | + try: |
| 110 | + return await cruds_flappybird.create_flappybird_score( |
| 111 | + flappybird_score=db_flappybird_score, |
| 112 | + db=db, |
| 113 | + ) |
| 114 | + except ValueError as error: |
| 115 | + raise HTTPException(status_code=400, detail=str(error)) |
0 commit comments