import { getDb, type ArticleBlock } from './db' /** * 内容读取层(动态/作品)。 * 统一负责 DB 行 -> 业务对象(解析 content/tags/gallery 的 JSON), * 供前台内容 API、后台管理 API、sitemap 共用。 */ export interface Article { id: number slug: string category: 'insights' | 'projects' | 'announcements' titleZh: string titleEn: string excerptZh: string excerptEn: string contentZh: ArticleBlock[] contentEn: ArticleBlock[] image: string | null date: string | null readTime: number author: string published: number createdAt: string updatedAt: string } export interface Work { id: number slug: string category: 'webDesign' | 'branding' | 'development' titleZh: string titleEn: string descriptionZh: string descriptionEn: string overviewZh: string overviewEn: string challengeZh: string challengeEn: string solutionZh: string solutionEn: string clientZh: string clientEn: string durationZh: string durationEn: string year: string tags: string[] image: string | null gallery: string[] url: string | null published: number createdAt: string updatedAt: string } const parseJson = (value: string | null, fallback: T): T => { if (!value) return fallback try { return JSON.parse(value) as T } catch { return fallback } } function rowToArticle(row: Record): Article { return { id: Number(row.id), slug: String(row.slug), category: row.category as Article['category'], titleZh: String(row.title_zh), titleEn: String(row.title_en), excerptZh: String(row.excerpt_zh), excerptEn: String(row.excerpt_en), contentZh: parseJson(String(row.content_zh ?? ''), []), contentEn: parseJson(String(row.content_en ?? ''), []), image: row.image ? String(row.image) : null, date: row.date ? String(row.date) : null, readTime: Number(row.read_time) || 5, author: String(row.author), published: Number(row.published) || 0, createdAt: String(row.created_at), updatedAt: String(row.updated_at), } } function rowToWork(row: Record): Work { return { id: Number(row.id), slug: String(row.slug), category: row.category as Work['category'], titleZh: String(row.title_zh), titleEn: String(row.title_en), descriptionZh: String(row.description_zh), descriptionEn: String(row.description_en), overviewZh: String(row.overview_zh), overviewEn: String(row.overview_en), challengeZh: String(row.challenge_zh), challengeEn: String(row.challenge_en), solutionZh: String(row.solution_zh), solutionEn: String(row.solution_en), clientZh: String(row.client_zh), clientEn: String(row.client_en), durationZh: String(row.duration_zh), durationEn: String(row.duration_en), year: String(row.year), tags: parseJson(String(row.tags ?? ''), []), image: row.image ? String(row.image) : null, gallery: parseJson(String(row.gallery ?? ''), []), url: row.url ? String(row.url) : null, published: Number(row.published) || 0, createdAt: String(row.created_at), updatedAt: String(row.updated_at), } } const ARTICLE_COLS = `id, slug, category, title_zh, title_en, excerpt_zh, excerpt_en, content_zh, content_en, image, date, read_time, author, published, created_at, updated_at` const WORK_COLS = `id, slug, category, title_zh, title_en, description_zh, description_en, overview_zh, overview_en, challenge_zh, challenge_en, solution_zh, solution_en, client_zh, client_en, duration_zh, duration_en, year, tags, image, gallery, url, published, created_at, updated_at` export function listArticles(options: { publishedOnly?: boolean } = {}): Article[] { const { publishedOnly = true } = options const sql = `SELECT ${ARTICLE_COLS} FROM articles ${publishedOnly ? 'WHERE published = 1' : ''} ORDER BY date DESC, id DESC` const rows = getDb().prepare(sql).all() as Array> return rows.map(rowToArticle) } export function getArticleBySlug(slug: string, options: { publishedOnly?: boolean } = {}): Article | null { const { publishedOnly = true } = options const sql = `SELECT ${ARTICLE_COLS} FROM articles WHERE slug = ? ${publishedOnly ? 'AND published = 1' : ''}` const row = getDb().prepare(sql).get(slug) as Record | undefined return row ? rowToArticle(row) : null } export function listWorks(options: { publishedOnly?: boolean } = {}): Work[] { const { publishedOnly = true } = options const sql = `SELECT ${WORK_COLS} FROM works ${publishedOnly ? 'WHERE published = 1' : ''} ORDER BY id ASC` const rows = getDb().prepare(sql).all() as Array> return rows.map(rowToWork) } export function getWorkBySlug(slug: string, options: { publishedOnly?: boolean } = {}): Work | null { const { publishedOnly = true } = options const sql = `SELECT ${WORK_COLS} FROM works WHERE slug = ? ${publishedOnly ? 'AND published = 1' : ''}` const row = getDb().prepare(sql).get(slug) as Record | undefined return row ? rowToWork(row) : null } /** 后台统计 */ export function getContentStats(): { articles: number works: number publishedArticles: number publishedWorks: number } { const db = getDb() const one = (sql: string) => (db.prepare(sql).get() as { c: number }).c return { articles: one('SELECT COUNT(*) AS c FROM articles'), works: one('SELECT COUNT(*) AS c FROM works'), publishedArticles: one('SELECT COUNT(*) AS c FROM articles WHERE published = 1'), publishedWorks: one('SELECT COUNT(*) AS c FROM works WHERE published = 1'), } }