Add product

This commit is contained in:
Niklas Fondberg
2021-04-08 15:09:31 +02:00
parent e0f629d54d
commit f60a51a94b
11 changed files with 184 additions and 171 deletions
+58 -45
View File
@@ -2,54 +2,59 @@ import { Maybe } from '../types';
// Get syntax highlighting with vscode by installing "Comment tagged templates2
const baseQuery = /* sql */ `
SELECT products.productid as id,
products.path,
products.visible,
products.browsable,
products.designerid,
products.inserted,
products.updated,
(
SELECT json_agg(
json_build_object(
'printId',
"product-printproducts".printid,
'productId',
"product-printproducts".productid,
'groupId',
"product-printproducts".groupid,
'inserted',
"product-printproducts".inserted,
'updated',
"product-printproducts".updated
)
) FROM "product-printproducts" WHERE "product-printproducts".productid = products.productid
) AS printProducts,
( SELECT json_agg(
json_build_object(
'id',
product_blacklist.id,
'productId',
product_blacklist.product_id,
'marketId',
product_blacklist.market_id,
'groupId',
product_blacklist.group_id,
'inserted',
product_blacklist.created_at,
'updated',
product_blacklist.updated_at
)
) FROM product_blacklist WHERE product_blacklist.product_id = products.productid
) AS blacklisting
FROM "product-products" products
`;
export function products(
limit: number = 100,
visible: Maybe<boolean>,
browsable: Maybe<boolean>,
) {
const query = /* sql */ `
SELECT products.productid as id,
products.path,
products.visible,
products.browsable,
products.designerid,
products.inserted,
products.updated,
(
SELECT json_agg(
json_build_object(
'printId',
"product-printproducts".printid,
'productId',
"product-printproducts".productid,
'groupId',
"product-printproducts".groupid,
'inserted',
"product-printproducts".inserted,
'updated',
"product-printproducts".updated
)
) FROM "product-printproducts" WHERE "product-printproducts".productid = products.productid
) AS printProducts,
( SELECT json_agg(
json_build_object(
'id',
product_blacklist.id,
'productId',
product_blacklist.product_id,
'marketId',
product_blacklist.market_id,
'groupId',
product_blacklist.group_id,
'inserted',
product_blacklist.created_at,
'updated',
product_blacklist.updated_at
)
) FROM product_blacklist WHERE product_blacklist.product_id = products.productid
) AS blacklisting
FROM "product-products" products
return (
baseQuery +
/* sql */ `
${
visible != null
? /* sql */ `
@@ -58,7 +63,15 @@ export function products(
: ``
}
LIMIT ${limit}
`;
`
);
}
return query;
export function product(id: number) {
return (
baseQuery +
/* sql */ `
WHERE products.productid = ${id}
`
);
}