add blog editor
This commit is contained in:
35
src/routes/blog/[slug]/+page.server.ts
Normal file
35
src/routes/blog/[slug]/+page.server.ts
Normal file
@@ -0,0 +1,35 @@
|
||||
import { db } from '$lib/server/db';
|
||||
import { blogArticles } from '$lib/server/db/schema';
|
||||
import { isAdmin } from '$lib/server/auth';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
import type { PageServerLoad } from './$types.js';
|
||||
import { error, redirect } from '@sveltejs/kit';
|
||||
|
||||
export const load: PageServerLoad = async (event) => {
|
||||
const [article] = await db
|
||||
.select()
|
||||
.from(blogArticles)
|
||||
.where(eq(blogArticles.slug, event.params.slug));
|
||||
|
||||
if (!article) {
|
||||
return error(404, { message: 'Article not found' });
|
||||
}
|
||||
|
||||
const admin = isAdmin(event);
|
||||
|
||||
if (article.status !== 'published') {
|
||||
if (!admin) {
|
||||
return error(404, { message: 'Article not found' });
|
||||
}
|
||||
// Drafts are only ever viewed/edited through the editor.
|
||||
return redirect(303, `/blog/${article.slug}/edit`);
|
||||
}
|
||||
|
||||
return {
|
||||
blogArticle: article,
|
||||
isAdmin: admin
|
||||
};
|
||||
};
|
||||
|
||||
export const actions = {};
|
||||
Reference in New Issue
Block a user