diff --git a/app/(app)/articles/_client.tsx b/app/(app)/articles/_client.tsx
index f0110541..97476d9a 100644
--- a/app/(app)/articles/_client.tsx
+++ b/app/(app)/articles/_client.tsx
@@ -48,9 +48,7 @@ const ArticlesPage = () => {
},
);
- const { status: tagsStatus, data: tagsData } = api.tag.get.useQuery({
- take: 10,
- });
+ const { status: tagsStatus, data: tagsData } = api.tag.get.useQuery();
const { ref, inView } = useInView();
@@ -59,6 +57,7 @@ const ArticlesPage = () => {
fetchNextPage();
}
}, [inView]);
+
return (
<>
@@ -187,14 +186,14 @@ const ArticlesPage = () => {
{tagsStatus === "loading" &&
}
{tagsStatus === "success" &&
- tagsData.data.map((tag) => (
+ tagsData.data.map(({ title }) => (
- {getCamelCaseFromLower(tag.title)}
+ {getCamelCaseFromLower(title)}
))}
diff --git a/schema/tag.ts b/schema/tag.ts
deleted file mode 100644
index fe5e2142..00000000
--- a/schema/tag.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-import z from "zod";
-
-export const GetTagsSchema = z.object({
- take: z.number(),
-});
diff --git a/server/api/router/tag.ts b/server/api/router/tag.ts
index f1566122..0634806b 100644
--- a/server/api/router/tag.ts
+++ b/server/api/router/tag.ts
@@ -1,21 +1,23 @@
import { createTRPCRouter, publicProcedure } from "../trpc";
-import { GetTagsSchema } from "../../../schema/tag";
import { TRPCError } from "@trpc/server";
+import { post_tag, tag } from "@/server/db/schema";
+import { desc, eq, count } from "drizzle-orm";
export const tagRouter = createTRPCRouter({
- get: publicProcedure.input(GetTagsSchema).query(async ({ ctx, input }) => {
+ get: publicProcedure.query(async ({ ctx }) => {
try {
- const count = await ctx.prisma.tag.count({});
- const response = await ctx.prisma.tag.findMany({
- orderBy: {
- PostTag: {
- _count: "desc",
- },
- },
- take: input.take,
- });
+ const data = await ctx.db
+ .select({
+ title: tag.title,
+ count: count(tag.title),
+ })
+ .from(tag)
+ .groupBy(tag.title)
+ .leftJoin(post_tag, eq(post_tag.tagId, tag.id))
+ .limit(10)
+ .orderBy(desc(count(tag.title)));
- return { data: response, count };
+ return { data };
} catch (error) {
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
diff --git a/server/db/schema.ts b/server/db/schema.ts
index 5497291a..afe7d30d 100644
--- a/server/db/schema.ts
+++ b/server/db/schema.ts
@@ -80,7 +80,7 @@ export const tag = pgTable(
{
createdAt: timestamp("createdAt").defaultNow().notNull(),
id: bigserial("id", { mode: "number" }).primaryKey(),
- title: varchar("title", { length: 256 }),
+ title: varchar("title", { length: 256 }).notNull(),
},
(t) => {
return {};