chore: prepare project for server deployment
This commit is contained in:
@@ -0,0 +1,149 @@
|
||||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const PublicApiService = require('../services/PublicApiService');
|
||||
const { getDb } = require('../config/database');
|
||||
|
||||
// 获取前台展示配置(只读)
|
||||
router.get('/settings', async (req, res) => {
|
||||
try {
|
||||
const rows = getDb().prepare('SELECT setting_key, setting_value FROM site_settings').all();
|
||||
res.json(Object.fromEntries(rows.map(row => [row.setting_key, row.setting_value])));
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取已发布的实体列表
|
||||
router.get('/entities', async (req, res) => {
|
||||
try {
|
||||
const { page = 1, pageSize = 10, orderBy } = req.query;
|
||||
const offset = (parseInt(page) - 1) * parseInt(pageSize);
|
||||
const entities = PublicApiService.getPublishedEntities({
|
||||
orderBy,
|
||||
limit: parseInt(pageSize),
|
||||
offset
|
||||
});
|
||||
res.json(entities);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取已发布的实体详情
|
||||
router.get('/entities/:id', async (req, res) => {
|
||||
try {
|
||||
const entity = PublicApiService.getPublishedEntityById(req.params.id);
|
||||
if (!entity) {
|
||||
return res.status(404).json({ error: '实体不存在' });
|
||||
}
|
||||
res.json(entity);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取实体的已发布关系
|
||||
router.get('/entities/:id/relationships', async (req, res) => {
|
||||
try {
|
||||
const sourceRelationships = PublicApiService.getPublishedRelationshipsBySource(req.params.id);
|
||||
const targetRelationships = PublicApiService.getPublishedRelationshipsByTarget(req.params.id);
|
||||
|
||||
res.json({
|
||||
source: sourceRelationships,
|
||||
target: targetRelationships
|
||||
});
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取实体的已发布内容
|
||||
router.get('/entities/:id/contents', async (req, res) => {
|
||||
try {
|
||||
const contents = PublicApiService.getEntityPublishedContents(req.params.id);
|
||||
res.json(contents);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取已发布的内容列表
|
||||
router.get('/contents', async (req, res) => {
|
||||
try {
|
||||
const { type, page = 1, pageSize = 10, orderBy } = req.query;
|
||||
const offset = (parseInt(page) - 1) * parseInt(pageSize);
|
||||
const contents = PublicApiService.getPublishedContents(type, {
|
||||
orderBy,
|
||||
limit: parseInt(pageSize),
|
||||
offset
|
||||
});
|
||||
res.json(contents);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取已发布的内容详情
|
||||
router.get('/contents/:id', async (req, res) => {
|
||||
try {
|
||||
const content = PublicApiService.getPublishedContentById(req.params.id);
|
||||
if (!content) {
|
||||
return res.status(404).json({ error: '内容不存在或未发布' });
|
||||
}
|
||||
res.json(content);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 获取内容的实体关联
|
||||
router.get('/contents/:id/entities', async (req, res) => {
|
||||
try {
|
||||
const entities = PublicApiService.getContentEntities(req.params.id);
|
||||
res.json(entities);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 搜索实体
|
||||
router.get('/search/entities', async (req, res) => {
|
||||
try {
|
||||
const { keyword, page = 1, pageSize = 10, orderBy } = req.query;
|
||||
if (!keyword) {
|
||||
return res.status(400).json({ error: '搜索关键词不能为空' });
|
||||
}
|
||||
|
||||
const offset = (parseInt(page) - 1) * parseInt(pageSize);
|
||||
const entities = PublicApiService.searchEntities(keyword, {
|
||||
orderBy,
|
||||
limit: parseInt(pageSize),
|
||||
offset
|
||||
});
|
||||
res.json(entities);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// 搜索内容
|
||||
router.get('/search/contents', async (req, res) => {
|
||||
try {
|
||||
const { keyword, type, page = 1, pageSize = 10, orderBy } = req.query;
|
||||
if (!keyword) {
|
||||
return res.status(400).json({ error: '搜索关键词不能为空' });
|
||||
}
|
||||
|
||||
const offset = (parseInt(page) - 1) * parseInt(pageSize);
|
||||
const contents = PublicApiService.searchContents(keyword, type, {
|
||||
orderBy,
|
||||
limit: parseInt(pageSize),
|
||||
offset
|
||||
});
|
||||
res.json(contents);
|
||||
} catch (error) {
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
Reference in New Issue
Block a user