forked from microsoft/github-copilot-vibe-coding-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.py
More file actions
285 lines (235 loc) · 8.29 KB
/
Copy pathapi.py
File metadata and controls
285 lines (235 loc) · 8.29 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
"""
API endpoints for the Simple Social Media API.
"""
from datetime import datetime
from typing import List
from fastapi import APIRouter, HTTPException, status
from sqlmodel import select
from database import SessionDep
from models import (
Comment,
Like,
LikeRequest,
LikeResponse,
NewCommentRequest,
NewPostRequest,
Post,
UpdateCommentRequest,
UpdatePostRequest,
)
# Create API router
router = APIRouter(prefix="/api")
# Posts endpoints
@router.get("/posts", response_model=List[Post])
def get_posts(session: SessionDep):
"""List all posts."""
posts = session.exec(select(Post)).all()
return posts
@router.post("/posts", response_model=Post, status_code=status.HTTP_201_CREATED)
def create_post(post_data: NewPostRequest, session: SessionDep):
"""Create a new post."""
post = Post(
username=post_data.username,
content=post_data.content
)
session.add(post)
session.commit()
session.refresh(post)
return post
@router.get("/posts/{post_id}", response_model=Post)
def get_post_by_id(post_id: str, session: SessionDep):
"""Get a specific post by ID."""
post = session.get(Post, post_id)
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
return post
@router.patch("/posts/{post_id}", response_model=Post)
def update_post(post_id: str, post_data: UpdatePostRequest, session: SessionDep):
"""Update a post."""
post = session.get(Post, post_id)
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
# Update post fields
post.username = post_data.username
post.content = post_data.content
post.updated_at = datetime.utcnow()
session.add(post)
session.commit()
session.refresh(post)
return post
@router.delete("/posts/{post_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_post(post_id: str, session: SessionDep):
"""Delete a post."""
post = session.get(Post, post_id)
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
# Delete associated comments and likes
comments = session.exec(select(Comment).where(Comment.post_id == post_id)).all()
for comment in comments:
session.delete(comment)
likes = session.exec(select(Like).where(Like.post_id == post_id)).all()
for like in likes:
session.delete(like)
# Delete the post
session.delete(post)
session.commit()
# Comments endpoints
@router.get("/posts/{post_id}/comments", response_model=List[Comment])
def get_comments_by_post_id(post_id: str, session: SessionDep):
"""List comments for a post."""
# Check if post exists
post = session.get(Post, post_id)
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
comments = session.exec(select(Comment).where(Comment.post_id == post_id)).all()
return comments
@router.post("/posts/{post_id}/comments", response_model=Comment, status_code=status.HTTP_201_CREATED)
def create_comment(post_id: str, comment_data: NewCommentRequest, session: SessionDep):
"""Create a comment."""
# Check if post exists
post = session.get(Post, post_id)
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
comment = Comment(
post_id=post_id,
username=comment_data.username,
content=comment_data.content
)
session.add(comment)
# Update comments count
post.comments_count += 1
session.add(post)
session.commit()
session.refresh(comment)
return comment
@router.get("/posts/{post_id}/comments/{comment_id}", response_model=Comment)
def get_comment_by_id(post_id: str, comment_id: str, session: SessionDep):
"""Get a specific comment."""
# Check if post exists
post = session.get(Post, post_id)
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
comment = session.get(Comment, comment_id)
if not comment or comment.post_id != post_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
return comment
@router.patch("/posts/{post_id}/comments/{comment_id}", response_model=Comment)
def update_comment(post_id: str, comment_id: str, comment_data: UpdateCommentRequest, session: SessionDep):
"""Update a comment."""
# Check if post exists
post = session.get(Post, post_id)
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
comment = session.get(Comment, comment_id)
if not comment or comment.post_id != post_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
# Update comment fields
comment.username = comment_data.username
comment.content = comment_data.content
comment.updated_at = datetime.utcnow()
session.add(comment)
session.commit()
session.refresh(comment)
return comment
@router.delete("/posts/{post_id}/comments/{comment_id}", status_code=status.HTTP_204_NO_CONTENT)
def delete_comment(post_id: str, comment_id: str, session: SessionDep):
"""Delete a comment."""
# Check if post exists
post = session.get(Post, post_id)
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
comment = session.get(Comment, comment_id)
if not comment or comment.post_id != post_id:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
# Update comments count
post.comments_count -= 1
session.add(post)
# Delete the comment
session.delete(comment)
session.commit()
# Likes endpoints
@router.post("/posts/{post_id}/likes", response_model=LikeResponse, status_code=status.HTTP_201_CREATED)
def like_post(post_id: str, like_data: LikeRequest, session: SessionDep):
"""Like a post."""
# Check if post exists
post = session.get(Post, post_id)
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
# Check if user already liked this post
existing_like = session.exec(
select(Like).where(Like.post_id == post_id, Like.username == like_data.username)
).first()
if existing_like:
# User already liked this post, return existing like
return LikeResponse(
post_id=existing_like.post_id,
username=existing_like.username,
liked_at=existing_like.liked_at
)
# Create new like
like = Like(
post_id=post_id,
username=like_data.username
)
session.add(like)
# Update likes count
post.likes_count += 1
session.add(post)
session.commit()
session.refresh(like)
return LikeResponse(
post_id=like.post_id,
username=like.username,
liked_at=like.liked_at
)
@router.delete("/posts/{post_id}/likes", status_code=status.HTTP_204_NO_CONTENT)
def unlike_post(post_id: str, session: SessionDep):
"""Unlike a post."""
# Check if post exists
post = session.get(Post, post_id)
if not post:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="The requested resource was not found"
)
# Since there's no way to identify the user in the DELETE request
# and the OpenAPI spec doesn't specify parameters or request body,
# we just return success. In a real implementation, this would
# require authentication to identify the user.
pass