import { app, HttpRequest, HttpResponseInit, InvocationContext } from "@azure/functions";
import { products, Product } from "../shared/data.js";
/**
* GET /api/products - List all products with optional filtering
* Query parameters:
* - category: Filter by product category
* - minPrice: Minimum price filter
* - maxPrice: Maximum price filter
*/
export async function getProducts(request: HttpRequest, context: InvocationContext): Promise<HttpResponseInit> {
context.log(`getProducts function processed a request`);
const category = request.query.get("category");
const minPrice = request.query.get("minPrice");
const maxPrice = request.query.get("maxPrice");
let result: Product[] = [...products];
// Apply category filter
if (category) {
result = result.filter(p => p.category.toLowerCase() === category.toLowerCase());
}
// Apply price filters
if (minPrice) {
const min = parseFloat(minPrice);
if (!isNaN(min)) {
result = result.filter(p => p.price >= min);
}
}
if (maxPrice) {
const max = parseFloat(maxPrice);
if (!isNaN(max)) {
result = result.filter(p => p.price <= max);
}
}
return {
status: 200,
jsonBody: {
success: true,
count: result.length,
products: result
}
};
}
app.http("getProducts", {
methods: ["GET"],
authLevel: "function",
route: "products",
handler: getProducts
});