This commit is contained in:
1 parent
831efac051
commit
f40b49c654
7 files changed
+13
-37
No files matched your search
@@ -23,10 +23,10 @@ export const load: PageServerLoad = async ({ platform }) => {
|
|||||||
try {
|
try {
|
||||||
const { results } = await db
|
const { results } = await db
|
||||||
.prepare(
|
.prepare(
|
||||||
`SELECT s.id, s.name, s.slug, s.description, COUNT(p.id) AS post_count
|
`SELECT s.id, s.name, s.slug, COUNT(p.id) AS post_count
|
||||||
FROM series s
|
FROM series s
|
||||||
LEFT JOIN posts p ON p.series_id = s.id
|
LEFT JOIN posts p ON p.series_id = s.id
|
||||||
GROUP BY s.id, s.name, s.slug, s.description
|
GROUP BY s.id, s.name, s.slug
|
||||||
ORDER BY s.name COLLATE NOCASE ASC`
|
ORDER BY s.name COLLATE NOCASE ASC`
|
||||||
)
|
)
|
||||||
.all();
|
.all();
|
||||||
@@ -36,7 +36,6 @@ export const load: PageServerLoad = async ({ platform }) => {
|
|||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
slug: string;
|
slug: string;
|
||||||
description: string | null;
|
|
||||||
post_count: number;
|
post_count: number;
|
||||||
}>
|
}>
|
||||||
};
|
};
|
||||||
@@ -54,16 +53,13 @@ export const actions: Actions = {
|
|||||||
const formData = await request.formData();
|
const formData = await request.formData();
|
||||||
const name = formData.get('name')?.toString().trim() || '';
|
const name = formData.get('name')?.toString().trim() || '';
|
||||||
const slug = makeSlug(formData.get('slug')?.toString() || name);
|
const slug = makeSlug(formData.get('slug')?.toString() || name);
|
||||||
const description = formData.get('description')?.toString().trim() || null;
|
|
||||||
|
|
||||||
if (!name || !slug) return fail(400, { error: '系列名称和 URL 别名不能为空' });
|
if (!name || !slug) return fail(400, { error: '系列名称和 URL 别名不能为空' });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const created = await db
|
const created = await db
|
||||||
.prepare(
|
.prepare('INSERT INTO series (name, slug) VALUES (?, ?) RETURNING id, name, slug')
|
||||||
'INSERT INTO series (name, slug, description) VALUES (?, ?, ?) RETURNING id, name, slug, description'
|
.bind(name, slug)
|
||||||
)
|
|
||||||
.bind(name, slug, description)
|
|
||||||
.first();
|
.first();
|
||||||
return { success: true, series: created };
|
return { success: true, series: created };
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
@@ -83,7 +79,6 @@ export const actions: Actions = {
|
|||||||
const id = Number(formData.get('id'));
|
const id = Number(formData.get('id'));
|
||||||
const name = formData.get('name')?.toString().trim() || '';
|
const name = formData.get('name')?.toString().trim() || '';
|
||||||
const slug = makeSlug(formData.get('slug')?.toString() || '');
|
const slug = makeSlug(formData.get('slug')?.toString() || '');
|
||||||
const description = formData.get('description')?.toString().trim() || null;
|
|
||||||
|
|
||||||
if (!Number.isInteger(id) || id <= 0 || !name || !slug) {
|
if (!Number.isInteger(id) || id <= 0 || !name || !slug) {
|
||||||
return fail(400, { error: '系列信息不完整' });
|
return fail(400, { error: '系列信息不完整' });
|
||||||
@@ -91,8 +86,8 @@ export const actions: Actions = {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await db
|
await db
|
||||||
.prepare('UPDATE series SET name = ?, slug = ?, description = ? WHERE id = ?')
|
.prepare('UPDATE series SET name = ?, slug = ? WHERE id = ?')
|
||||||
.bind(name, slug, description, id)
|
.bind(name, slug, id)
|
||||||
.run();
|
.run();
|
||||||
return { success: true };
|
return { success: true };
|
||||||
} catch (err: unknown) {
|
} catch (err: unknown) {
|
||||||
|
|||||||
@@ -68,14 +68,6 @@
|
|||||||
oninput={handleSlugInput}
|
oninput={handleSlugInput}
|
||||||
required
|
required
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<label for="description">描述</label>
|
|
||||||
<textarea
|
|
||||||
id="description"
|
|
||||||
name="description"
|
|
||||||
rows="5"
|
|
||||||
placeholder="介绍这个系列的主题和阅读顺序"></textarea>
|
|
||||||
|
|
||||||
<button type="submit" class="btn btn-primary">创建</button>
|
<button type="submit" class="btn btn-primary">创建</button>
|
||||||
</form>
|
</form>
|
||||||
</section>
|
</section>
|
||||||
@@ -102,12 +94,6 @@
|
|||||||
<input id="slug-{item.id}" name="slug" type="text" value={item.slug} required />
|
<input id="slug-{item.id}" name="slug" type="text" value={item.slug} required />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<label for="description-{item.id}">描述</label>
|
|
||||||
<textarea
|
|
||||||
id="description-{item.id}"
|
|
||||||
name="description"
|
|
||||||
rows="3"
|
|
||||||
value={item.description || ''}></textarea>
|
|
||||||
<button type="submit" class="action-btn">保存修改</button>
|
<button type="submit" class="action-btn">保存修改</button>
|
||||||
<a
|
<a
|
||||||
href={resolve('/series/[slug]', { slug: item.slug })}
|
href={resolve('/series/[slug]', { slug: item.slug })}
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ interface PostRow {
|
|||||||
series_id: number | null;
|
series_id: number | null;
|
||||||
series_name: string | null;
|
series_name: string | null;
|
||||||
series_slug: string | null;
|
series_slug: string | null;
|
||||||
series_description: string | null;
|
|
||||||
series_weight: number | null;
|
series_weight: number | null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -72,7 +71,7 @@ export const load: PageServerLoad = async ({ platform, params }) => {
|
|||||||
c.name as category_name, c.slug as category_slug,
|
c.name as category_name, c.slug as category_slug,
|
||||||
u.username as author,
|
u.username as author,
|
||||||
s.id as series_id, s.name as series_name, s.slug as series_slug,
|
s.id as series_id, s.name as series_name, s.slug as series_slug,
|
||||||
s.description as series_description, p.series_weight
|
p.series_weight
|
||||||
FROM posts p
|
FROM posts p
|
||||||
LEFT JOIN categories c ON p.category_id = c.id
|
LEFT JOIN categories c ON p.category_id = c.id
|
||||||
LEFT JOIN users u ON p.author_id = u.id
|
LEFT JOIN users u ON p.author_id = u.id
|
||||||
@@ -100,7 +99,6 @@ export const load: PageServerLoad = async ({ platform, params }) => {
|
|||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
slug: string;
|
slug: string;
|
||||||
description: string | null;
|
|
||||||
} | null = null;
|
} | null = null;
|
||||||
let seriesPosts: Array<{
|
let seriesPosts: Array<{
|
||||||
id: number;
|
id: number;
|
||||||
@@ -113,8 +111,7 @@ export const load: PageServerLoad = async ({ platform, params }) => {
|
|||||||
series = {
|
series = {
|
||||||
id: post.series_id,
|
id: post.series_id,
|
||||||
name: post.series_name,
|
name: post.series_name,
|
||||||
slug: post.series_slug,
|
slug: post.series_slug
|
||||||
description: post.series_description
|
|
||||||
};
|
};
|
||||||
const seriesQuery = await db
|
const seriesQuery = await db
|
||||||
.prepare(
|
.prepare(
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ export const load: PageServerLoad = async ({ platform }) => {
|
|||||||
try {
|
try {
|
||||||
const { results } = await db
|
const { results } = await db
|
||||||
.prepare(
|
.prepare(
|
||||||
`SELECT s.id, s.name, s.slug, s.description, COUNT(p.id) AS post_count
|
`SELECT s.id, s.name, s.slug, COUNT(p.id) AS post_count
|
||||||
FROM series s
|
FROM series s
|
||||||
LEFT JOIN posts p ON p.series_id = s.id
|
LEFT JOIN posts p ON p.series_id = s.id
|
||||||
GROUP BY s.id, s.name, s.slug, s.description
|
GROUP BY s.id, s.name, s.slug
|
||||||
ORDER BY s.name COLLATE NOCASE ASC`
|
ORDER BY s.name COLLATE NOCASE ASC`
|
||||||
)
|
)
|
||||||
.all();
|
.all();
|
||||||
@@ -21,7 +21,6 @@ export const load: PageServerLoad = async ({ platform }) => {
|
|||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
slug: string;
|
slug: string;
|
||||||
description: string | null;
|
|
||||||
post_count: number;
|
post_count: number;
|
||||||
}>
|
}>
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -22,7 +22,6 @@
|
|||||||
{#each data.series as item (item.id)}
|
{#each data.series as item (item.id)}
|
||||||
<a href={resolve('/series/[slug]', { slug: item.slug })} class="series-card">
|
<a href={resolve('/series/[slug]', { slug: item.slug })} class="series-card">
|
||||||
<h2>{item.name}</h2>
|
<h2>{item.name}</h2>
|
||||||
{#if item.description}<p>{item.description}</p>{/if}
|
|
||||||
<span>{item.post_count} 篇文章</span>
|
<span>{item.post_count} 篇文章</span>
|
||||||
</a>
|
</a>
|
||||||
{:else}
|
{:else}
|
||||||
|
|||||||
@@ -6,9 +6,9 @@ export const load: PageServerLoad = async ({ platform, params }) => {
|
|||||||
if (!db) throw error(500, '数据库未连接');
|
if (!db) throw error(500, '数据库未连接');
|
||||||
try {
|
try {
|
||||||
const series = await db
|
const series = await db
|
||||||
.prepare('SELECT id, name, slug, description FROM series WHERE slug = ?')
|
.prepare('SELECT id, name, slug FROM series WHERE slug = ?')
|
||||||
.bind(params.slug)
|
.bind(params.slug)
|
||||||
.first<{ id: number; name: string; slug: string; description: string | null }>();
|
.first<{ id: number; name: string; slug: string }>();
|
||||||
if (!series) throw error(404, '系列不存在');
|
if (!series) throw error(404, '系列不存在');
|
||||||
const { results } = await db
|
const { results } = await db
|
||||||
.prepare(
|
.prepare(
|
||||||
|
|||||||
@@ -18,7 +18,7 @@
|
|||||||
<article class="article-detail yue">
|
<article class="article-detail yue">
|
||||||
<div class="banner-title">
|
<div class="banner-title">
|
||||||
<h1 class="post-title">{data.series.name}</h1>
|
<h1 class="post-title">{data.series.name}</h1>
|
||||||
{#if data.series.description}<p class="post-meta">{data.series.description}</p>{/if}
|
<span class="post-meta">{data.posts.length} 篇文章</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<ol class="series-posts">
|
<ol class="series-posts">
|
||||||
|
|||||||
Reference in new issue
Block a user