In the previous tutorial, we have added Github OAuth with JWT based Authentication.
In this tutorial, we will extend the existing app into a social network site, like facebook, where users can add each other friend and have real time chat and video/voice call.
We will be building the backend of the friend feature, in this tutorial
We will be going to do something like this.
For this we are going to need 2 APIs :
1. To add a Friend (
2. To List Friends (
Let’s Get Started.
- Knowledge of Java, OOP & Spring Boot Framework
- Java Development Kit (JDK)
- IntelliJ IDEA Ultimate — open-source (Recommended)
- MySQL/MariaDB Database
- A good browser (Chrome — recommended)
- Create a model to store Friends.
- Create a Friends Repository to interact with the Friends model.
- Create Services to perform CRUD operations.
- Create API endpoints to interact with Frontend.
The model describes that how the SQL Table will get structured by Spring boot.
we can add the Table Name, Column Name, and Key related fields using our model class.
We will start by creating a Model class for Friends
We have added 3 fields to the model user id of 2 users who are wanted to be friends and the date of record created. The date of record created can be used later as we show the friends since x months.
Now, that we have created our model, we need to create a Repository to create and apply various methods in which we can find records by specifying particular conditions.
If you are familiar with Spring Boot, you would know that
Repository the interface contains methods to fetch data from the table.
Creating CRUD methods manually means writing a lot of boilerplate code unless you let the
JPARepository interface carry about routine.
Following is the complete code of
JPARepositorywill automatically create and implement methods for the basic CRUD operations.
- we have defined 3 methods for the repository.
- existsByFirstUserAndSecondUser:- This method gets 2 User as an input and will send the true if the matching record exists.
- findByFirstUser:- This method will take a User object and will return a List of records which consist of the specified user as the first user in Table.
- findBySecondUser:- This method will take the User object and will return a list of records which consist of the specified user as a second user in the table.
Now, our Friend Repository looks good to go.
Let’s go towards the services we will need to create for the friends' feature.
services are the essential part of Creating RESTful web services. services define the methods of operations that will be triggering on API calls.(GET ,POST)
Here’s the complete code for Service
Currently, we need to add two services to our feature.
- To add friend:-
saveFriendthe method will store the friends' user in the Friends table in the database.
- To list all friend:-
getFriendsthe method will return the list of all friends of the current Logged-in User.
That’s all for Service.
Create a file
FriendController.java and paste the following code.
GetMapping represents the Api Method GET and String passed in Constructor of GetMapping represents the API Endpoint for getting method.
- addUser:- This method will accept the user id of another user with whom the logged-in user wants to be a Friend.
- get friends:- This method will return the List of Users which are Friends with the current Logged In User.
Note, that these methods Use the services to perform various operations like adding/fetching/modifying records from/to database.
Congratulations, we have now added the Friends feature to our backend. We will build the front end in the next tutorial.