Add search for products in all sub categories (#68)

This commit is contained in:
Niklas Fondberg
2021-10-11 19:28:15 +02:00
committed by GitHub
parent d7532848a1
commit 1fbe972af7
4 changed files with 57 additions and 9 deletions
+12 -2
View File
@@ -88,8 +88,8 @@ export class ProductAPI extends BaseSQLDataSource {
return mem;
}, {});
fields['artNo'] = fields['artNo'];
fields['name'] = fields['name'];
fields['artNo'] = fields['artNo'] ?? ''; // some actually exists without artNo
fields['name'] = fields['name'] ?? ''; // some actually exists without name
fields['copyright'] = fields['copyright'] ?? '';
fields['comments'] = fields['comments'] ?? '';
fields['batch'] = fields['batch'] ?? '';
@@ -196,6 +196,16 @@ export class ProductAPI extends BaseSQLDataSource {
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
}
async getCategoryWithSubCategoriesProducts(
categoryId: number,
): Promise<Array<Product>> {
const query = sql.categoryWithAllSubCategoriesProducts(categoryId);
return this.knex
.raw(query)
.then((data) => data.rows.map((row) => this.createProductFromRow(row)));
}
async getKeywordProducts(keywordId: number): Promise<Array<Product>> {
const query = sql.keywordProducts(keywordId);
+32 -4
View File
@@ -121,7 +121,32 @@ export function categoryProducts(categoryId: number) {
return (
baseQuery +
/* sql */ `
WHERE products.productid IN (SELECT product_id FROM product_category WHERE category_id = ${categoryId});
WHERE products.productid IN (SELECT product_id FROM product_category WHERE category_id = ${categoryId})
ORDER BY products.inserted DESC
`
);
}
export function categoryWithAllSubCategoriesProducts(categoryId: number) {
return (
baseQuery +
/* sql */ `
WHERE products.productid IN (
SELECT product_id
FROM product_category
WHERE category_id IN (
SELECT cat.id
FROM categories cat
JOIN (
SELECT id, lft, rgt
FROM categories
WHERE id = ${categoryId}
) tree
ON cat.lft >= tree.lft AND cat.rgt <= tree.rgt
)
ORDER BY product_id
)
ORDER BY products.inserted DESC
`
);
}
@@ -130,7 +155,8 @@ export function designerProducts() {
return (
baseQuery +
/* sql */ `
WHERE products.designerid = ?;
WHERE products.designerid = ?
ORDER BY products.inserted DESC
`
);
}
@@ -139,7 +165,8 @@ export function keywordProducts(keywordId: number) {
return (
baseQuery +
/* sql */ `
WHERE products.productid IN (SELECT product_id FROM product_keyword WHERE keyword_id = ${keywordId});
WHERE products.productid IN (SELECT product_id FROM product_keyword WHERE keyword_id = ${keywordId})
ORDER BY products.inserted DESC
`
);
}
@@ -148,7 +175,8 @@ export function searchByFieldValue(fieldid: number) {
return (
baseQuery +
/* sql */ `
WHERE products.productid IN (SELECT productid FROM "product-products_fields" WHERE fieldid = ${fieldid} AND LOWER(value) LIKE ? LIMIT 5000); -- limit to 5000 results
WHERE products.productid IN (SELECT productid FROM "product-products_fields" WHERE fieldid = ${fieldid} AND LOWER(value) LIKE ? LIMIT 5000) -- limit to 5000 results
ORDER BY products.inserted DESC
`
);
}
+9 -2
View File
@@ -3,9 +3,16 @@ import { CategoryAPI } from '../datasources/category-api';
import { ProductAPI } from '../datasources/product-api';
const Category = {
async products({ id }, _args, { dataSources, auth }) {
async products({ id }, { includeAllSubCategories }, { dataSources, auth }) {
console.log('includeAllSubCategories:', includeAllSubCategories);
checkAccess(['products.read'], auth);
return (<ProductAPI>dataSources.productApi).getCategoryProducts(id);
if (includeAllSubCategories === true) {
return (<ProductAPI>(
dataSources.productApi
)).getCategoryWithSubCategoriesProducts(id);
} else {
return (<ProductAPI>dataSources.productApi).getCategoryProducts(id);
}
},
};
+4 -1
View File
@@ -256,7 +256,10 @@ type Category {
childCount: Int
lft: Int
rgt: Int
products: [Product]
"""
Default to false
"""
products(includeAllSubCategories: Boolean): [Product]
}
type Batch {