Let’s Build Backend for Posts in Our Social Network App using Spring
Tags: facebookclone, socialmediaapp, chattingapp, spring
Published: 2021-04-23 16:44:05




In this tutorial, we are going to add a Posts feature using Springboot Backend for a demo social network site we are building.

![](https://cdn-images-1.medium.com/max/5736/1*F3gW49rAYaG9NIjcVUr6mQ.png)

## 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

 1. Users can log in by email or Github.

 2. Users can add each other as friends.

 3. Users can post and see each other’s posts (only text, images and videos are not yet supported)

 4. Users can chat with their friends in whatsapp like interface.

We have used [Vue.js ](https://medium.com/javarevisited/top-5-online-courses-to-learn-vue-js-in-2021-249e66b60646)as a frontend framework, which we deployed using Netlify and [Spring Boot](https://medium.com/javarevisited/top-10-courses-to-learn-spring-boot-in-2020-best-of-lot-6ffce88a1b6e) 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**](https://medium.com/javarevisited/lets-build-a-robust-social-login-using-spring-boot-vue-js-and-firebase-36fc5bac0d0b)
[**Let’s Build Backend for Friends in Our Social Network App using Spring**](https://medium.com/javarevisited/lets-build-backend-for-friends-in-our-social-network-app-using-spring-262bdeba926a)
> # Live Demo
> # [https://simplecoding-social.netlify.app/](https://simplecoding-social.netlify.app/)

## Pre-Requisites :

 1. The basic knowledge of Java programming language.

 2. An idea of [SpringBoot MVC](https://spring.io/guides/gs/spring-boot/) file structure and working principles.

 3. Basic knowledge of creating APIs and working with JSON requests & responses.

## Goals

 1. Users should able to Post content (API to add a post).

 2. The user should be able to see his own Posts (API to see posts by the user himself).

 3. Users should be able to see all the Posts (API to get all posts).

 4. Users should be able to see the posts of specific users(e.g. friend’s post).

## Road Map

 1. Create a Model to store the records in the database in the specified format.

 2. Create a Repository to add custom User-defined methods to interact with the model and get required data.

 3. Create a Service to perform a CRUD operation on the database by using repository methods.

 4. 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 :
{% gist https://gist.github.com/tailorsmit/f8cd35a85211d067595949d0b6b85c53.js %}
Note, that we have declared 3 entities in our Post model Id, User, Content.

 1. The id represents the unique identification of the record.

 2. Content will store the string content provided by the User.

 3. Record creation date and time will be stored into ‘CreatedDate’ column of the Table.

 4. 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](https://medium.com/hackernoon/top-5-sql-and-database-courses-to-learn-online-48424533ac61).

create PostRepository.java file and paste the following code :
{% gist https://gist.github.com/tailorsmit/803fba72bfa04476bbc8a1ab35ce357b.js %}
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 :
{% gist https://gist.github.com/tailorsmit/add0be3d14dd21ab966b6601a280bcc8.js %}
Here, we have created 3 methods to save a Post, get the post of a specific user, and get all the posts.

 1. save post method will take a currently logged-in user’s information and the content of the post and save it in the [database](https://medium.com/hackernoon/top-5-sql-and-database-courses-to-learn-online-48424533ac61).

 2. get Post of a user method will take a user id of some user whose Posts the current user wants to see.

 3. 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](https://medium.com/javarevisited/10-advanced-spring-boot-courses-for-experienced-java-developers-5e57606816bd?source=collection_home---4------0-----------------------). 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 :
{% gist https://gist.github.com/tailorsmit/93f5aeb2c0e54b20d94e56da25660add.js %}
Here, we have created 4 endpoints addpost, mypost, posts, /{user_id}/posts .

 1. 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.

 2. My Post endpoint will show the post of the currently logged-in user.

 3. 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.

 4. 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.

![Add Post](https://cdn-images-1.medium.com/max/2108/1*hLE3gybpLXAFiLwLBxLW2g.png)

![My Post](https://cdn-images-1.medium.com/max/2078/1*0ouYPPtG6AKcVLiDDeDTfg.png)

![Get All Post](https://cdn-images-1.medium.com/max/2034/1*F2Qxr-cyZsDHKd3bK-7zZw.png)

![Get Post of Specific User](https://cdn-images-1.medium.com/max/2012/1*eZwTzP2y7hx5i8dUmZ-hYQ.png)

Hurray! It's working.

## Congratulations

You have learned to create a Post feature in the Backend. Stay tuned for more upcoming features.