|
1 | | -from sqlalchemy.ext.asyncio import AsyncSession |
| 1 | +from sqlalchemy import func |
2 | 2 | from sqlalchemy.future import select |
3 | 3 | from domain.test.entity.exam import Exam |
4 | 4 | from domain.test.entity.question import Question |
5 | 5 | from domain.user.entity.user import User |
| 6 | +from app.utils.dto.success import ok |
| 7 | +from sqlalchemy.ext.asyncio import AsyncSession |
| 8 | +from app.test.dto.response.exam_response_dto import ExamResponseDTO |
| 9 | +from domain.test.repository.test_repository import TestRepository |
6 | 10 | from exception.client_exception import NotFoundException |
7 | | -from app.utils.dto.success import created, ok |
8 | 11 |
|
9 | 12 | async def get_test_mode_usecase( |
10 | 13 | exam_id: int, |
@@ -44,3 +47,31 @@ async def get_test_mode_usecase( |
44 | 47 | }, |
45 | 48 | message="์ํ ์กฐํ ์ฑ๊ณต" |
46 | 49 | ) |
| 50 | + |
| 51 | +async def get_exam_list_by_certificate_id_usecase(certificate_id: int, db: AsyncSession): |
| 52 | + repo = TestRepository(db) |
| 53 | + exams = await repo.get_exams_by_certificate_id(certificate_id) |
| 54 | + |
| 55 | + if not exams: |
| 56 | + raise NotFoundException("ํด๋น ์๊ฒฉ์ฆ์ ๋ํ ์ํ์ด ์กด์ฌํ์ง ์์ต๋๋ค.") |
| 57 | + |
| 58 | + exam_list = [] |
| 59 | + |
| 60 | + for exam in exams: |
| 61 | + stmt = select(func.count()).where(Question.exam_id == exam.id) |
| 62 | + result = await db.execute(stmt) |
| 63 | + question_count = result.scalar_one() |
| 64 | + |
| 65 | + dto = ExamResponseDTO( |
| 66 | + id=exam.id, |
| 67 | + name=exam.name, |
| 68 | + year=exam.year, |
| 69 | + month=exam.month, |
| 70 | + trial=exam.trial, |
| 71 | + time=exam.time, |
| 72 | + pass_rate=exam.pass_rate, |
| 73 | + question_count=question_count |
| 74 | + ) |
| 75 | + exam_list.append(dto.model_dump()) |
| 76 | + |
| 77 | + return ok(data={"exams": exam_list}, message="์ํ ๋ฆฌ์คํธ ์กฐํ ์ฑ๊ณต") |
0 commit comments