Repository navigation
Expand file tree
/
Copy pathMain.py
More file actions
311 lines (251 loc) · 7.17 KB
/
Copy pathMain.py
File metadata and controls
311 lines (251 loc) · 7.17 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from typing import Optional
from urllib import response
from fastapi import FastAPI,Response,status,HTTPException
from fastapi.params import Body
from schemas.post import Post
from random import randrange
from routers.posts import router as post_router
from database.memory import my_posts,find_post,my_posts
app=FastAPI()
app.include_router(post_router)
class Post(BaseModel):
title:str
content:str
published:bool=True
rating: Optional[int]=None
def find_index_post(id):
for i,p in enumerate(my_posts):
if p["id"]==id:
return i
@app.get("/")
def root():
return {"message":"Hello World"}
@app.get("/posts")
def get_posts():
return {"message":my_posts}
@app.post("/posts",status_code=status.HTTP_201_CREATED)
def create_posts(post:Post):
post_dict=post.dict()
post_dict["id"]=randrange(0,100000000)
my_posts.append(post_dict)
return{"data":post_dict}
@app.get("/posts/{id}")
def get_post(id:int):
post=find_post(int(id))
if not post:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"post with id: {id} was not found")
return{"post_detail":f"post with id: {id}"}
@app.delete("/posts/{id}",status_code=status.HTTP_204_NO_CONTENT)
def delete_post(id:int):
index=find_index_post(id)
if index==None:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=f"post with id: {id} was not found")
my_posts.pop(index)
return Response(status_code=status.HTTP_204_NO_CONTENT)
@app.get("/posts/count")
def get_post_count():
return {"count": len(my_posts)}
@app.get("/posts/latest")
def get_latest_post():
if not my_posts:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="No posts available"
)
return {"data": my_posts[-1]}
@app.get("/posts/first")
def get_first_post():
if not my_posts:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="No posts available"
)
return {"data": my_posts[0]}
@app.get("/posts/published")
def get_published_posts():
published_posts = []
for post in my_posts:
if post.get("published", True):
published_posts.append(post)
return {"data": published_posts}
@app.get("/posts/unpublished")
def get_unpublished_posts():
unpublished_posts = []
for post in my_posts:
if not post.get("published", True):
unpublished_posts.append(post)
return {"data": unpublished_posts}
@app.get("/posts/rating/{rating}")
def get_posts_by_rating(rating: int):
posts = []
for post in my_posts:
if post.get("rating") == rating:
posts.append(post)
return {"data": posts}
@app.get("/posts/top")
def get_top_post():
if not my_posts:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="No posts available"
)
rated_posts = [
post for post in my_posts
if post.get("rating") is not None
]
if not rated_posts:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="No rated posts available"
)
top_post = max(rated_posts, key=lambda post: post["rating"])
return {"data": top_post}
@app.get("/posts/title/{title}")
def search_by_title(title: str):
results = []
for post in my_posts:
if title.lower() in post["title"].lower():
results.append(post)
return {"data": results}
@app.get("/posts/content/{keyword}")
def search_by_content(keyword: str):
results = []
for post in my_posts:
if keyword.lower() in post["content"].lower():
results.append(post)
return {"data": results}
@app.get("/posts/{id}/exists")
def post_exists(id: int):
post = find_post(id)
return {
"id": id,
"exists": post is not None
}
@app.get("/posts/limit")
def get_posts_limit(limit: int = 10):
return {"data": my_posts[:limit]}
@app.get("/posts/skip")
def get_posts_skip(skip: int = 0):
return {"data": my_posts[skip:]}
@app.get("/posts/page")
def get_posts_page(skip: int = 0, limit: int = 10):
return {
"data": my_posts[skip:skip + limit],
"skip": skip,
"limit": limit
}
@app.get("/posts/filter/rating")
def filter_by_rating(min_rating: int):
results = []
for post in my_posts:
rating = post.get("rating")
if rating is not None and rating >= min_rating:
results.append(post)
return {"data": results}
@app.get("/posts/filter/rating")
def filter_by_rating(min_rating: int):
results = []
for post in my_posts:
rating = post.get("rating")
if rating is not None and rating >= min_rating:
results.append(post)
return {"data": results}
@app.get("/posts/sort/rating")
def sort_posts_by_rating():
results = sorted(
my_posts,
key=lambda post: post.get("rating") or 0,
reverse=True
)
return {"data": results}
def post_id_exists(id):
for post in my_posts:
if post["id"] == id:
return True
return False
def generate_post_id():
new_id = randrange(0, 100000000)
while post_id_exists(new_id):
new_id = randrange(0, 100000000)
return new_id
def title_exists(title):
for post in my_posts:
if post["title"].lower() == title.lower():
return True
return False
@app.get("/posts/title-available")
def check_title(title: str):
return {
"title": title,
"available": not title_exists(title)
}
@app.get("/posts/stats/published")
def published_count():
count = 0
for post in my_posts:
if post.get("published", True):
count += 1
return {
"published_posts": count
}
@app.get("/posts/stats/unpublished")
def unpublished_count():
count = 0
for post in my_posts:
if not post.get("published", True):
count += 1
return {
"unpublished_posts": count
}
@app.get("/posts/stats/rating")
def rating_statistics():
ratings = []
for post in my_posts:
if post.get("rating") is not None:
ratings.append(post["rating"])
if not ratings:
return {
"count": 0,
"average": 0
}
return {
"count": len(ratings),
"average": sum(ratings) / len(ratings)
}
@app.get("/posts/stats")
def post_statistics():
published = 0
unpublished = 0
rated = 0
for post in my_posts:
if post.get("published", True):
published += 1
else:
unpublished += 1
if post.get("rating") is not None:
rated += 1
return {
"total_posts": len(my_posts),
"published_posts": published,
"unpublished_posts": unpublished,
"rated_posts": rated
}
@app.get("/posts/sort/title")
def sort_posts_by_title():
results = sorted(
my_posts,
key=lambda post: post["title"].lower()
)
return {
"data": results
}
@app.get("/posts/sort/title/reverse")
def sort_posts_by_title_reverse():
results = sorted(
my_posts,
key=lambda post: post["title"].lower(),
reverse=True
)
return {
"data": results
}