forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlikes.py
More file actions
64 lines (52 loc) · 2.08 KB
/
Copy pathlikes.py
File metadata and controls
64 lines (52 loc) · 2.08 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
from fastapi import APIRouter, Depends, HTTPException, Query, status
from sqlalchemy.orm import Session
from database import get_db
from models import Post as PostModel, Like as LikeModel
from schemas import Like, LikeRequest, Error
router = APIRouter(prefix="/posts/{postId}/likes", tags=["Likes"])
@router.post("", response_model=Like, status_code=status.HTTP_201_CREATED)
def like_post(postId: str, like_request: LikeRequest, 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의 게시물이 존재하지 않습니다"}
)
# Check if already liked
existing_like = db.query(LikeModel).filter(
LikeModel.post_id == postId,
LikeModel.username == like_request.username
).first()
if existing_like:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail={"message": "잘못된 요청", "details": "이미 좋아요를 눌렀습니다"}
)
new_like = LikeModel(
post_id=postId,
username=like_request.username
)
db.add(new_like)
db.commit()
db.refresh(new_like)
return {
"postId": new_like.post_id,
"username": new_like.username,
"createdAt": new_like.created_at
}
@router.delete("", status_code=status.HTTP_204_NO_CONTENT)
def unlike_post(postId: str, username: str = Query(...), db: Session = Depends(get_db)):
"""게시물 좋아요 취소"""
like = db.query(LikeModel).filter(
LikeModel.post_id == postId,
LikeModel.username == username
).first()
if not like:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail={"message": "리소스를 찾을 수 없습니다", "details": "좋아요를 찾을 수 없습니다"}
)
db.delete(like)
db.commit()
return None