Skip to content

Commit 8fd6345

Browse files
author
Luan Andrey Vieira
committed
API endpoints to fecth products
1 parent eb2deee commit 8fd6345

File tree

5 files changed

+23114
-0
lines changed

5 files changed

+23114
-0
lines changed

Diff for: app/api/products/[productId]/route.ts

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { NextResponse } from 'next/server';
2+
import { getProductById } from '../../../../src/utils/products';
3+
4+
export async function GET(request: { url: string | URL }) {
5+
try {
6+
const { pathname } = new URL(request.url);
7+
const segments = pathname.split('/');
8+
const productId = segments[segments.length - 1];
9+
10+
const product = getProductById(productId);
11+
if (product) {
12+
return NextResponse.json(product);
13+
} else {
14+
return NextResponse.json({ message: 'Product not found' }, { status: 404 });
15+
}
16+
} catch (error) {
17+
console.error(error);
18+
return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
19+
}
20+
}

Diff for: app/api/products/route.ts

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { NextResponse } from 'next/server';
2+
import { getProducts, searchProducts } from '../../../src/utils/products';
3+
4+
// Handle GET requests
5+
export async function GET(request: { url: string | URL }) {
6+
try {
7+
const { searchParams } = new URL(request.url);
8+
const limit = searchParams.get('limit');
9+
const offset = searchParams.get('offset');
10+
const searchQuery = searchParams.get('search');
11+
12+
if (searchQuery) {
13+
// If a search query is provided, use this function to filter the results
14+
const results = searchProducts(searchQuery, Number(limit), Number(offset));
15+
return NextResponse.json(results);
16+
} else {
17+
// Otherwise, return the paginated list of products
18+
const products = getProducts(Number(limit), Number(offset));
19+
return NextResponse.json(products);
20+
}
21+
} catch (error) {
22+
console.error(error);
23+
return NextResponse.json({ message: 'Internal server error' }, { status: 500 });
24+
}
25+
}

Diff for: docs/backend/products-api.md

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Product API Documentation
2+
3+
## Overview
4+
5+
This document outlines the implementation of the products API endpoints for the small and large datasets utilizing mock data.
6+
7+
## Endpoints
8+
9+
### 1. List Products
10+
11+
**GET /api/products**:
12+
Retrieve a list of products with optional pagination and search functionality.
13+
14+
**Query Parameters:**
15+
16+
- limit (number, optional): Maximum number of products to return.
17+
- offset (number, optional): Number of products to skip before starting to collect the results.
18+
- search (number, optional): Query string to search for products name.
19+
20+
Examples:
21+
**GET /api/products** to retrieve all products (be aware of the size of the dataset we're working with).
22+
**GET /api/products?limit=10&offset=20** to retrieve products with pagination (ignoring the first 20 and fetching the next 10).
23+
**GET /api/products?search=Awesome** to search for products starting with "Awesome".
24+
**GET /api/products?search=Awesome&limit=5&offset=10** to combine pagination with name searching.
25+
26+
Response always returns a JSON array of product objects matching the criteria in this format:
27+
28+
>
29+
30+
{
31+
"id":"82f5c990-fc36-4931-8678-8af71434ca47",
32+
"name":"Recycled Steel Chips",
33+
"price":"972.00",
34+
"description":"The automobile layout consists of a front-engine design...",
35+
"category":"Movies",
36+
"rating":1.9314758584368974,
37+
"numReviews":53,
38+
"countInStock":52
39+
}
40+
41+
---
42+
43+
### 2. Get Product by ID
44+
45+
**GET /api/products/{id}**:
46+
Retrieves detailed information about a single product by its unique ID.
47+
48+
**Path Parameters:**
49+
50+
- {id} (string): The unique identifier of the product.
51+
52+
Examples:
53+
**GET /api/products/82f5c990-fc36-4931-8678-8af71434ca47** to retrieve the product with the informed Id.
54+
55+
Response returns a JSON object of the product in this format:
56+
57+
>
58+
59+
{
60+
"id":"82f5c990-fc36-4931-8678-8af71434ca47",
61+
"name":"Recycled Steel Chips",
62+
"price":"972.00",
63+
"description":"The automobile layout consists of a front-engine design...",
64+
"category":"Movies",
65+
"rating":1.9314758584368974,
66+
"numReviews":53,
67+
"countInStock":52
68+
}
69+
70+
---
71+
72+
### Testing
73+
74+
Ensure the application is running locally by executing `pnpm dev`.
75+
Use a browser. cURL or Postman for simple `GET` requests.
76+
77+
**Cases:**
78+
List all products: `GET http://localhost:3000/api/products`
79+
List Products with Pagination: `GET http://localhost:3000/api/products?limit=10&offset=0`
80+
Search Products by Name: `GET http://localhost:3000/api/products?search=Awesome`
81+
Combined Pagination and Search: `GET http://localhost:3000/api/products?search=Pro&limit=5&offset=0`
82+
Get Product by ID: `GET GET http://localhost:3000/api/products/82f5c990-fc36-4931-8678-8af71434ca47`

0 commit comments

Comments
 (0)