forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomments.py
More file actions
136 lines (117 loc) · 4.55 KB
/
Copy pathcomments.py
File metadata and controls
136 lines (117 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from fastapi import APIRouter, Depends, HTTPException, status
from sqlalchemy.orm import Session
from typing import List
import uuid
from database import get_db
from models import Post as PostModel, Comment as CommentModel
from schemas import Comment, CreateCommentRequest, UpdateCommentRequest, Error
router = APIRouter(prefix="/posts/{postId}/comments", tags=["Comments"])
@router.get("", response_model=List[Comment])
def get_comments(postId: str, db: Session = Depends(get_db)):
"""게시물의 댓글 조회"""
post = db.query(PostModel).filter(PostModel.id == postId).first()
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={"message": "리소스를 찾을 수 없습니다", "details": "지정된 ID의 게시물이 존재하지 않습니다"}
)
comments = db.query(CommentModel).filter(CommentModel.post_id == postId).all()
return [{
"id": comment.id,
"postId": comment.post_id,
"username": comment.username,
"content": comment.content,
"createdAt": comment.created_at,
"updatedAt": comment.updated_at
} for comment in comments]
@router.post("", response_model=Comment, status_code=status.HTTP_201_CREATED)
def create_comment(postId: str, comment_request: CreateCommentRequest, db: Session = Depends(get_db)):
"""댓글 생성"""
post = db.query(PostModel).filter(PostModel.id == postId).first()
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={"message": "리소스를 찾을 수 없습니다", "details": "지정된 ID의 게시물이 존재하지 않습니다"}
)
comment_id = f"comment-{uuid.uuid4()}"
new_comment = CommentModel(
id=comment_id,
post_id=postId,
username=comment_request.username,
content=comment_request.content
)
db.add(new_comment)
db.commit()
db.refresh(new_comment)
return {
"id": new_comment.id,
"postId": new_comment.post_id,
"username": new_comment.username,
"content": new_comment.content,
"createdAt": new_comment.created_at,
"updatedAt": new_comment.updated_at
}
@router.get("/{commentId}", response_model=Comment)
def get_comment_by_id(postId: str, commentId: str, db: Session = Depends(get_db)):
"""특정 댓글 조회"""
comment = db.query(CommentModel).filter(
CommentModel.id == commentId,
CommentModel.post_id == postId
).first()
if not comment:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={"message": "리소스를 찾을 수 없습니다", "details": "지정된 ID의 댓글이 존재하지 않습니다"}
)
return {
"id": comment.id,
"postId": comment.post_id,
"username": comment.username,
"content": comment.content,
"createdAt": comment.created_at,
"updatedAt": comment.updated_at
}
@router.patch("/{commentId}", response_model=Comment)
def update_comment(
postId: str,
commentId: str,
comment_request: UpdateCommentRequest,
db: Session = Depends(get_db)
):
"""댓글 업데이트"""
comment = db.query(CommentModel).filter(
CommentModel.id == commentId,
CommentModel.post_id == postId
).first()
if not comment:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={"message": "리소스를 찾을 수 없습니다", "details": "지정된 ID의 댓글이 존재하지 않습니다"}
)
comment.username = comment_request.username
comment.content = comment_request.content
db.commit()
db.refresh(comment)
return {
"id": comment.id,
"postId": comment.post_id,
"username": comment.username,
"content": comment.content,
"createdAt": comment.created_at,
"updatedAt": comment.updated_at
}
@router.delete("/{commentId}", status_code=status.HTTP_204_NO_CONTENT)
def delete_comment(postId: str, commentId: str, db: Session = Depends(get_db)):
"""댓글 삭제"""
comment = db.query(CommentModel).filter(
CommentModel.id == commentId,
CommentModel.post_id == postId
).first()
if not comment:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={"message": "리소스를 찾을 수 없습니다", "details": "지정된 ID의 댓글이 존재하지 않습니다"}
)
db.delete(comment)
db.commit()
return None