|
3 | 3 |
|
4 | 4 | from fastapi import APIRouter, Depends, HTTPException, status |
5 | 5 |
|
6 | | -from app.api.deps import SessionDep, CurrentClient |
| 6 | +from app.api.deps import CurrentClient, SessionDep, get_content_service |
7 | 7 | from app.schemas.topic import TopicCreate, TopicDetail, TopicResponse, TopicUpdate |
8 | | -from app.services.content import content_service |
| 8 | +from app.services.content import ContentService |
9 | 9 |
|
10 | 10 | router = APIRouter() |
11 | 11 |
|
12 | 12 |
|
13 | 13 | @router.get("/", response_model=list[TopicResponse]) |
14 | 14 | async def read_topics( |
15 | | - session: SessionDep, |
| 15 | + session: SessionDep, |
16 | 16 | current_client: CurrentClient, |
17 | | - skip: int = 0, |
18 | | - limit: int = 100 |
| 17 | + skip: int = 0, |
| 18 | + limit: int = 100, |
| 19 | + service: ContentService = Depends(get_content_service), |
19 | 20 | ) -> list[TopicResponse]: |
20 | | - """Retrieve topics (Global + Private).""" |
21 | | - return await content_service.get_topics(session, current_client=current_client, skip=skip, limit=limit) |
| 21 | + """ |
| 22 | + Retrieve topics. |
| 23 | + Clients only see Global topics or their own Private topics. |
| 24 | + """ |
| 25 | + return await service.get_topics( |
| 26 | + session, current_client=current_client, skip=skip, limit=limit |
| 27 | + ) |
22 | 28 |
|
23 | 29 |
|
24 | | -@router.get("/{topic_id}", response_model=TopicDetail) |
25 | | -async def read_topic( |
26 | | - session: SessionDep, |
| 30 | +@router.post("/", response_model=TopicResponse) |
| 31 | +async def create_topic( |
| 32 | + *, |
| 33 | + session: SessionDep, |
27 | 34 | current_client: CurrentClient, |
28 | | - topic_id: UUID |
29 | | -) -> TopicDetail: |
30 | | - """Get a specific topic by ID.""" |
31 | | - return await content_service.get_topic(session, current_client=current_client, topic_id=topic_id) |
| 35 | + topic_in: TopicCreate, |
| 36 | + service: ContentService = Depends(get_content_service), |
| 37 | +) -> TopicResponse: |
| 38 | + """ |
| 39 | + Create a new topic. |
| 40 | + """ |
| 41 | + return await service.create_topic( |
| 42 | + session, current_client=current_client, topic_in=topic_in |
| 43 | + ) |
32 | 44 |
|
33 | 45 |
|
34 | | -# Admin routes (TODO: Add Auth dependency later for protection) |
35 | | -@router.post("/", response_model=TopicResponse, status_code=status.HTTP_201_CREATED) |
36 | | -async def create_topic(session: SessionDep, topic_in: TopicCreate) -> TopicResponse: |
37 | | - """Create a new topic.""" |
38 | | - return await content_service.create_topic(session, topic_in) |
| 46 | +@router.put("/{topic_id}", response_model=TopicResponse) |
| 47 | +async def update_topic( |
| 48 | + *, |
| 49 | + session: SessionDep, |
| 50 | + current_client: CurrentClient, |
| 51 | + topic_id: UUID, |
| 52 | + topic_in: TopicUpdate, |
| 53 | + service: ContentService = Depends(get_content_service), |
| 54 | +) -> TopicResponse: |
| 55 | + """ |
| 56 | + Update a topic. |
| 57 | + """ |
| 58 | + topic = await service.update_topic( |
| 59 | + session, current_client=current_client, topic_id=topic_id, topic_in=topic_in |
| 60 | + ) |
| 61 | + if not topic: |
| 62 | + raise HTTPException( |
| 63 | + status_code=status.HTTP_404_NOT_FOUND, |
| 64 | + detail="Topic not found or access denied", |
| 65 | + ) |
| 66 | + return topic |
39 | 67 |
|
40 | 68 |
|
41 | | -@router.put("/{topic_id}", response_model=TopicResponse) |
42 | | -async def update_topic(session: SessionDep, topic_id: UUID, topic_in: TopicUpdate) -> TopicResponse: |
43 | | - """Update a topic.""" |
44 | | - return await content_service.update_topic(session, topic_id, topic_in) |
| 69 | +@router.get("/{topic_id}", response_model=TopicDetail) |
| 70 | +async def read_topic( |
| 71 | + *, |
| 72 | + session: SessionDep, |
| 73 | + current_client: CurrentClient, |
| 74 | + topic_id: UUID, |
| 75 | + service: ContentService = Depends(get_content_service), |
| 76 | +) -> TopicDetail: |
| 77 | + """ |
| 78 | + Get topic by ID. |
| 79 | + """ |
| 80 | + topic = await service.get_topic( |
| 81 | + session, current_client=current_client, topic_id=topic_id |
| 82 | + ) |
| 83 | + if not topic: |
| 84 | + raise HTTPException( |
| 85 | + status_code=status.HTTP_404_NOT_FOUND, |
| 86 | + detail="Topic not found or access denied", |
| 87 | + ) |
| 88 | + return topic |
0 commit comments