Kategorien

This commit is contained in:
2025-10-01 21:46:15 +02:00
parent 70991a4345
commit fd0542f2a4
6 changed files with 173 additions and 17 deletions

View File

@@ -46,6 +46,18 @@ function slugify(value) {
.replace(/(^-|-$)+/g, '') || crypto.randomUUID()
}
function normalizeCategoryForSearch(category) {
if (!category) return null
// Convert slug format to display format for backward compatibility
const categoryMap = {
'books-magazine': 'Books & Magazine',
'clothing-shoes': 'Clothing & Shoes',
'collectibles-art': 'Collectibles & Art',
'toys-hobbies': 'Toys & Hobbies'
}
return categoryMap[category.toLowerCase()] || category
}
async function generateUniqueSlug(title, excludeId) {
const base = slugify(title)
let slug = base
@@ -138,6 +150,7 @@ function mapPostRow(row) {
sections: row.sections || [],
footer: row.footer,
isEditorsPick: row.is_editors_pick,
category: row.category,
createdAt: row.created_at,
updatedAt: row.updated_at
}
@@ -192,11 +205,21 @@ function getUploadFields() {
return fields
}
app.get('/posts', async (_req, res) => {
app.get('/posts', async (req, res) => {
try {
const result = await query(
'SELECT * FROM blog_posts ORDER BY created_at DESC'
)
const { category } = req.query
let queryText = 'SELECT * FROM blog_posts'
const queryParams = []
if (category) {
const normalizedCategory = normalizeCategoryForSearch(category)
queryText += ' WHERE category = $1'
queryParams.push(normalizedCategory)
}
queryText += ' ORDER BY created_at DESC'
const result = await query(queryText, queryParams)
const posts = result.rows.map(mapPostRow).map(post => ({
...post,
excerpt: createExcerpt(post.sections)
@@ -251,8 +274,8 @@ app.post('/posts', upload.fields(getUploadFields()), async (req, res) => {
await ensureEditorsPickLimit(null, isEditorsPick)
const result = await query(
`INSERT INTO blog_posts (title, slug, preview_image, link_url, sections, footer, is_editors_pick)
VALUES ($1, $2, $3, $4, $5, $6, $7)
`INSERT INTO blog_posts (title, slug, preview_image, link_url, sections, footer, is_editors_pick, category)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING *`,
[
payload.title.trim(),
@@ -261,7 +284,8 @@ app.post('/posts', upload.fields(getUploadFields()), async (req, res) => {
payload.linkUrl || null,
JSON.stringify(sections),
payload.footer || null,
isEditorsPick
isEditorsPick,
payload.category || null
]
)
@@ -317,8 +341,9 @@ app.put('/posts/:id', upload.fields(getUploadFields()), async (req, res) => {
link_url = $4,
sections = $5,
footer = $6,
is_editors_pick = $7
WHERE id = $8
is_editors_pick = $7,
category = $8
WHERE id = $9
RETURNING *`,
[
payload.title.trim(),
@@ -328,6 +353,7 @@ app.put('/posts/:id', upload.fields(getUploadFields()), async (req, res) => {
JSON.stringify(sections),
payload.footer || null,
isEditorsPick,
payload.category || null,
id
]
)