Files
dashlibstorage/src/sql/category.js
2026-08-02 19:02:10 +02:00

98 lines
2.1 KiB
JavaScript

export const GET_TREE_FOR_COMPANY = `
WITH tree AS (
SELECT id, parent_id, sort
FROM category
WHERE id = @rootId
UNION ALL
SELECT c.id, c.parent_id, c.sort
FROM category c
INNER JOIN tree t ON c.parent_id = t.id
)
SELECT
t.id,
t.parent_id AS parentId,
t.sort,
ct.name,
ct.slug,
p.thumbw AS thumbW,
p.thumbh AS thumbH,
p.thumbhash AS thumbHash
FROM tree t
LEFT JOIN categorytext ct
ON ct.category_id = t.id AND ct.language_id = @languageId
OUTER APPLY (
SELECT TOP 1 cp.pic_id
FROM categorypic cp
WHERE cp.category_id = t.id
ORDER BY cp.sort ASC, cp.id ASC
) first_pic
LEFT JOIN pic p ON p.id = first_pic.pic_id
ORDER BY t.sort, t.id
`;
import {
ARTICLE_LIST_COLUMNS,
ARTICLE_LIST_JOINS,
} from './article.js';
export const GET_CATEGORY_ARTICLES = `
SELECT
${ARTICLE_LIST_COLUMNS}
FROM categoryarticle ca
INNER JOIN article a ON a.id = ca.article_id
${ARTICLE_LIST_JOINS}
WHERE ca.category_id = @categoryId
ORDER BY a.id
`;
export const CREATE = `
INSERT INTO category (parent_id, sort)
OUTPUT INSERTED.id, INSERTED.parent_id AS parentId, INSERTED.sort
VALUES (@parentId, @sort)
`;
export const RESOLVE_CATEGORY_ACCESS = `
WITH ancestors AS (
SELECT id, parent_id
FROM category
WHERE id = @categoryId
UNION ALL
SELECT c.id, c.parent_id
FROM category c
INNER JOIN ancestors a ON c.id = a.parent_id
),
root AS (
SELECT TOP 1 id AS rootCategoryId
FROM ancestors
WHERE parent_id IS NULL
)
SELECT
p.id AS categoryId,
r.rootCategoryId,
co.id AS companyId,
co.default_language_id AS languageId,
co.poscustomergroup_id AS posCustomerGroupId,
cu.id AS companyuserId
FROM category p
LEFT JOIN root r ON 1 = 1
LEFT JOIN company co ON co.root_category_id = r.rootCategoryId
LEFT JOIN companyuser cu
ON cu.company_id = co.id AND cu.google_auth_id = @googleAuthId
WHERE p.id = @categoryId
`;
export const GET_NEXT_SIBLING_SORT = `
SELECT ISNULL(MAX(sort), -1) + 1 AS sort
FROM category
WHERE parent_id = @parentId
`;
export const DELETE = `
DELETE FROM category
WHERE id = @id
`;