This commit is contained in:
1 parent
d1abc1657d
commit
8d949f3e37
2 files changed
+39
-8
No files matched your search
@@ -12,9 +12,10 @@ export const GET: RequestHandler = async ({ platform, url }) => {
|
||||
const { results } = await db
|
||||
.prepare(
|
||||
`
|
||||
SELECT p.id, p.slug, p.title, p.published_at, p.updated_at, c.slug as category_slug, c.name as category_name
|
||||
SELECT p.id, p.slug, p.title, p.published_at, p.updated_at, c.slug as category_slug, c.name as category_name, s.slug as series_slug, s.name as series_name
|
||||
FROM posts p
|
||||
LEFT JOIN categories c ON p.category_id = c.id
|
||||
LEFT JOIN series s ON p.series_id = s.id
|
||||
ORDER BY p.published_at DESC
|
||||
LIMIT ? OFFSET ?
|
||||
`
|
||||
@@ -45,11 +46,12 @@ export const POST: RequestHandler = async ({ request, platform }) => {
|
||||
|
||||
try {
|
||||
const data = (await request.json()) as Record<string, unknown>;
|
||||
const { slug, title, content, category_slug, tags } = data as {
|
||||
const { slug, title, content, category_slug, series_slug, tags } = data as {
|
||||
slug?: string;
|
||||
title?: string;
|
||||
content?: string;
|
||||
category_slug?: string;
|
||||
series_slug?: string;
|
||||
tags?: string[];
|
||||
};
|
||||
|
||||
@@ -69,14 +71,23 @@ export const POST: RequestHandler = async ({ request, platform }) => {
|
||||
if (catResult) categoryId = catResult.id;
|
||||
}
|
||||
|
||||
let seriesId = null;
|
||||
if (series_slug) {
|
||||
const seriesResult = await db
|
||||
.prepare('SELECT id FROM series WHERE slug = ?')
|
||||
.bind(series_slug)
|
||||
.first<{ id: number }>();
|
||||
if (seriesResult) seriesId = seriesResult.id;
|
||||
}
|
||||
|
||||
const result = await db
|
||||
.prepare(
|
||||
`
|
||||
INSERT INTO posts (slug, title, content, author_id, category_id, published_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
INSERT INTO posts (slug, title, content, author_id, category_id, series_id, published_at, updated_at)
|
||||
VALUES (?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP)
|
||||
`
|
||||
)
|
||||
.bind(slug, title, content, authorId, categoryId)
|
||||
.bind(slug, title, content, authorId, categoryId, seriesId)
|
||||
.run();
|
||||
|
||||
if (result.success) {
|
||||
|
||||
@@ -11,9 +11,10 @@ export const GET: RequestHandler = async ({ params, platform }) => {
|
||||
const post = await db
|
||||
.prepare(
|
||||
`
|
||||
SELECT p.id, p.slug, p.title, p.content, p.published_at, p.updated_at, c.slug as category_slug, c.name as category_name
|
||||
SELECT p.id, p.slug, p.title, p.content, p.published_at, p.updated_at, c.slug as category_slug, c.name as category_name, s.slug as series_slug, s.name as series_name
|
||||
FROM posts p
|
||||
LEFT JOIN categories c ON p.category_id = c.id
|
||||
LEFT JOIN series s ON p.series_id = s.id
|
||||
WHERE p.slug = ?
|
||||
`
|
||||
)
|
||||
@@ -53,14 +54,21 @@ export const PUT: RequestHandler = async ({ params, request, platform }) => {
|
||||
|
||||
try {
|
||||
const data = (await request.json()) as Record<string, unknown>;
|
||||
const { title, content, category_slug, tags } = data as {
|
||||
const { title, content, category_slug, series_slug, tags } = data as {
|
||||
title?: string;
|
||||
content?: string;
|
||||
category_slug?: string;
|
||||
series_slug?: string;
|
||||
tags?: string[];
|
||||
};
|
||||
|
||||
if (!title && !content && category_slug === undefined && tags === undefined) {
|
||||
if (
|
||||
!title &&
|
||||
!content &&
|
||||
category_slug === undefined &&
|
||||
series_slug === undefined &&
|
||||
tags === undefined
|
||||
) {
|
||||
return json({ error: 'No fields to update' }, { status: 400 });
|
||||
}
|
||||
|
||||
@@ -96,6 +104,18 @@ export const PUT: RequestHandler = async ({ params, request, platform }) => {
|
||||
updates.push('category_id = ?');
|
||||
bindings.push(categoryId);
|
||||
}
|
||||
if (series_slug !== undefined) {
|
||||
let seriesId = null;
|
||||
if (series_slug) {
|
||||
const seriesResult = await db
|
||||
.prepare('SELECT id FROM series WHERE slug = ?')
|
||||
.bind(series_slug)
|
||||
.first<{ id: number }>();
|
||||
if (seriesResult) seriesId = seriesResult.id;
|
||||
}
|
||||
updates.push('series_id = ?');
|
||||
bindings.push(seriesId);
|
||||
}
|
||||
|
||||
updates.push('updated_at = CURRENT_TIMESTAMP');
|
||||
bindings.push(slug); // for WHERE clause
|
||||
|
||||
Reference in new issue
Block a user