This commit is contained in:
@@ -28,6 +28,8 @@ export async function updateArticle(
|
||||
article: { ...article, content: JSON.parse(article.content) },
|
||||
articleId,
|
||||
});
|
||||
revalidatePath("/");
|
||||
|
||||
// if (!result[0]?.id?.length) return false;
|
||||
// return revalidatePath(`/artikel/${result[0]?.slug}/edit`);
|
||||
}
|
||||
@@ -35,5 +37,7 @@ export async function deleteArticle(articleId: string) {
|
||||
const result = await api.article.delete({
|
||||
articleId,
|
||||
});
|
||||
revalidatePath("/");
|
||||
|
||||
// if (!result[0]?.id?.length) return false;
|
||||
}
|
||||
|
||||
@@ -6,16 +6,25 @@ import { hasPermission, Role } from "@/lib/validation/permissions";
|
||||
|
||||
export async function uploadFile(formData: FormData) {
|
||||
const session = await auth();
|
||||
if (!session || hasPermission(session.user.role, Role.EDITOR)) return false;
|
||||
|
||||
if (!session || !hasPermission(session.user.role, Role.EDITOR)) return false;
|
||||
console.log("Starting upload");
|
||||
|
||||
const file = formData.get("file") as File;
|
||||
const arrayBuffer = await file.arrayBuffer();
|
||||
const buffer = new Uint8Array(arrayBuffer);
|
||||
const filename = `upload-${Date.now()}-${file.name}.png`;
|
||||
console.log("Filename", filename);
|
||||
|
||||
try {
|
||||
await fs.writeFile(`./public/uploads/${filename}`, buffer);
|
||||
console.log("File uploaded successfully");
|
||||
|
||||
revalidatePath("/");
|
||||
|
||||
return `/uploads/${filename}`;
|
||||
} catch (e) {
|
||||
console.error("Error uploading file:", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
import { desc, eq } from "drizzle-orm";
|
||||
import { createTRPCRouter, publicProcedure } from "../trpc";
|
||||
import { articles as articlesTable } from "@/server/db/schema";
|
||||
|
||||
export const appRouter = createTRPCRouter({
|
||||
getSidebarMain: publicProcedure.query(async ({ ctx }) => {
|
||||
@@ -11,6 +13,8 @@ export const appRouter = createTRPCRouter({
|
||||
});
|
||||
const articles = await ctx.db.query.articles.findMany({
|
||||
limit: 3,
|
||||
where: eq(articlesTable.published, true),
|
||||
orderBy: desc(articlesTable.createdAt),
|
||||
columns: {
|
||||
slug: true,
|
||||
title: true,
|
||||
|
||||
@@ -71,8 +71,11 @@ export const articleRouter = createTRPCRouter({
|
||||
get: publicProcedure
|
||||
.input(z.object({ slug: z.string() }))
|
||||
.query(async ({ ctx, input }) => {
|
||||
const user = ctx?.session?.user;
|
||||
const isEditor = user ? hasPermission(user.role, Role.EDITOR) : false;
|
||||
const publishedArg = !isEditor ? eq(articles.published, true) : undefined;
|
||||
return (await ctx.db.query.articles.findFirst({
|
||||
where: eq(articles.slug, input.slug),
|
||||
where: and(eq(articles.slug, input.slug), publishedArg),
|
||||
with: { category: true },
|
||||
})) as Article;
|
||||
}),
|
||||
@@ -112,20 +115,19 @@ export const articleRouter = createTRPCRouter({
|
||||
sortConfig,
|
||||
cursorObj,
|
||||
);
|
||||
const user = ctx?.session?.user;
|
||||
const isEditor = user ? hasPermission(user.role, Role.EDITOR) : false;
|
||||
const publishedArg = !isEditor ? eq(articles.published, true) : undefined;
|
||||
|
||||
const items = await ctx.db.query.articles.findMany({
|
||||
where: and(
|
||||
cursorArg,
|
||||
categoryArg,
|
||||
queryFilterArg,
|
||||
eq(articles.published, true),
|
||||
),
|
||||
where: and(cursorArg, categoryArg, queryFilterArg, publishedArg),
|
||||
limit: limit + 1,
|
||||
orderBy,
|
||||
columns: {
|
||||
title: true,
|
||||
slug: true,
|
||||
createdAt: true,
|
||||
published: isEditor ? true : false,
|
||||
},
|
||||
});
|
||||
|
||||
|
||||
@@ -89,6 +89,7 @@ const timingMiddleware = t.middleware(async ({ next, path }) => {
|
||||
|
||||
if (t._config.isDev) {
|
||||
// artificial delay in dev
|
||||
// const waitMs = 1000 * 5; // 5 seconds
|
||||
const waitMs = Math.floor(Math.random() * 400) + 100;
|
||||
await new Promise((resolve) => setTimeout(resolve, waitMs));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user