-
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.
Merge pull request #13 from 2jun0/GDET-24
GDET-24: 퀴즈 정답 제출 db 저장
- Loading branch information
Showing
9 changed files
with
143 additions
and
36 deletions.
There are no files selected for viewing
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,3 +1,4 @@ | ||
[pytest] | ||
env_override_existing_values = 1 | ||
env_files = | ||
.test.env |
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
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
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
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,42 @@ | ||
from datetime import datetime | ||
from typing import Optional | ||
|
||
from sqlalchemy.orm import selectinload | ||
from sqlmodel import select | ||
from sqlmodel.ext.asyncio.session import AsyncSession | ||
|
||
from ..game.model import GameScreenshot | ||
from ..repository import CRUDMixin, IRepository | ||
from .model import Quiz, QuizSubmit | ||
|
||
|
||
class QuizRepository(IRepository[Quiz], CRUDMixin): | ||
model = Quiz | ||
|
||
def __init__(self, session: AsyncSession) -> None: | ||
self._session = session | ||
|
||
async def get_with_game(self, *, id: int) -> Optional[Quiz]: | ||
stmt = ( | ||
select(Quiz) | ||
.where(Quiz.id == id) | ||
.options(selectinload(Quiz.screenshots).selectinload(GameScreenshot.game)) # type: ignore | ||
) | ||
rs = await self._session.exec(stmt) | ||
return rs.first() | ||
|
||
async def get_by_created_at_interval_with_screenshots(self, *, start_at: datetime, end_at: datetime): | ||
stmts = ( | ||
select(Quiz) | ||
.where(Quiz.created_at >= start_at, Quiz.created_at <= end_at) | ||
.options(selectinload(Quiz.screenshots)) # type: ignore | ||
) | ||
rs = await self._session.exec(stmts) | ||
return rs.all() | ||
|
||
|
||
class QuizSubmitRepository(IRepository[QuizSubmit], CRUDMixin): | ||
model = QuizSubmit | ||
|
||
def __init__(self, session: AsyncSession) -> None: | ||
self._session = session |
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,49 +1,34 @@ | ||
from datetime import datetime, time | ||
from typing import Optional, Sequence | ||
from typing import Sequence | ||
|
||
from sqlalchemy.orm import selectinload | ||
from sqlmodel import select | ||
from sqlmodel.ext.asyncio.session import AsyncSession | ||
|
||
from ..game.model import GameScreenshot | ||
from .exception import QuizNotFoundError | ||
from .model import Quiz | ||
from .model import Quiz, QuizSubmit | ||
from .repository import QuizRepository, QuizSubmitRepository | ||
|
||
|
||
class QuizService: | ||
def __init__(self, session: AsyncSession) -> None: | ||
self._session = session | ||
def __init__(self, *, quiz_repository: QuizRepository, quiz_submit_repository: QuizSubmitRepository) -> None: | ||
self._quiz_repo = quiz_repository | ||
self._quiz_submit_repo = quiz_submit_repository | ||
|
||
async def get_today_quizes(self) -> Sequence[Quiz]: | ||
now = datetime.utcnow() | ||
today = now.date() | ||
start_datetime = datetime.combine(today, time.min) | ||
end_datetime = datetime.combine(today, time.max) | ||
start_at = datetime.combine(today, time.min) | ||
end_at = datetime.combine(today, time.max) | ||
|
||
stmts = ( | ||
select(Quiz) | ||
.where(Quiz.created_at >= start_datetime, Quiz.created_at <= end_datetime) | ||
.options(selectinload(Quiz.screenshots)) # type: ignore | ||
) | ||
rs = await self._session.exec(stmts) | ||
return rs.all() | ||
return await self._quiz_repo.get_by_created_at_interval_with_screenshots(start_at=start_at, end_at=end_at) | ||
|
||
async def submit_answer(self, *, quiz_id: int, answer: str) -> bool: | ||
""" | ||
퀴즈에 대한 정답 여부를 반환하는 함수 | ||
""" | ||
|
||
# TODO: 제출 기록을 저장해야 함 | ||
quiz = await self._get_quiz_by_id_with_game(quiz_id) | ||
"""퀴즈에 대한 정답 여부를 반환하는 함수""" | ||
quiz = await self._quiz_repo.get_with_game(id=quiz_id) | ||
|
||
if quiz is None: | ||
raise QuizNotFoundError | ||
|
||
return quiz.game.name == answer | ||
correct = quiz.game.name == answer | ||
quiz_submit = QuizSubmit(answer=answer, correct=correct, quiz_id=quiz_id) | ||
|
||
await self._quiz_submit_repo.create(model=quiz_submit) | ||
|
||
async def _get_quiz_by_id_with_game(self, id: int) -> Optional[Quiz]: | ||
stmt = ( | ||
select(Quiz).where(Quiz.id == id).options(selectinload(Quiz.screenshots).selectinload(GameScreenshot.game)) # type: ignore | ||
) | ||
rs = await self._session.exec(stmt) | ||
return rs.first() | ||
return correct |
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,36 @@ | ||
from typing import Generic, Protocol, Type, TypeVar | ||
|
||
from sqlalchemy import exc | ||
from sqlmodel import SQLModel, exists, select | ||
from sqlmodel.ext.asyncio.session import AsyncSession | ||
|
||
ModelTypeT = TypeVar("ModelTypeT", bound=SQLModel) | ||
ModelTypeS = TypeVar("ModelTypeS", bound=SQLModel) | ||
|
||
|
||
class IRepository(Protocol, Generic[ModelTypeT]): | ||
_session: AsyncSession | ||
model: Type[ModelTypeT] | ||
|
||
|
||
class CRUDMixin: | ||
async def get(self: IRepository[ModelTypeS], *, id: int) -> ModelTypeS | None: | ||
stmt = select(self.model).where(self.model.id == id) | ||
rs = await self._session.exec(stmt) | ||
return rs.first() | ||
|
||
async def exists(self: IRepository[ModelTypeS], *, id: int) -> bool: | ||
stmt = select(exists().where(self.model.id == id)) | ||
rs = await self._session.exec(stmt) | ||
return rs.one() | ||
|
||
async def create(self: IRepository[ModelTypeS], *, model: ModelTypeS) -> ModelTypeS: | ||
try: | ||
self._session.add(model) | ||
await self._session.commit() | ||
except exc.IntegrityError: | ||
await self._session.rollback() | ||
raise | ||
|
||
await self._session.refresh(model) | ||
return model |
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
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