You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
745 B
29 lines
745 B
/**
|
|
* GET /api/products
|
|
*
|
|
* Returns a list of all active products available for purchase.
|
|
* Products are sorted by category and name.
|
|
*/
|
|
|
|
import { eq } from 'drizzle-orm'
|
|
import { products } from '../../database/schema'
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const db = useDatabase()
|
|
|
|
try {
|
|
// Fetch all active products
|
|
const allProducts = await db.query.products.findMany({
|
|
where: eq(products.active, true),
|
|
orderBy: (products, { asc }) => [asc(products.category), asc(products.name)],
|
|
})
|
|
|
|
return allProducts
|
|
} catch (error) {
|
|
console.error('Error fetching products:', error)
|
|
throw createError({
|
|
statusCode: 500,
|
|
statusMessage: 'Failed to fetch products',
|
|
})
|
|
}
|
|
})
|
|
|