Skip to content

Repository files navigation

Product Catalog API

Overview

This is a RESTful API for managing a product catalog, designed to support an e-commerce platform. The API allows users to perform CRUD operations on products, organize them into categories, search for products, track inventory, and apply pricing and discounts.

Features

  • User authentication and authorization (JWT-based)
  • CRUD operations for products and categories
  • Role-based access control (Admin/Customer)
  • Search and filtering functionality
  • Product variants (e.g., size, color)
  • Inventory tracking
  • Error handling and validation

Technologies Used

  • Node.js
  • Express.js
  • MongoDB (with Mongoose ORM)
  • JSON for data exchange
  • Postman for API testing
  • JWT for authentication

Installation

  1. Clone this repository:

    git clone https://github.com/ayadeleke/ProductCatalogAPI.git
    cd product-catalog-api
  2. Install dependencies: You can check package.json to see the list of dependencies to be installed.

    npm install
  3. Set up a .env file for environment variables:

    MONGO_URI=mongodb_connection_string
    PORT=3000
    JWT_SECRET=jwt_secret_key
  4. Start the server:

    npx nodemon server.js // This will make the server.js to auto restart

    The API will run on http://127.0.0.1:3000.

API Endpoints

1. Authentication & User Management

  • Register a new user:

    POST http://localhost:3000/auth/register

    Request Body:

    {
      "name": "John Doe",
      "email": "john@example.com",
      "password": "securepassword",
      "role": "admin"
    }
  • Login user:

    POST http://localhost:3000/auth/login

    Request Body:

    {
      "email": "john@example.com",
      "password": "securepassword"
    }
  • Get all users (Admin Only):

    GET http://localhost:3000/users
    Authorization: Bearer <jwt-token>

2. Product Management

  • Create a product (Admin Only):

    POST http://localhost:3000/products
    Content-Type: application/json
    Authorization: Bearer <jwt-token>

    Request Body:

    {
      "name": "Nike Shoes",
      "description": "Some Nike shoes Descriptions",
      "category": "Shoes",
      "price": 99.99,
      "stock": 50,
      "image": "null",
      "variants": [
          { "size": "M", "color": "Red", "quantity": 5 },
          { "size": "L", "color": "Blue", "quantity": 10 }
      ]
    }
  • Get all products:

    GET http://localhost:3000/products

Response Body:

{
  "name": "Nike Shoes",
  "description": "Some Nike shoes Descriptions",
  "category": "Shoes",
  "price": 99.99,
  "stock": 50,
  "image": "null"
  "variants": [
      { "size": "M", "color": "Red", "quantity": 5 },
      { "size": "L", "color": "Blue", "quantity": 10 }
  ]
}
  • Get a specific product:

    GET http://localhost:3000/products/:id
  • Update a product (Admin Only):

    PUT http://localhost:3000/products/:id
    Content-Type: application/json
    Authorization: Bearer <jwt-token>
  • Delete a product (Admin Only):

    DELETE http://localhost:3000/products/:id
    Authorization: Bearer <jwt-token>

3. Category Management

  • Create a category (Admin Only):

    POST http://localhost:3000/categories
    Content-Type: application/json
    Authorization: Bearer <jwt-token>
  • Get all categories:

    GET http://localhost:3000/categories
  • Get a specific category:

    GET http://localhost:3000/categories/:id
  • Update a category (Admin Only):

    PUT http://localhost:3000/categories/:id
    Authorization: Bearer <jwt-token>
  • Delete a category (Admin Only):

    DELETE http://localhost:3000/categories/:id
    Authorization: Bearer <jwt-token>

4. Search and Filtering

  • Search by name or description:

    GET http://localhost:3000/products?search=nike
  • Filter by price range:

    GET http://localhost:3000/products?minPrice=50&maxPrice=200
  • Filter by stock availability (admin):

    GET http://localhost:3000/products?inStock=true

Error Handling

The API returns structured error messages. Example:

{
  "error": "Product not found",
  "error": "Product Exist variant and stock updated"
}

Testing

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages