-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
192 lines (170 loc) · 5.92 KB
/
api.py
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
from typing import Optional
from fastapi import APIRouter, status, Path, HTTPException, File, UploadFile, Header
from fastapi.responses import JSONResponse
from fastapi.templating import Jinja2Templates
from auth.login import get_current_user
from database import db
from feed.feed import update_post_data
templates = Jinja2Templates(directory="templates")
feed_router = APIRouter()
@feed_router.get('/feed')
async def get_feed():
'''
Get all posts
'''
posts = await db.get_all_posts()
posts = update_post_data(posts)
return JSONResponse(status_code=status.HTTP_200_OK, content={
'posts': posts
})
@feed_router.get('/feed/{n_posts}')
async def get_n_posts(n_posts: int = Path(..., gt=0,description='Number of random posts to be returned')):
'''
Get user defined number of posts
'''
posts = await db.get_random_n_posts(n_posts)
posts = update_post_data(posts)
return JSONResponse(status_code=status.HTTP_200_OK, content={
'posts': posts
})
@feed_router.get('/feed/page/{page}')
async def gen_posts_by_page(page: int = Path(..., gt=0,description='Page number')):
'''
Get posts on page
'''
posts = await db.get_all_posts_with_paging(page)
if posts:
posts = update_post_data(posts)
return JSONResponse(status_code=status.HTTP_200_OK, content={
'posts': posts
})
else:
return JSONResponse(status_code=status.HTTP_200_OK, content={
'posts': []
})
@feed_router.get('/feed/post/{feed_id}')
async def get_post(feed_id: int = Path(..., gt=0,description='Post id')):
'''
Get post by id
'''
post = await db.get_post(feed_id)
if post:
post = update_post_data([post])[0]
return JSONResponse(status_code=status.HTTP_200_OK, content={
'post': post,
})
else:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid feed id"
)
@feed_router.get('/feed/user/{user_id}')
async def get_posts_of_user(user_id: int = Path(..., gt=0,description='User id')):
'''
Get posts of user by id
'''
posts = await db.get_posts_of_user(user_id)
posts = update_post_data(posts)
return JSONResponse(status_code=status.HTTP_200_OK, content={
'posts': posts
})
@feed_router.post('/feed/like/{feed_id}')
async def like_post(feed_id: int = Path(..., gt=0,description='Post id'), access_token: Optional[str] = Header(None,description='JWT auth token')):
'''
Like post if authorized
'''
if not access_token:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="You're not logged in",
headers={"WWW-Authenticate": "Bearer"},
)
user = await get_current_user(access_token)
if not await db.like_post(user[0], feed_id):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Already liked or invalid id"
)
return JSONResponse(status_code=status.HTTP_200_OK, content={
'detail': "Executed",
})
@feed_router.post('/feed/unlike/{feed_id}')
async def unlike_post(feed_id: int = Path(..., gt=0,description='Post id'), access_token: Optional[str] = Header(None,description='JWT auth token')):
'''
Remove like if authorized
'''
if not access_token:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="You're not logged in",
headers={"WWW-Authenticate": "Bearer"},
)
user = await get_current_user(access_token)
if not await db.unlike_post(user[0], feed_id):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Already unliked or invalid id"
)
return JSONResponse(status_code=status.HTTP_200_OK, content={
'detail': "Executed",
})
@feed_router.delete('/feed/post/{feed_id}')
async def delete_post(
feed_id: int = Path(..., gt=0,description='Post id'),
access_token: Optional[str] = Header(None,description='JWT auth token')):
'''
Delete post and data, associated with it if authorized
'''
if not access_token:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="You're not logged in",
headers={"WWW-Authenticate": "Bearer"},
)
user = await get_current_user(access_token)
post = await db.get_post(feed_id)
if not post:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Invalid feed id"
)
if post[2] != user[0]:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Post doesnt belong to user"
)
if not await db.delete_post(feed_id):
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Unknown error"
)
return JSONResponse(status_code=status.HTTP_200_OK, content={
'detail': "Executed",
})
@feed_router.post('/feed')
async def add_new_post(
title: str,
description: str,
photo_file: UploadFile = File(...,description='Upload file'),
access_token: Optional[str] = Header(None,description='JWT auth token')):
'''
Add new post if authorized
'''
if not access_token:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="You're not logged in",
headers={"WWW-Authenticate": "Bearer"},
)
user = await get_current_user(access_token)
id = await db.add_new_post(title, description, photo_file, user[0])
if not id:
raise HTTPException(
status_code=status.HTTP_400_BAD_REQUEST,
detail="Post title is not unique"
)
return JSONResponse(status_code=status.HTTP_201_CREATED, content={
'id': id[0],
'title': title,
'description': description
})