Skip to content

Feat: API endpoints to fetch products #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions app/api/products/[productId]/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NextRequest, NextResponse } from 'next/server';
import { getProductById } from '../../../../src/utils/products';

export async function GET(request: Request | NextRequest) {
try {
const { pathname } = new URL(request.url);
const segments = pathname.split('/');
const productId = segments[segments.length - 1];

const product = getProductById(productId);
if (product) {
return NextResponse.json(product);
} else {
return NextResponse.json({ message: 'Product not found' }, { status: 404 });
}
} catch (error) {
console.error(error);
return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
}
}
25 changes: 25 additions & 0 deletions app/api/products/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { NextRequest, NextResponse } from 'next/server';
import { getProducts, searchProducts } from '../../../src/utils/products';

// Handle GET requests
export async function GET(request: Request | NextRequest) {
try {
const { searchParams } = new URL(request.url);
const limit = searchParams.get('limit');
const offset = searchParams.get('offset');
const searchQuery = searchParams.get('search');

if (searchQuery) {
// If a search query is provided, use this function to filter the results
const results = searchProducts(searchQuery, Number(limit), Number(offset));
return NextResponse.json(results);
} else {
// Otherwise, return the paginated list of products
const products = getProducts(Number(limit), Number(offset));
return NextResponse.json(products);
}
} catch (error) {
console.error(error);
return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
}
}
82 changes: 82 additions & 0 deletions docs/backend/products-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
# Product API Documentation

## Overview

This document outlines the implementation of the products API endpoints for the small and large datasets utilizing mock data.

## Endpoints

### 1. List Products

**GET /api/products**:
Retrieve a list of products with optional pagination and search functionality.

**Query Parameters:**

- limit (number, optional): Maximum number of products to return.
- offset (number, optional): Number of products to skip before starting to collect the results.
- search (number, optional): Query string to search for products name.

Examples:
**GET /api/products** to retrieve all products (be aware of the size of the dataset we're working with).
**GET /api/products?limit=10&offset=20** to retrieve products with pagination (ignoring the first 20 and fetching the next 10).
**GET /api/products?search=Awesome** to search for products starting with "Awesome".
**GET /api/products?search=Awesome&limit=5&offset=10** to combine pagination with name searching.

Response always returns a JSON array of product objects matching the criteria in this format:

>

{
"id":"82f5c990-fc36-4931-8678-8af71434ca47",
"name":"Recycled Steel Chips",
"price":"972.00",
"description":"The automobile layout consists of a front-engine design...",
"category":"Movies",
"rating":1.9314758584368974,
"numReviews":53,
"countInStock":52
}

---

### 2. Get Product by ID

**GET /api/products/{id}**:
Retrieves detailed information about a single product by its unique ID.

**Path Parameters:**

- {id} (string): The unique identifier of the product.

Examples:
**GET /api/products/82f5c990-fc36-4931-8678-8af71434ca47** to retrieve the product with the informed Id.

Response returns a JSON object of the product in this format:

>

{
"id":"82f5c990-fc36-4931-8678-8af71434ca47",
"name":"Recycled Steel Chips",
"price":"972.00",
"description":"The automobile layout consists of a front-engine design...",
"category":"Movies",
"rating":1.9314758584368974,
"numReviews":53,
"countInStock":52
}

---

### Testing

Ensure the application is running locally by executing `pnpm dev`.
Use a browser. cURL or Postman for simple `GET` requests.

**Cases:**
List all products: `GET http://localhost:3000/api/products`
List Products with Pagination: `GET http://localhost:3000/api/products?limit=10&offset=0`
Search Products by Name: `GET http://localhost:3000/api/products?search=Awesome`
Combined Pagination and Search: `GET http://localhost:3000/api/products?search=Pro&limit=5&offset=0`
Get Product by ID: `GET GET http://localhost:3000/api/products/82f5c990-fc36-4931-8678-8af71434ca47`
Loading