forked from admin/zeropost-engine
feat: editor_notes + /api/stats/live + tokens в getArticleBySlug
- БД: таблица editor_notes (title/content/author/tags/is_pinned/is_published) - routes/notes.js: CRUD заметок редактора - /api/stats/live: latest article, processing job, активность за 7 дней - getArticleBySlug: JOIN с generation_jobs для tokens_in/out
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const { query } = require('../config/db');
|
||||
|
||||
// GET /api/notes — публичный список заметок
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const limit = Math.min(parseInt(req.query.limit) || 20, 100);
|
||||
const { rows } = await query(
|
||||
`SELECT id, title, content, author, tags, is_pinned, created_at, updated_at
|
||||
FROM editor_notes
|
||||
WHERE is_published=true
|
||||
ORDER BY is_pinned DESC, created_at DESC LIMIT $1`,
|
||||
[limit]
|
||||
);
|
||||
res.json(rows);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/notes/:id
|
||||
router.get('/:id', async (req, res) => {
|
||||
try {
|
||||
const { rows } = await query(
|
||||
`SELECT * FROM editor_notes WHERE id=$1 AND is_published=true`,
|
||||
[req.params.id]
|
||||
);
|
||||
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
||||
res.json(rows[0]);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/notes — создать (требует auth, для админа через app.zeropost.ru)
|
||||
router.post('/', async (req, res) => {
|
||||
try {
|
||||
const { title, content, author, tags = [], is_pinned = false } = req.body;
|
||||
if (!content) return res.status(400).json({ error: 'content required' });
|
||||
const { rows } = await query(
|
||||
`INSERT INTO editor_notes (title, content, author, tags, is_pinned)
|
||||
VALUES ($1,$2,$3,$4,$5) RETURNING *`,
|
||||
[title || null, content, author || 'Редактор', JSON.stringify(tags), is_pinned]
|
||||
);
|
||||
res.json(rows[0]);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// PATCH /api/notes/:id
|
||||
router.patch('/:id', async (req, res) => {
|
||||
try {
|
||||
const { title, content, author, tags, is_pinned, is_published } = req.body;
|
||||
const { rows } = await query(
|
||||
`UPDATE editor_notes SET
|
||||
title=COALESCE($1, title),
|
||||
content=COALESCE($2, content),
|
||||
author=COALESCE($3, author),
|
||||
tags=COALESCE($4::jsonb, tags),
|
||||
is_pinned=COALESCE($5, is_pinned),
|
||||
is_published=COALESCE($6, is_published),
|
||||
updated_at=NOW()
|
||||
WHERE id=$7 RETURNING *`,
|
||||
[
|
||||
title, content, author,
|
||||
tags !== undefined ? JSON.stringify(tags) : null,
|
||||
is_pinned, is_published, req.params.id,
|
||||
]
|
||||
);
|
||||
if (!rows.length) return res.status(404).json({ error: 'Not found' });
|
||||
res.json(rows[0]);
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE /api/notes/:id
|
||||
router.delete('/:id', async (req, res) => {
|
||||
try {
|
||||
await query(`DELETE FROM editor_notes WHERE id=$1`, [req.params.id]);
|
||||
res.json({ ok: true });
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
@@ -36,4 +36,54 @@ router.get('/', async (_, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/stats/live — «прямо сейчас»: текущая активность и активность за 7 дней
|
||||
router.get('/live', async (_, res) => {
|
||||
try {
|
||||
// Последняя опубликованная статья
|
||||
const { rows: latestRow } = await query(
|
||||
`SELECT a.id, a.slug, a.title, a.published_at, j.tokens_in, j.tokens_out
|
||||
FROM articles a
|
||||
LEFT JOIN generation_jobs j ON j.id=a.job_id
|
||||
WHERE a.status='published'
|
||||
ORDER BY a.published_at DESC LIMIT 1`
|
||||
);
|
||||
|
||||
// Сейчас обрабатывается?
|
||||
const { rows: processing } = await query(
|
||||
`SELECT id, type, topic, created_at FROM generation_jobs
|
||||
WHERE status IN ('pending','processing')
|
||||
ORDER BY created_at DESC LIMIT 1`
|
||||
);
|
||||
|
||||
// Активность по дням за 7 дней
|
||||
const { rows: byDay } = await query(
|
||||
`SELECT
|
||||
DATE_TRUNC('day', published_at) as day,
|
||||
COUNT(*)::int as cnt
|
||||
FROM articles
|
||||
WHERE status='published' AND published_at > NOW() - INTERVAL '7 days'
|
||||
GROUP BY day ORDER BY day ASC`
|
||||
);
|
||||
|
||||
// Запоняем пропущенные дни нулями
|
||||
const week = [];
|
||||
for (let i = 6; i >= 0; i--) {
|
||||
const d = new Date();
|
||||
d.setDate(d.getDate() - i);
|
||||
d.setHours(0, 0, 0, 0);
|
||||
const dayStr = d.toISOString().slice(0, 10);
|
||||
const found = byDay.find(r => r.day && new Date(r.day).toISOString().slice(0, 10) === dayStr);
|
||||
week.push({ day: dayStr, cnt: found ? found.cnt : 0 });
|
||||
}
|
||||
|
||||
res.json({
|
||||
latest: latestRow[0] || null,
|
||||
processing: processing[0] || null,
|
||||
week,
|
||||
});
|
||||
} catch (err) {
|
||||
res.status(500).json({ error: err.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user