P5 7619 refactor products to return list (#11)

This commit is contained in:
Niklas Fondberg
2021-08-20 08:16:01 +02:00
committed by GitHub
parent 234996abfd
commit 964a904f42
15 changed files with 235 additions and 92 deletions
+21 -8
View File
@@ -1,3 +1,4 @@
import { ProductsFilter, ProductsFilterInput } from '../../types/product-types';
import { Maybe } from '../../types/types';
// Get syntax highlighting with vscode by installing "Comment tagged templates2
@@ -75,19 +76,17 @@ FROM "product-products" products
LEFT JOIN "product-stockproducts" stock ON stock.productid = products.productid
`;
export function products(
limit: number = 100,
visible: Maybe<boolean>,
browsable: Maybe<boolean>,
) {
export function products(input: ProductsFilterInput) {
const limit = input.pagination.limit ?? 100;
const filter = input.filter;
return (
baseQuery +
/* sql */ `
${
visible != null
filter.visible != null
? /* sql */ `
WHERE products.visible = ${visible ? '1' : '0'}
AND products.browsable = ${browsable ? '1' : '0'}`
WHERE products.visible = ${filter?.visible ? '1' : '0'}
AND products.browsable = ${filter?.browsable ? '1' : '0'}`
: ``
}
LIMIT ${limit}
@@ -95,6 +94,20 @@ LIMIT ${limit}
);
}
export function productsTotal(input: ProductsFilterInput) {
const filter = input.filter;
return /*sql */ `
SELECT count(*) FROM "product-products" products
${
filter.visible != null
? /* sql */ `
WHERE products.visible = ${filter?.visible ? '1' : '0'}
AND products.browsable = ${filter?.browsable ? '1' : '0'}`
: ``
}
`;
}
export function product(id: number) {
return (
baseQuery +