|
1 | 1 | from typing import Annotated |
| 2 | + |
2 | 3 | from fastapi import APIRouter, Depends |
3 | 4 | from loguru import logger |
4 | 5 |
|
5 | | -from src.app.usecases.analyze_issue import AnalyzeIssue |
6 | 6 | from config import Settings |
| 7 | +from src.app.ports.repositories.issues import IssueRepository |
| 8 | +from src.app.repository import UnitOfWork as BaseUnitOfWork |
| 9 | +from src.app.usecases.analyze_issue import AnalyzeIssue |
7 | 10 | from src.domain.issue import Issue |
8 | 11 | from src.interface_adapters.exceptions import UnsupportedOperationException |
9 | 12 | from src.resource_adapters.persistence.in_memory.issues import InMemoryIssueRepository |
10 | | -from src.resource_adapters.persistence.in_memory.unit_of_work import UnitOfWork |
| 13 | +from src.resource_adapters.persistence.in_memory.unit_of_work import ( |
| 14 | + UnitOfWork as InMemoryUnitOfWork, |
| 15 | +) |
| 16 | +from src.resource_adapters.persistence.sqlmodel.issues import SQLModelIssueRepository |
| 17 | +from src.resource_adapters.persistence.sqlmodel.unit_of_work import SQLModelUnitOfWork |
11 | 18 |
|
12 | 19 | router = APIRouter() |
13 | 20 |
|
14 | 21 |
|
15 | 22 | # https://fastapi.tiangolo.com/tutorial/dependencies/ |
16 | | -async def configure_unit_of_work() -> UnitOfWork: |
17 | | - if Settings.get_settings().execution_mode == "in-memory": |
18 | | - return UnitOfWork() |
| 23 | +async def configure_unit_of_work() -> BaseUnitOfWork: |
| 24 | + execution_mode = Settings.get_settings().execution_mode |
| 25 | + if execution_mode == "in-memory": |
| 26 | + return InMemoryUnitOfWork() |
| 27 | + elif execution_mode == "sqlmodel": |
| 28 | + return SQLModelUnitOfWork(database_url=Settings.get_settings().database_url) |
19 | 29 | else: |
20 | 30 | raise UnsupportedOperationException( |
21 | 31 | message="Unsupported unit of work configuration", |
22 | | - detail="Only in-memory unit of work is currently supported" |
| 32 | + detail=f"Execution mode '{execution_mode}' is not supported", |
23 | 33 | ) |
24 | 34 |
|
25 | 35 |
|
26 | | -async def configure_repository() -> InMemoryIssueRepository: |
27 | | - if Settings.get_settings().execution_mode == "in-memory": |
| 36 | +async def configure_repository() -> IssueRepository: |
| 37 | + execution_mode = Settings.get_settings().execution_mode |
| 38 | + if execution_mode == "in-memory": |
28 | 39 | return InMemoryIssueRepository() |
| 40 | + elif execution_mode == "sqlmodel": |
| 41 | + # For SQLModel, the repository is managed by the UnitOfWork |
| 42 | + # This is just a placeholder that will be replaced |
| 43 | + return SQLModelIssueRepository(None) # type: ignore |
29 | 44 | else: |
30 | 45 | raise UnsupportedOperationException( |
31 | 46 | message="Unsupported repository configuration", |
32 | | - detail="Only in-memory repository is currently supported" |
| 47 | + detail=f"Execution mode '{execution_mode}' is not supported", |
33 | 48 | ) |
34 | 49 |
|
35 | 50 |
|
36 | 51 | @router.post("/issues/{issue_number}/analyze", response_model=Issue) |
37 | 52 | async def analyze_issue( |
38 | 53 | issue_number: int, |
39 | | - unit_of_work: Annotated[UnitOfWork, Depends(configure_unit_of_work)], |
40 | | - repo: Annotated[InMemoryIssueRepository, Depends(configure_repository)], |
| 54 | + unit_of_work: Annotated[BaseUnitOfWork, Depends(configure_unit_of_work)], |
| 55 | + repo: Annotated[IssueRepository, Depends(configure_repository)], |
41 | 56 | ) -> Issue: |
42 | 57 | logger.info(f"analyzing issue: {issue_number}") |
43 | | - use_case = AnalyzeIssue(issue_number=issue_number, repo=repo, unit_of_work=unit_of_work) |
| 58 | + |
| 59 | + # For SQLModel, we need to use the repository from the unit of work |
| 60 | + if isinstance(unit_of_work, SQLModelUnitOfWork): |
| 61 | + repo = unit_of_work.issues |
| 62 | + |
| 63 | + use_case = AnalyzeIssue( |
| 64 | + issue_number=issue_number, repo=repo, unit_of_work=unit_of_work |
| 65 | + ) |
44 | 66 | return await use_case.analyze() |
0 commit comments