3871 - Add ids filter for products query (#154)

This commit is contained in:
Anders Gustafsson
2023-05-31 14:38:23 +02:00
committed by GitHub
parent 6feb7ee388
commit 1b6c24a5d2
4 changed files with 57 additions and 26 deletions
+7 -2
View File
@@ -203,7 +203,10 @@ export class ProductAPI extends BaseSQLDataSource {
Scopes.PRODUCTS_PUBLIC_READ, Scopes.PRODUCTS_PUBLIC_READ,
]); ]);
const query = sql.productsTotal(input); const query = sql.productsTotal(input);
const res = await this.cachedRaw(query, null)
const variables = input.filter?.ids ? input.filter.ids : [];
const res = await this.cachedRaw(query, variables)
.cache(MINUTE * 5) .cache(MINUTE * 5)
.then((data) => data.rows); .then((data) => data.rows);
return res[0]['count'] as number; // Optimize later to return Promise return res[0]['count'] as number; // Optimize later to return Promise
@@ -216,8 +219,10 @@ export class ProductAPI extends BaseSQLDataSource {
]); ]);
const query = sql.products(input); const query = sql.products(input);
const variables = input.filter?.ids ? input.filter.ids : [];
return this.knex return this.knex
.raw(query) .raw(query, variables)
.then((data) => data.rows.map((row) => this.createProductFromRow(row))); .then((data) => data.rows.map((row) => this.createProductFromRow(row)));
} }
+48 -24
View File
@@ -1,4 +1,5 @@
import { ProductsFilterInput } from '../../types/product-types'; import { ProductsFilter, ProductsFilterInput } from '../../types/product-types';
import { createQuestionMarksFromList } from '../utils';
// Get syntax highlighting with vscode by installing "Comment tagged templates2 // Get syntax highlighting with vscode by installing "Comment tagged templates2
@@ -76,38 +77,61 @@ FROM "product-products" products
LEFT JOIN stockproducts stock ON stock.product_id = products.productid LEFT JOIN stockproducts stock ON stock.product_id = products.productid
`; `;
const addFiltersToQuery = (baseQuery: string, filter: ProductsFilter) => {
if (!filter) {
return baseQuery;
}
if ('browsable' in filter || 'visible' in filter || 'ids' in filter) {
let query = /* sql */ `${baseQuery} WHERE`;
if ('browsable' in filter) {
query = /* sql */ `${query} products.browsable = ${
filter.browsable ? '1' : '0'
}`;
}
if ('visible' in filter) {
if ('browsable' in filter) {
query = /* sql */ `${query} AND`;
}
query = /* sql */ `${query} products.visible = ${
filter.visible ? '1' : '0'
}`;
}
if ('ids' in filter) {
if ('browsable' in filter || 'visible' in filter) {
query = /* sql */ `${query} AND`;
}
query = /* sql */ `${query} products.productid IN (${createQuestionMarksFromList(
filter.ids,
)})`;
}
return query;
}
};
export function products(input: ProductsFilterInput) { export function products(input: ProductsFilterInput) {
const limit = input.pagination.limit ?? 100; const limit = input.pagination.limit ?? 100;
const offset = input.pagination.offset ?? 0; const offset = input.pagination.offset ?? 0;
const filter = input.filter ?? null; const filter = input.filter ?? null;
return (
baseQuery + const query = addFiltersToQuery(baseQuery, filter);
/* sql */ `
${ return /* sql */ `
filter?.visible != null ${query}
? /* sql */ ` ORDER BY products.publishing_date DESC, products.productid DESC
WHERE products.visible = ${filter?.visible ? '1' : '0'} LIMIT ${limit} OFFSET ${offset}
AND products.browsable = ${filter?.browsable ? '1' : '0'}` `;
: ``
}
ORDER BY products.publishing_date DESC, products.productid DESC
LIMIT ${limit} OFFSET ${offset}
`
);
} }
export function productsTotal(input: ProductsFilterInput) { export function productsTotal(input: ProductsFilterInput) {
const filter = input.filter; const filter = input.filter;
return /*sql */ ` const query = /*sql */ `
SELECT count(*) FROM "product-products" products SELECT count(*) FROM "product-products" products
${
filter?.visible != null
? /* sql */ `
WHERE products.visible = ${filter?.visible ? '1' : '0'}
AND products.browsable = ${filter?.browsable ? '1' : '0'}`
: ``
}
`; `;
return addFiltersToQuery(query, filter);
} }
export function product() { export function product() {
+1
View File
@@ -78,6 +78,7 @@ input DateFilterInput {
input ProductsFilterInput { input ProductsFilterInput {
visible: Boolean visible: Boolean
browsable: Boolean browsable: Boolean
ids: [Int]
} }
type PaginationResult { type PaginationResult {
+1
View File
@@ -47,6 +47,7 @@ export interface ProductsFilterInput {
export interface ProductsFilter { export interface ProductsFilter {
visible?: boolean; visible?: boolean;
browsable?: boolean; browsable?: boolean;
ids?: number[];
} }
export interface ProductsSearchResult { export interface ProductsSearchResult {