In this tutorial, we are going to add a Posts feature using Springboot Backend for a demo social network site we are building.
Introduction
We use a lot of social media platforms every day, like Facebook, Linkedin, Twitter, Whatsapp, Instagram. I always wanted to build one myself. So in this series of tutorials, we are going to build one from scratch. It has currently following features now
Users can log in by email or Github.
Users can add each other as friends.
Users can post and see each other’s posts (only text, images and videos are not yet supported)
Users can chat with their friends in whatsapp like interface.
We have used Vue.js as a frontend framework, which we deployed using Netlify and Spring Boot as backend, deployed in Heroku.
In this tutorial, we will discuss the backend of Posts.
Complete Series
Previously we have built, login system and friends features. We will extend to posts feature now.
Let’s Build a Robust Social Login Using Spring Boot, Vue.js and Firebase
Let’s Build Backend for Friends in Our Social Network App using Spring
Live Demo
https://simplecoding-social.netlify.app/
Pre-Requisites :
The basic knowledge of Java programming language.
An idea of SpringBoot MVC file structure and working principles.
Basic knowledge of creating APIs and working with JSON requests & responses.
Goals
Users should able to Post content (API to add a post).
The user should be able to see his own Posts (API to see posts by the user himself).
Users should be able to see all the Posts (API to get all posts).
Users should be able to see the posts of specific users(e.g. friend’s post).
Road Map
Create a Model to store the records in the database in the specified format.
Create a Repository to add custom User-defined methods to interact with the model and get required data.
Create a Service to perform a CRUD operation on the database by using repository methods.
Create a Controller to add API endpoints to interact with Users or Frontend Developers.
Let’s Get Started.
Model
The model defines the structure/format in which the data has to be stored in the database. It represents the java object carrying the data.
create Post.java Model class and paste the following code :
Note, that we have declared 3 entities in our Post model Id, User, Content.
The id represents the unique identification of the record.
Content will store the string content provided by the User.
Record creation date and time will be stored into ‘CreatedDate’ column of the Table.
The user_id column will store the id of the user.
Now, we have created a model for Post, let’s create a Repository for the same.
Repository
The repository contains the various methods for getting the records from the database. It is also used to create a user-defined method to get records/data from the database.
create PostRepository.java file and paste the following code :
Here, we need one custom(user-defined) method to get the user-specific posts. All other methods (e.g. findPostByID,findAllPost) will be provided by default as we extend the JpaRepository interface.
After that, we need to show the latest post at first. So, we have added custom method findAllByOrderByIdDesc . This method will send the posts into reverse order of Id.
Now, Let’s create a service for performing CRUD operations.
Service
The service is used to perform various operations(e.g. CRUD) on the database. The service file is used to write business logic in the code. Nowadays getting data is not enough we need to apply the preprocessing on data. The methods of service class will be useful to extract the data from the database holding specific conditions and rules.
create a file PostService.java and paste the following code :
Here, we have created 3 methods to save a Post, get the post of a specific user, and get all the posts.
save post method will take a currently logged-in user’s information and the content of the post and save it in the database.
get Post of a user method will take a user id of some user whose Posts the current user wants to see.
get all post method will simply provide the list of all Posts in Reversed order.
Finally, we have created services. Let’s move towards the final step.
Controller
A controller is a very important part of Spring Boot MVC. It is used to create API endpoints and interact with users via request and response. It uses the Service that we have created previously to perform the various operation to manipulate the data so that we get all data in the proper format.
paste the following code on the controller file :
Here, we have created 4 endpoints addpost, mypost, posts, /{user_id}/posts .
Add Post is the POST method that will accept the content of the user if a user is logged in and verified and store the content in the database.
My Post endpoint will show the post of the currently logged-in user.
Posts endpoints will not be requiring any authentication as all people can see the posts of other users. It will send all the Posts in response.
The last endpoint /{user_id}/posts is used to get the Posts of the specified users. It will take the user id from the request and pass it to the postService and send posts of the specified user.
Finally, we have completed the implementation of our backend. Let’s test our API endpoints.
Hurray! It's working.
Congratulations
You have learned to create a Post feature in the Backend. Stay tuned for more upcoming features.
Top comments (0)