create category dialog

This commit is contained in:
2025-03-08 12:48:18 +01:00
parent dd3c321350
commit 66aadfd04d
19 changed files with 535 additions and 25 deletions
+1 -2
View File
@@ -3,7 +3,6 @@
import { categorySchema } from "@/lib/validation/zod/category";
import { api } from "@/trpc/server";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { z } from "zod";
export async function createCategory(category: z.infer<typeof categorySchema>) {
@@ -11,7 +10,7 @@ export async function createCategory(category: z.infer<typeof categorySchema>) {
category,
});
if (!result[0]?.slug?.length) return false;
return redirect(`/kategorie/${result[0].slug}/edit`);
return revalidatePath("/");
}
export async function updateCategory(
category: z.infer<typeof categorySchema>,
+2
View File
@@ -2,6 +2,7 @@ import { articleRouter } from "./routers/article";
import { categoryRouter } from "@/server/api/routers/category";
import { createCallerFactory, createTRPCRouter } from "@/server/api/trpc";
import { usersRouter } from "./routers/users";
import { authorRouter } from "./routers/author";
/**
* This is the primary router for your server.
@@ -12,6 +13,7 @@ export const appRouter = createTRPCRouter({
article: articleRouter,
category: categoryRouter,
users: usersRouter,
author: authorRouter,
});
// export type definition of API
+17 -1
View File
@@ -7,11 +7,12 @@ import {
} from "@/server/api/trpc";
import { articles } from "@/server/db/schema";
import { articleSchema } from "@/lib/validation/zod/article";
import { eq } from "drizzle-orm";
import { count, eq } from "drizzle-orm";
import { hasPermission, Role } from "@/lib/validation/permissions";
import { generateSlug } from "@/lib/utils";
export const articleRouter = createTRPCRouter({
// queries
get: publicProcedure
.input(z.object({ slug: z.string() }))
.query(async ({ ctx, input }) => {
@@ -43,6 +44,21 @@ export const articleRouter = createTRPCRouter({
},
});
}),
getCount: publicProcedure
.input(z.object({ categoryId: z.string() }).optional())
.query(async ({ ctx, input }) => {
return (
await ctx.db
.select({ count: count() })
.from(articles)
.where(
input?.categoryId
? eq(articles.categoryId, input.categoryId)
: undefined,
)
)[0]?.count;
}),
// mutations
create: protectedProcedure
.input(z.object({ article: articleSchema }))
.mutation(async ({ ctx, input }) => {
+7
View File
@@ -0,0 +1,7 @@
import { createTRPCRouter, protectedProcedure, publicProcedure } from "../trpc";
export const authorRouter = createTRPCRouter({
getCount: publicProcedure.query(async ({ ctx }) => {
return 7;
}),
});
+5 -1
View File
@@ -6,7 +6,7 @@ import {
} from "@/server/api/trpc";
import { categories } from "@/server/db/schema";
import { eq } from "drizzle-orm";
import { count, eq } from "drizzle-orm";
import { hasPermission, Role } from "@/lib/validation/permissions";
import { categorySchema } from "@/lib/validation/zod/category";
import { generateSlug } from "@/lib/utils";
@@ -24,6 +24,10 @@ export const categoryRouter = createTRPCRouter({
return await ctx.db.query.categories.findMany();
}),
getCount: publicProcedure.query(async ({ ctx }) => {
return (await ctx.db.select({ count: count() }).from(categories))[0]?.count;
}),
create: protectedProcedure
.input(z.object({ category: categorySchema }))
.mutation(async ({ ctx, input }) => {