Thanks to visit codestin.com
Credit goes to github.com

Skip to content

kamimehrabi/e_commerce_api

Repository files navigation

E-Commerce API Backend

This repository contains the backend implementation for the E-Commerce Product Page interview task, built with NestJS, Supabase, and TypeScript. It provides a RESTful API for products, comments, and cart management, with user authentication via Supabase.

Table of Contents

Features

  • Authentication: Email/password signup & signin via Supabase
  • Products: CRUD operations on products
  • Comments: Post and retrieve comments with star ratings (1–5), only for logged-in users
  • Cart Management: Add, retrieve, and remove cart items, with quantity accumulation
  • Error Handling: Consistent HTTP status codes and messages, no raw DB errors leaked
  • TypeScript: Strong typing for interfaces, DTOs, and Supabase interactions
  • REST API: NestJS controllers, services, and guards with Supabase as the database layer

Tech Stack

  • Framework: NestJS (TypeScript)
  • Database & Auth: Supabase (PostgreSQL + PostgREST + GoTrue)
  • Validation: class-validator & class-transformer
  • HTTP Client: Postman (collection included)

Getting Started

Prerequisites

  • Node.js v16+ / npm or Yarn
  • A Supabase project (URL & Key)
  • Git

Installation

  1. Clone the repo:
    git clone https://https://github.com/kamimehrabi/e-commerce-api.git
    cd e-commerce-api
  2. Install dependencies:
    yarn install
    # or
    npm install

Environment Variables

Create a .env file in the project root with the following values:

SUPABASE_URL=<your-supabase-url>
SUPABASE_KEY=<your-supabase-service-role-key>

Database Setup

This project uses a Supabase backend with all tables, views, and functions already created. No manual migrations are required—simply point your .env at the provided database and you’re ready to go.

The following views are available out of the box:

  • ``** view**: joins comments → `auth.users`, exposing each comment with the author’s `email` and `display_name`.
  • ``** view**: left‑joins products with `comments` and computes each product’s average star rating (`avg_rating`).

You can see their definitions under supabase/migrations/ for reference, but running them locally is optional since the live database already contains these objects.

Running the App

Start the NestJS server in development mode:

npm run start:dev
# or
yarn start:dev

Server will be running at http://localhost:3000.

API Endpoints

Auth

  • POST /auth/signup – Register a new user (displayName, email, password)
  • POST /auth/signin – Log in and receive an access token (email, password)

Products

  • GET /products – List all products
  • GET /products/:id – Retrieve a single product with average rating
  • POST /products – Create a product (protected)
  • PATCH /products/:id – Update a product (protected)
  • DELETE /products/:id – Delete a product (protected)

Comments

  • GET /products/:productId/comments – List comments (with user display name)
  • POST /products/:productId/comments – Add a comment (protected)

Cart

  • GET /cart – Retrieve current user’s cart items (protected)
  • POST /cart – Add or increment a cart item (protected)
  • DELETE /cart/:id – Remove a cart item (protected)

Testing

  • Use the included Postman collection (postman_collection.json) to test all endpoints.
  • Create a Postman environment (e.g. Local Dev) with the following variables:
    Variable Initial Value Description
    base http://localhost:3000 The root URL of your API
    token Obtained from POST /auth/signin response
  1. Import the collection into Postman.
  2. Select the Local Dev environment.
  3. Run the signin request in the Auth folder to populate the token variable.
  4. All other protected requests will automatically use {{base}} and the Authorization: Bearer {{token}} header.

Attachments

  • Postman Collection: postman_collection.json in the repository root
  • Environment Template: .env.example

Credentials

I will send an email containing the following test credentials(email and password) for the login route.


About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published