Skip to content

bhagatpratham/likes_microservice

Repository files navigation

Likes System Microservice

Tech Stack Used

  • Node.js
  • PostgreSQL

Prerequisites

  • Docker should be installed and running on your machine.
  • Node.js and npm should be installed on your machine.
  • PostgreSQL 15.3

Running the Application

Steps

  1. Clone the project repository to your local machine.

  2. Open a terminal and navigate to the project's root directory.

  3. Install all the dependencies by running the following command npm install

  4. Update the config.json file

    • Open the project folder in your preferred text editor and navigate to the likes-service/src directory.

    • Locate the config.json file and open it

      Update the following fields in the config.json file with your own PostgreSQL information:

       {
         "database": {
           "host": "db",
           "port": 5432,
           "database": "likes",
           "user": "postgres",
           "password": "pra123156"
         }
       }
      

    Your updated config.json file should reflect your correct PostgreSQL connection details.

  5. Running the Containers and Database

    • Make sure you are in the project's root directory.

    • Run the following command to start the containers: docker-compose up -d --build

      • This command will build and start the containers defined in the docker-compose.yml file.

The containers should start successfully. You can check them in Docker Desktop App.

Testing the Routes:

Open Postman to test the API routes.

{
    "user_id": "123",
    "content_id": "47"
}

like_route

This route will store a "like" event for the specified user and content.

hasLiked_route

This route will check if the specified user has liked the specified content.

totalLikes

This route will fetch the total number of likes for the specified content.

Functional Tests

  • Make sure you have set up the application by following the instructions mentioned above.

Running the Functional Tests

To run the functional tests, perform the following steps:

  • Open a terminal and navigate to the project's root directory. Run the following command node functional-tests/test.js

It will send HTTP requests to the likes microservice and validate the responses. The results of each test case will be displayed in the terminal.

Test Cases

The test.js file contains the following test cases:

  • Testing the "like" route: This test case sends a POST request to the /like route with the user_id and content_id parameters. It verifies the response status and logs the response data.

  • Testing the "hasLiked" route: This test case sends a GET request to the /hasLiked route with the user_id and content_id query parameters. It checks if the user has liked the post and logs the response status and data.

  • Testing the "totalLikes" route: This test case sends a GET request to the /totalLikes route with the content_id query parameter. It retrieves the total number of likes for the post and logs the response status and data.