fenya123 / forum123

Flask application written for educational purposes.
http://193.124.115.171/
1 stars 1 forks source link

112: Add REST endpoint to get lst of posts #113

Closed EugeneTurkin closed 1 year ago

EugeneTurkin commented 1 year ago

We are going to add REST API interface to our forum, and we decided to begin with creating endpoints without authentication. It won't be secure but it will make development process easier. We will add authentication to these endpoints in the future.

We need an endpoint which will return the list of posts from particular topic with abitily to sort results. Sorting will be specified via order_by query parameter. order_by should accept a string with two words separated by comma. First word is the name of the sorting field (only created_at is acceptable for now). Second word is the sorting direction (asc or desc). If there were no order_by provided, then return posts ordered by created_at ascending.

Request example:

GET /api/topics/42/posts?order_by=created_at,desc

Response example:

[
    {
        "id": 12,
        "author_id": 21,
        "created_at": "2023-04-08 19:51:43.986889".
        "body": "Some post body",
        "topic_id": 42
    },
    {
        "id": 10,
        "author_id": 1,
        "created_at": "2023-04-08 19:51:37.503064",
        "body": "Another post body"
        "topic_id": 42
    }
]

So in the scope of this task we need to create an endpoint GET /api/topics/42/posts which will return list of posts info in json format, or 400 if request is not valid, or 404 if topic doesn't exist.