graduapp.com

Enhancing Our Social Network App: Building the Friends Feature

Written on

Chapter 1: Introduction to the Friends Feature

In this tutorial, we will be integrating a Friends feature into our social network application using a Spring Boot backend. This enhancement will allow users to connect with each other, similar to how platforms like Facebook operate.

This video tutorial, titled "Let's Build Backend for Posts in Our Social Network App using Spring," provides a great foundation for understanding backend development in this context.

In our last session, we successfully implemented GitHub OAuth with JWT-based authentication. Now, we are set to expand our application to include functionalities such as adding friends and facilitating real-time chat, along with video and voice calling capabilities.

Final Outcome

You can test the application [here](#). The Friends feature will allow users to connect and interact within the app.

API Overview

To implement this feature, we will create two main APIs:

  1. addFriend: To add a new friend.
  2. listFriends: To retrieve the list of friends.

Getting Started

#### Prerequisites

  • Familiarity with Java, OOP, and the Spring Boot Framework.
  • Java Development Kit (JDK).
  • IntelliJ IDEA Ultimate (recommended) or any open-source IDE.
  • MySQL or MariaDB database.
  • A reliable web browser (Chrome is recommended).

Development Flow

  1. Create a model to represent friends.
  2. Develop a Friends Repository for database interactions.
  3. Implement services to handle CRUD operations.
  4. Set up API endpoints to connect with the frontend.

Model Definition

The model will dictate how the SQL table is structured within Spring Boot. We will define the table name, column names, and key fields in our model class.

Let’s start by creating a model class named Friend.java. This class will include three fields: the user IDs of the two friends and the date when the friendship was established. The creation date can later be utilized to display how long users have been friends.

Repository Setup

Next, we will create a Repository to manage friend records. For those familiar with Spring Boot, the Repository interface provides methods for data retrieval.

Instead of writing extensive boilerplate code for CRUD methods, we can utilize the JPARepository interface, which automates these operations. Below is the complete code for FriendRepository.java. By extending JPARepository, we can easily implement basic CRUD functionalities.

We will define three methods in our repository:

  • existsByFirstUserAndSecondUser: Checks if a friendship exists between two users.
  • findByFirstUser: Returns a list of records where the specified user is the first user.
  • findBySecondUser: Returns records where the specified user is the second user.

Our Friend Repository is now ready to be utilized.

Service Layer Creation

Services play a crucial role in developing RESTful web services. They define the operations that will be executed in response to API calls (GET, POST).

Here’s the complete code for FriendService.java. For this feature, we need to create two services:

  • saveFriend: This method stores a friendship record in the database.
  • getFriends: This method retrieves the list of friends for the currently logged-in user.

Controller Implementation

Now, we will write the code for the controller. If you are acquainted with Spring Boot or similar MVC frameworks, you’ll recognize that the controller defines the API endpoints.

Create a file named FriendController.java and insert the following code. The @GetMapping annotation denotes the GET method, and the string provided in the constructor indicates the API endpoint for this method.

  • addUser: Accepts the user ID of another user that the logged-in user wishes to befriend.
  • getFriends: Returns a list of users who are friends with the currently logged-in user.

These methods will utilize the services to manage operations related to adding, fetching, and modifying records in the database.

Congratulations!

Congratulations! You have successfully integrated the Friends feature into our backend. In the upcoming tutorial, we will focus on developing the frontend to complement these backend functionalities.

In this video titled "Spring Boot & React: Build a Social Networking App from Scratch," you will find valuable insights on creating the frontend for our application.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

# Mastodon: A New Era for Social Media?

Exploring the rise of Mastodon amidst social media's challenges, focusing on decentralized platforms and community dynamics.

Ancient Solar Shrine Discovered: A Glimpse into Prehistoric Worship

Archaeologists uncover a 4,000-year-old solar shrine in the Netherlands, revealing ancient rituals and cultural significance.

Unlocking Your Potential: A Guide to Achieving Your Goals

Discover how to define and pursue your goals effectively while enjoying the journey towards personal excellence.