Skip to content

Latest commit

 

History

11 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Product Pagination and Sorting Application

This is a Spring Boot application that provides a REST API for managing products with pagination and sorting capabilities. The application uses a PostgreSQL database, which is configured and run using Docker Compose.

Prerequisites

To run this application, ensure you have the following installed:

  • Docker: Required to run the PostgreSQL database.
    • Windows: Install Docker Desktop.
    • macOS: Install Docker Desktop.
    • Linux (e.g., Ubuntu): Install Docker and Docker Compose.
  • Java 17 or later: Required to run the Spring Boot application.
  • Maven: The project uses Maven for building and running. The Maven Wrapper (./mvnw) is included, so you don't need to install Maven separately.
  • Git (optional): To clone the repository.

Project Setup

  1. Clone the Repository:

    git clone https://github.com/stuart7j/pagination-sorting.git
    cd pagination-sorting
  2. Verify Docker Installation: Ensure Docker and Docker Compose are installed and running:

    docker --version
    docker-compose --version

    For Docker Desktop on Windows or macOS, confirm the application is running.

Running the Application

Step 1: Start the PostgreSQL Database

The application uses a PostgreSQL database, configured in the docker-compose.yaml file.

  1. Run Docker Compose: From the project root (where docker-compose.yaml is located), start the PostgreSQL container in detached mode:

    docker-compose up -d
  2. Verify the Container is Running: Check that the pg_products container is up:

    docker ps

    You should see a container named pg_products with the postgres:15 image, listening on port 5432.

  3. Database Configuration: The PostgreSQL database is configured with:

    • Database Name: productsdb
    • User: postgres
    • Password: postgres
    • Port: 5432 (mapped to localhost:5432)

    The application is preconfigured to connect to this database. No additional setup is needed unless you modify the application.properties or application.yml file.

Step 2: Build and Run the Spring Boot Application

The project includes a Maven Wrapper (./mvnw), so you don't need to install Maven.

  1. Run the Application: From the project root, execute:

    ./mvnw spring-boot:run

    This command:

    • Downloads dependencies (if not already cached).
    • Compiles the code.
    • Starts the Spring Boot application.
  2. Verify the Application is Running: Once started, the application is available at http://localhost:8059. Test the API using a tool like Postman or cURL:

    curl http://localhost:8059/api/products

    This returns a paginated list of products sorted by id in ascending order (default).

Step 3: Accessing the API

The application provides a REST API for managing products. Key endpoints include:

  • GET /api/products: Retrieves a paginated and sorted list of products.

    • Query Parameters:

      • page (default: 0): Page number.
      • size (default: 10): Page size.
      • sort (default: id,asc): Sort criteria (format: property,direction, e.g., price,desc).
      • category (optional): Filter by category.
      • minPrice (optional): Filter by minimum price.
      • maxPrice (optional): Filter by maximum price.
    • Example:

      curl "http://localhost:8059/api/products?sort=price,desc&category=electronics"

Stopping the Application

  1. Stop the Spring Boot Application: Press Ctrl+C in the terminal where ./mvnw spring-boot:run is running.

  2. Stop the PostgreSQL Container: To stop and remove the Docker container:

    docker-compose down

    To also remove the database volume (deleting all data):

    docker-compose down -v

Troubleshooting

  • PostgreSQL Container Not Starting:

    • Check if port 5432 is in use:

      netstat -an | grep 5432

      If another service is using port 5432, stop it or change the port mapping in docker-compose.yaml (e.g., 5433:5432).

    • Verify Docker is running: Ensure Docker Desktop (Windows/macOS) or the Docker daemon (Linux) is active.

  • Application Fails to Connect to Database:

    • Confirm the PostgreSQL container is running:

      docker ps
    • Verify the database settings in application.properties or application.yml:

      spring.datasource.url=jdbc:postgresql://localhost:5432/productsdb
      spring.datasource.username=postgres
      spring.datasource.password=postgres
    • Check application logs for errors:

      tail -f target/spring-boot.log
  • API Returns Errors:

    • For Invalid sort parameter errors, ensure the sort parameter is in the format property,direction (e.g., id,asc).
    • Test with Postman or cURL to verify query parameters.
  • Maven Issues:

    • If ./mvnw spring-boot:run fails, confirm Java 17+ is installed:

      java --version
    • Clear the Maven cache and retry:

      ./mvnw clean

Project Structure

  • src/main/java: Java source code, including ProductController and ProductService.
  • src/main/resources/application.properties: Configuration for the database and Spring Boot.
  • docker-compose.yaml: Defines the PostgreSQL service.
  • pom.xml: Maven configuration with dependencies.

Notes

  • PostgreSQL data is persisted in a Docker volume (pgdata). To reset the database, remove the volume with docker-compose down -v.
  • The application runs on port 8059 by default. To change this, update server.port in application.properties.

About

A Spring Boot REST API for retrieving product data. Implementation: Pagination (page, size query params). Sorting (sort query param, supporting multiple fields). Filtering (e.g., filter by category, price range). Product data stored in PostgreSQL.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages