Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/v1/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from api.v1.review import review_router
from api.v1.auth import auth_router
from api.v1.user import user_router
from api.v1.user import user_router, certificate_router
from api.v1.studybook import studybook_router
from api.v1.studybook import studybook_question_router
from api.v1.test import test_router
Expand All @@ -13,4 +13,5 @@
router.include_router(user_router.router, tags=["user"], prefix="/user")
router.include_router(studybook_router.router, tags=["studybook"], prefix="/studybook")
router.include_router(studybook_question_router.router, tags=["studybook_question"], prefix="/studybook-question")
router.include_router(test_router.router, tags=["test"], prefix="/test")
router.include_router(test_router.router, tags=["test"], prefix="/test")
router.include_router(certificate_router.router, tags=["certificate"], prefix="/user")
12 changes: 1 addition & 11 deletions api/v1/test/test_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
router = APIRouter()

# 오늘의 문제
@router.post("/create-today-questions/{certificate_id}", summary="오늘의 문제 생성 또는 불러오기")
@router.post("/create-today-questions/{certificate_id}", summary="오늘의 문제 생성")
async def create_today_questions(
certificate_id: int = Path(..., description="자격증 ID"),
current_user: User = Depends(get_current_user),
Expand All @@ -81,16 +81,6 @@ async def submit_today_test(
):
return await submit_today_test_usecase(current_user, db)

# 시험 결과 제출 (시험 모드)
@router.post("/submit/{test_id}", response_model=SubmitTestResponse)
async def submit_test(
test_id: str = Path(...),
request: SubmitTestRequest = Depends(),
db: Session = Depends(get_db),
current_user: User = Depends(get_current_user)
):
return await submit_test_usecase(test_id, request, db, current_user)

# 문제 풀기 (쉬엄 모드)
@router.get("/rest-mode/{question_count}", response_model=RestModeResponse)
async def get_relax_mode_questions(
Expand Down
23 changes: 23 additions & 0 deletions api/v1/user/certificate_router.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from typing import List

from fastapi import APIRouter, Query, Depends
from sqlalchemy.ext.asyncio import AsyncSession

from app.user.dto.response.certificates_response import CertificateResponseDto
from app.user.usecase.certificate_usecase import CertificateUseCase
from database.dependency import get_db
from domain.user.repository.certificate_repository import CertificateRepository
from domain.user.service.certificate_service import CertificateService
from exception.success import ok

router = APIRouter()
@router.get("/get-certificates-list", response_model=CertificateResponseDto)
async def get_certificates_list(
offset: int = Query(0, ge=0),
limit: int = Query(20, ge=0),
db: AsyncSession = Depends(get_db)
):
usecase = CertificateUseCase()
data = await usecase.execute(db=db, offset=offset, limit=limit)

return ok(data=data.model_dump() ,message="자격증 리스트 조회 성공")
6 changes: 6 additions & 0 deletions app/test/usecase/create_today_test_usecase.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ async def create_today_questions_usecase(certificate_id: int, current_user: User

await db.commit()

result = await db.execute(
select(TodayTestQuestion)
.where(TodayTestQuestion.today_test_by_ai_id == today_test.id)
)
questions = result.scalars().all()

return ok(
data={
"question_count": len(questions),
Expand Down
6 changes: 6 additions & 0 deletions app/user/dto/response/certificate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pydantic import BaseModel


class CertificateDto(BaseModel):
certificate_id: int
name: str
9 changes: 9 additions & 0 deletions app/user/dto/response/certificates_response.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from typing import List
from pydantic import BaseModel
from app.user.dto.response.certificate import CertificateDto
from app.utils.dto.pagination import PaginationDto


class CertificateResponseDto(BaseModel):
certificates: List[CertificateDto]
pagination: PaginationDto
31 changes: 31 additions & 0 deletions app/user/usecase/certificate_usecase.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from typing import List

from sqlalchemy.ext.asyncio import AsyncSession

from app.user.dto.response.certificates_response import CertificateResponseDto
from app.utils.dto.pagination import PaginationDto
from domain.user.repository.certificate_repository import CertificateRepository
from domain.user.service.certificate_service import CertificateService
from exception.client_exception import NotFoundException, BadRequestException


class CertificateUseCase:

@staticmethod
async def execute(db: AsyncSession, offset: int, limit: int) -> CertificateResponseDto:
certificates, total = await CertificateService.fetch_certificates(db, offset, limit)

if offset >= total:
raise BadRequestException(message="더이상 불러올 내용이 없습니다.")

pagination = PaginationDto(
offset=offset,
limit=limit,
total_count=total,
next_page=(offset + limit < total)
)

return CertificateResponseDto(
certificates=certificates,
pagination=pagination
)
Empty file added app/utils/__init__.py
Empty file.
Empty file added app/utils/dto/__init__.py
Empty file.
8 changes: 8 additions & 0 deletions app/utils/dto/pagination.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from openai import BaseModel


class PaginationDto(BaseModel):
offset: int
limit: int
total_count: int
next_page: bool
15 changes: 14 additions & 1 deletion domain/user/repository/certificate_repository.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
from sqlalchemy import Sequence
from typing import List

from sqlalchemy import Sequence, select
from sqlalchemy.ext.asyncio import AsyncSession
from domain.user.entity.certificate import Certificate
from domain.user.entity.user import User
Expand Down Expand Up @@ -30,3 +32,14 @@ async def add_user_certificate(db: AsyncSession, user: User, certificates: Seque
await db.commit()
await db.refresh(user)
return user


@staticmethod
async def get_certificates_list(db: AsyncSession, offset: int, limit: int) -> (List[Certificate], int):
result = await db.execute(
select(Certificate).offset(offset).limit(limit)
)
certificates = result.scalars().all()
total_result = await db.execute(select(Certificate))
total = len(total_result.scalars().all())
return certificates, total
17 changes: 17 additions & 0 deletions domain/user/service/certificate_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from typing import List

from sqlalchemy.ext.asyncio import AsyncSession

from app.user.dto.response.certificate import CertificateDto
from domain.user.repository.certificate_repository import CertificateRepository


class CertificateService:

@staticmethod
async def fetch_certificates(db: AsyncSession, offset: int, limit: int) -> (List[CertificateDto], int):
certificates, total = await CertificateRepository.get_certificates_list(db, offset, limit)
certificates_list = [
CertificateDto(certificate_id=certificate.id, name=certificate.name) for certificate in certificates
]
return certificates_list, total
Loading