Skip to content

Commit

Permalink
add get_user_posts
Browse files Browse the repository at this point in the history
  • Loading branch information
mh-malekpour committed Jun 12, 2020
1 parent 00c51e7 commit ccea540
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
62 changes: 52 additions & 10 deletions api-documentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -898,11 +898,7 @@ response code will be **422**

method: `GET`

*input*:

Authorization Header:

- Bearer \<access token>
*input*: **None**

*output*:

Expand Down Expand Up @@ -955,10 +951,6 @@ method: `GET`

pass query parametr in URL

Authorization Header:

- Bearer \<access token>

*output*:

response code will be **200**
Expand All @@ -969,7 +961,6 @@ response code will be **200**
"author_fullname": "Mohammad Hossein Malekpour",
"category": "recepie",
"content": "who konws!",
"current_user_mail": "[email protected]",
"post_id": 2,
"slug": "avaliwern-post-dailywrdiet",
"summary": "pooof",
Expand Down Expand Up @@ -1072,3 +1063,54 @@ response code will be **403**
```

----------

### `/blog/posts/user`

method: `GET`

*input*:

Authorization Header:

- Bearer \<access token>

*output*:

response code will be **200**

```json
{
"2": {
"author_email": "[email protected]",
"author_fullname": "Mohammad Hossein Malekpour",
"category": "recepie",
"content": "who konws!",
"current_user_mail": "[email protected]",
"slug": "avaliwern-post-dailywrdiet",
"summary": "pooof",
"title": "How To Get Diet?"
},
"3": {
"author_email": "[email protected]",
"author_fullname": "Mohammad Hossein Malekpour",
"category": "recepie",
"content": "who konws!",
"current_user_mail": "[email protected]",
"slug": "dovomi-post-dailywrdiet",
"summary": "pooof",
"title": "How Toqwewe Get Diet?"
},
"4": {
"author_email": "[email protected]",
"author_fullname": "Mohammad Hossein Malekpour",
"category": "recepie",
"content": "who konws!",
"current_user_mail": "[email protected]",
"slug": "dovomi-post-daasdilywrdiet",
"summary": "pooof",
"title": "How Toqwewdasde Get Diet?"
}
}
```

----------
22 changes: 18 additions & 4 deletions blog/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


@blog.route('/', methods=['GET'])
@jwt_required
def list_posts():
posts = Post.query.order_by(Post.id.desc()).all()
result = dict()
Expand All @@ -20,18 +19,33 @@ def list_posts():
tmp['id'] = p.id
slug = p.slug.replace(' ', '-')
print(p.writer.FullName)
tmp = {'slug': slug, 'title': p.title, 'summary': p.summary, 'content': p.content, 'category': p.category, 'author_fullname': p.writer.FullName, 'author_email': p.writer.Email, 'current_user_mail': get_jwt_identity()}
tmp = {'slug': slug, 'title': p.title, 'summary': p.summary, 'content': p.content, 'category': p.category, 'author_fullname': p.writer.FullName, 'author_email': p.writer.Email}
result.update({f'{p.id}': tmp})
return jsonify(result), 200


@blog.route('/<string:slug>', methods=['GET'])
@jwt_required
def single_post(slug):
post = Post.query.filter(Post.slug == slug).first()
if not post:
return {'error': 'post not exist!'}, 404
result = {'post_id':post.id, 'slug': post.slug, 'title': post.title, 'summary': post.summary, 'content': post.content, 'category': post.category, 'author_fullname': post.writer.FullName, 'author_email': post.writer.Email, 'current_user_mail': get_jwt_identity()}
result = {'post_id':post.id, 'slug': post.slug, 'title': post.title, 'summary': post.summary, 'content': post.content, 'category': post.category, 'author_fullname': post.writer.FullName, 'author_email': post.writer.Email}
return jsonify(result), 200


@blog.route('/posts/user', methods=['GET'])
@jwt_required
def get_user_posts():
ID = User.query.filter_by(Email=get_jwt_identity()).first().id
posts = Post.query.filter(Post.authorId == ID).all()
result = dict()
for p in posts:
tmp = dict()
tmp['id'] = p.id
slug = p.slug.replace(' ', '-')
print(p.writer.FullName)
tmp = {'slug': slug, 'title': p.title, 'summary': p.summary, 'content': p.content, 'category': p.category, 'author_fullname': p.writer.FullName, 'author_email': p.writer.Email, 'current_user_mail': get_jwt_identity()}
result.update({f'{p.id}': tmp})
return jsonify(result), 200


Expand Down
Binary file modified requirements.txt
Binary file not shown.

0 comments on commit ccea540

Please sign in to comment.