Compare commits
2 Commits
02058981bf
...
d82afe5a5f
Author | SHA1 | Date | |
---|---|---|---|
![]() |
d82afe5a5f | ||
![]() |
1216bef8f8 |
@@ -3,11 +3,11 @@ import type { NextRequest } from 'next/server';
|
|||||||
import { logger } from 'src/utils/logger';
|
import { logger } from 'src/utils/logger';
|
||||||
import { STATUS, response, handleError } from 'src/utils/response';
|
import { STATUS, response, handleError } from 'src/utils/response';
|
||||||
|
|
||||||
import { _products } from 'src/_mock/_product';
|
import { getProductBySkuOrName } from 'src/app/services/product.service';
|
||||||
|
|
||||||
// ----------------------------------------------------------------------
|
// ----------------------------------------------------------------------
|
||||||
|
|
||||||
export const runtime = 'edge';
|
// export const runtime = 'edge';
|
||||||
|
|
||||||
/** **************************************
|
/** **************************************
|
||||||
* GET - Search products
|
* GET - Search products
|
||||||
@@ -21,14 +21,11 @@ export async function GET(req: NextRequest) {
|
|||||||
return response({ results: [] }, STATUS.OK);
|
return response({ results: [] }, STATUS.OK);
|
||||||
}
|
}
|
||||||
|
|
||||||
const products = _products();
|
const testResult = await getProductBySkuOrName(query);
|
||||||
|
|
||||||
// Accept search by name or sku
|
logger('[Product] search-results', testResult?.length);
|
||||||
const results = products.filter(({ name, sku }) => name.toLowerCase().includes(query) || sku?.toLowerCase().includes(query));
|
|
||||||
|
|
||||||
logger('[Product] search-results', results.length);
|
return response({ testResult }, STATUS.OK);
|
||||||
|
|
||||||
return response({ results }, STATUS.OK);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return handleError('Product - Get search', error);
|
return handleError('Product - Get search', error);
|
||||||
}
|
}
|
||||||
|
@@ -0,0 +1,5 @@
|
|||||||
|
###
|
||||||
|
GET http://localhost:7272/api/product/search?query=B
|
||||||
|
|
||||||
|
###
|
||||||
|
GET http://localhost:7272/api/product/search?query=Classic
|
@@ -68,9 +68,7 @@ type UpdateProduct = {
|
|||||||
|
|
||||||
async function listProducts(): Promise<ProductItem[]> {
|
async function listProducts(): Promise<ProductItem[]> {
|
||||||
return prisma.productItem.findMany({
|
return prisma.productItem.findMany({
|
||||||
include: {
|
include: { reviews: true },
|
||||||
reviews: true,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,6 +80,14 @@ async function getProduct(productId: string): Promise<ProductItem | null> {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getProductBySkuOrName(searchText: string): Promise<ProductItem[] | null> {
|
||||||
|
return prisma.productItem.findMany({
|
||||||
|
where: { OR: [{ sku: { contains: searchText, mode: 'insensitive' } }, { name: { contains: searchText, mode: 'insensitive' } }] },
|
||||||
|
include: { reviews: true },
|
||||||
|
//
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
async function createProduct(productData: any) {
|
async function createProduct(productData: any) {
|
||||||
return await prisma.productItem.create({ data: productData });
|
return await prisma.productItem.create({ data: productData });
|
||||||
}
|
}
|
||||||
@@ -97,4 +103,4 @@ async function deleteProduct(productId: string) {
|
|||||||
return prisma.productItem.delete({ where: { id: productId } });
|
return prisma.productItem.delete({ where: { id: productId } });
|
||||||
}
|
}
|
||||||
|
|
||||||
export { getProduct, listProducts, createProduct, updateProduct, deleteProduct, type CreateProduct, type UpdateProduct };
|
export { getProduct, listProducts, createProduct, updateProduct, deleteProduct, getProductBySkuOrName, type CreateProduct, type UpdateProduct };
|
||||||
|
@@ -34,9 +34,8 @@
|
|||||||
"node_modules",
|
"node_modules",
|
||||||
".next",
|
".next",
|
||||||
//
|
//
|
||||||
"**/* copy *.tsx",
|
"**/* copy *.*",
|
||||||
"**/* copy.tsx",
|
"**/* copy.*",
|
||||||
"**/*.bak",
|
|
||||||
"**/*.bak",
|
"**/*.bak",
|
||||||
"**/*.bug",
|
"**/*.bug",
|
||||||
"**/*.del",
|
"**/*.del",
|
||||||
|
@@ -1,39 +1,35 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
|
"allowJs": true,
|
||||||
/* Bundler */
|
/* Bundler */
|
||||||
"baseUrl": ".",
|
"baseUrl": ".",
|
||||||
"module": "ESNext",
|
"esModuleInterop": true,
|
||||||
|
"incremental": true,
|
||||||
|
"isolatedModules": true,
|
||||||
"jsx": "react-jsx",
|
"jsx": "react-jsx",
|
||||||
"allowJs": true,
|
|
||||||
"resolveJsonModule": true,
|
|
||||||
/* Build */
|
|
||||||
"target": "ES2020",
|
|
||||||
"lib": [
|
"lib": [
|
||||||
"ES2020",
|
"ES2020",
|
||||||
"DOM",
|
"DOM",
|
||||||
"DOM.Iterable"
|
"DOM.Iterable"
|
||||||
],
|
],
|
||||||
|
"module": "ESNext",
|
||||||
"moduleResolution": "bundler",
|
"moduleResolution": "bundler",
|
||||||
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
|
"noEmit": true,
|
||||||
"incremental": true,
|
"resolveJsonModule": true,
|
||||||
"skipLibCheck": true,
|
"skipLibCheck": true,
|
||||||
"esModuleInterop": true,
|
|
||||||
"isolatedModules": true,
|
|
||||||
/* Linting */
|
/* Linting */
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"noEmit": true,
|
"strictNullChecks": true,
|
||||||
"strictNullChecks": true
|
/* Build */
|
||||||
|
"target": "ES2020",
|
||||||
|
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo"
|
||||||
},
|
},
|
||||||
"include": [
|
|
||||||
"src"
|
|
||||||
],
|
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"node_modules",
|
"node_modules",
|
||||||
".next",
|
".next",
|
||||||
//
|
//
|
||||||
"**/* copy *.tsx",
|
"**/* copy *.*",
|
||||||
"**/* copy.tsx",
|
"**/* copy.*",
|
||||||
"**/*.bak",
|
|
||||||
"**/*.bak",
|
"**/*.bak",
|
||||||
"**/*.bug",
|
"**/*.bug",
|
||||||
"**/*.del",
|
"**/*.del",
|
||||||
@@ -42,9 +38,12 @@
|
|||||||
"**/*.tmp",
|
"**/*.tmp",
|
||||||
"**/*del"
|
"**/*del"
|
||||||
],
|
],
|
||||||
|
"include": [
|
||||||
|
"src"
|
||||||
|
],
|
||||||
"references": [
|
"references": [
|
||||||
{
|
{
|
||||||
"path": "./tsconfig.node.json"
|
"path": "./tsconfig.node.json"
|
||||||
}
|
}
|
||||||
],
|
]
|
||||||
}
|
}
|
||||||
|
@@ -6,8 +6,8 @@
|
|||||||
"allowSyntheticDefaultImports": true
|
"allowSyntheticDefaultImports": true
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"**/* copy *.tsx",
|
"**/* copy *.*",
|
||||||
"**/* copy.tsx",
|
"**/* copy.*",
|
||||||
"**/*.bak",
|
"**/*.bak",
|
||||||
"**/*.bak",
|
"**/*.bak",
|
||||||
"**/*.bug",
|
"**/*.bug",
|
||||||
@@ -16,5 +16,8 @@
|
|||||||
"**/*.log",
|
"**/*.log",
|
||||||
"**/*.tmp",
|
"**/*.tmp",
|
||||||
"**/*del"
|
"**/*del"
|
||||||
], "include": ["vite.config.ts"]
|
],
|
||||||
|
"include": [
|
||||||
|
"vite.config.ts"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
@@ -24,9 +24,8 @@
|
|||||||
"node_modules",
|
"node_modules",
|
||||||
".next",
|
".next",
|
||||||
//
|
//
|
||||||
"**/* copy *.tsx",
|
"**/* copy *.*",
|
||||||
"**/* copy.tsx",
|
"**/* copy.*",
|
||||||
"**/*.bak",
|
|
||||||
"**/*.bak",
|
"**/*.bak",
|
||||||
"**/*.bug",
|
"**/*.bug",
|
||||||
"**/*.del",
|
"**/*.del",
|
||||||
@@ -43,4 +42,4 @@
|
|||||||
"path": "./tsconfig.node.json"
|
"path": "./tsconfig.node.json"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
@@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
|
"allowSyntheticDefaultImports": true,
|
||||||
"composite": true,
|
"composite": true,
|
||||||
"module": "ESNext",
|
"module": "ESNext",
|
||||||
"moduleResolution": "Node",
|
"moduleResolution": "Node"
|
||||||
"allowSyntheticDefaultImports": true
|
|
||||||
},
|
},
|
||||||
"exclude": [
|
"exclude": [
|
||||||
"**/* copy *.tsx",
|
"**/* copy *.tsx",
|
||||||
|
Reference in New Issue
Block a user