forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposts.py
More file actions
114 lines (97 loc) · 3.61 KB
/
Copy pathposts.py
File metadata and controls
114 lines (97 loc) · 3.61 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
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
from schemas import Post, CreatePostRequest, UpdatePostRequest, Error
router = APIRouter(prefix="/posts", tags=["Posts"])
@router.get("", response_model=List[Post])
def get_posts(db: Session = Depends(get_db)):
"""모든 게시물 조회"""
posts = db.query(PostModel).all()
result = []
for post in posts:
result.append({
"id": post.id,
"username": post.username,
"content": post.content,
"createdAt": post.created_at,
"updatedAt": post.updated_at,
"likesCount": len(post.likes),
"commentsCount": len(post.comments)
})
return result
@router.post("", response_model=Post, status_code=status.HTTP_201_CREATED)
def create_post(post_request: CreatePostRequest, db: Session = Depends(get_db)):
"""새 게시물 생성"""
post_id = f"post-{uuid.uuid4()}"
new_post = PostModel(
id=post_id,
username=post_request.username,
content=post_request.content
)
db.add(new_post)
db.commit()
db.refresh(new_post)
return {
"id": new_post.id,
"username": new_post.username,
"content": new_post.content,
"createdAt": new_post.created_at,
"updatedAt": new_post.updated_at,
"likesCount": 0,
"commentsCount": 0
}
@router.get("/{postId}", response_model=Post)
def get_post_by_id(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의 게시물이 존재하지 않습니다"}
)
return {
"id": post.id,
"username": post.username,
"content": post.content,
"createdAt": post.created_at,
"updatedAt": post.updated_at,
"likesCount": len(post.likes),
"commentsCount": len(post.comments)
}
@router.patch("/{postId}", response_model=Post)
def update_post(postId: str, post_request: UpdatePostRequest, 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의 게시물이 존재하지 않습니다"}
)
post.username = post_request.username
post.content = post_request.content
db.commit()
db.refresh(post)
return {
"id": post.id,
"username": post.username,
"content": post.content,
"createdAt": post.created_at,
"updatedAt": post.updated_at,
"likesCount": len(post.likes),
"commentsCount": len(post.comments)
}
@router.delete("/{postId}", status_code=status.HTTP_204_NO_CONTENT)
def delete_post(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의 게시물이 존재하지 않습니다"}
)
db.delete(post)
db.commit()
return None