11 - Blog Post Blogging

In this module, we'll create the views needed to finish the API by implementing actions to use on the Blog Post model

The Blog Post Views

Now that we have all of the actions needed for the users, we still need to present, create, and manipulate the blog post model. Just like how we created the user_view file that contained all the user actions, we're going to use blog_post_views.py file to handle the blog post traffic. Then, we will add the blueprint to the app module, so we have access to those routes

Create Blog Post

Start off by adding the following code to views/blog_post_view.py . This will start off by creating the blueprint and importing all of the dependencies needed for the views

Delete Post

We don't want a random user to be able to delete someone else's post. So we'll need to add that functionality. Luckily, Flask comes with the g object we've discussed earlier. g holds everything Flask needs and anything else we need to persist, into a dictionary. Add the following function to views/blog_post_view.py

Get All

This method is really short. All it does is grab all of the posts and returns them. There isn't a lot of room for error, so we don't need to handle anything that can go wrong. Either something gets returned, or nothing does

Get one Blog Post

Instead of grabbing all of the posts, what if we just want to grab one? well, if we remember, we added a method already in the BlogPost model to do this. We just need to call, save the object, and return it

Update Blog Post

Custom Response

We've seen this before with the user object but we never implemented it, nor imported it here. Let's do that

Registering the Blue Print

Make the create_app function in src/app.py file look like the following

Final Steps

Run the API using the steps we took in module 10, and test the endpoints with postman just like we did before

Last updated

Was this helpful?