Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 232e4fe

Browse filesBrowse files
committed
updated
1 parent d4138cf commit 232e4fe
Copy full SHA for 232e4fe

File tree

2 files changed

+25
-5
lines changed
Filter options

2 files changed

+25
-5
lines changed

‎app/routers/post.py

Copy file name to clipboardExpand all lines: app/routers/post.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
router = APIRouter()
99

1010

11-
@router.get('/')
11+
@router.get('/', response_model=schemas.ListPostResponse)
1212
def get_posts(db: Session = Depends(get_db), limit: int = 10, page: int = 1, search: str = '', user_id: str = Depends(require_user)):
1313
skip = (page - 1) * limit
1414

1515
posts = db.query(models.Post).group_by(models.Post.id).filter(
1616
models.Post.title.contains(search)).limit(limit).offset(skip).all()
17-
return {'status': 'success', 'results': len(posts), 'data': posts}
17+
return {'status': 'success', 'results': len(posts), 'posts': posts}
1818

1919

2020
@router.post('/', status_code=status.HTTP_201_CREATED, response_model=schemas.PostResponse)
@@ -28,14 +28,14 @@ def create_post(post: schemas.CreatePostSchema, db: Session = Depends(get_db), o
2828

2929

3030
@router.put('/{id}', response_model=schemas.PostResponse)
31-
def update_post(id: str, post: schemas.CreatePostSchema, db: Session = Depends(get_db), user_id: str = Depends(require_user)):
31+
def update_post(id: str, post: schemas.UpdatePostSchema, db: Session = Depends(get_db), user_id: str = Depends(require_user)):
3232
post_query = db.query(models.Post).filter(models.Post.id == id)
3333
updated_post = post_query.first()
3434

3535
if not updated_post:
3636
raise HTTPException(status_code=status.HTTP_200_OK,
3737
detail=f'No post with this id: {id} found')
38-
if updated_post.owner_id != user_id:
38+
if updated_post.user_id != uuid.UUID(user_id):
3939
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN,
4040
detail='You are not allowed to perform this action')
4141
post_query.update(post.dict(), synchronize_session=False)

‎app/schemas.py

Copy file name to clipboardExpand all lines: app/schemas.py
+21-1Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from datetime import datetime
2+
from typing import List
23
import uuid
34
from pydantic import BaseModel, EmailStr, constr
45

@@ -30,6 +31,10 @@ class UserResponse(UserBaseSchema):
3031
updated_at: datetime
3132

3233

34+
class FilteredUserResponse(UserBaseSchema):
35+
pass
36+
37+
3338
class PostBaseSchema(BaseModel):
3439
title: str
3540
content: str
@@ -44,9 +49,24 @@ class Config:
4449
class CreatePostSchema(PostBaseSchema):
4550
pass
4651

52+
class UpdatePostSchema(BaseModel):
53+
title: str | None = None
54+
content: str | None = None
55+
category: str | None = None
56+
image: str | None = None
57+
58+
class Config:
59+
orm_mode = True
60+
4761

4862
class PostResponse(PostBaseSchema):
4963
id: uuid.UUID
50-
user: UserResponse
64+
user: FilteredUserResponse
5165
created_at: datetime
5266
updated_at: datetime
67+
68+
69+
class ListPostResponse(BaseModel):
70+
status: str
71+
results: int
72+
posts: List[PostResponse]

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.