Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor tag retrieval in ArticlesPage component to Drizzle #835

Merged
merged 2 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions app/(app)/articles/_client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -59,6 +57,7 @@ const ArticlesPage = () => {
fetchNextPage();
}
}, [inView]);

return (
<>
<div className="mx-2">
Expand Down Expand Up @@ -187,14 +186,14 @@ const ArticlesPage = () => {
<div className="flex flex-wrap gap-2">
{tagsStatus === "loading" && <PopularTagsLoading />}
{tagsStatus === "success" &&
tagsData.data.map((tag) => (
tagsData.data.map(({ title }) => (
<Link
key={tag.id}
key={title}
// only reason this is toLowerCase is to make url look nicer. Not needed for functionality
href={`/articles?tag=${tag.title.toLowerCase()}`}
href={`/articles?tag=${title.toLowerCase()}`}
className="border border-neutral-300 bg-white px-6 py-2 text-neutral-900 dark:border-neutral-600 dark:bg-neutral-900 dark:text-neutral-50"
>
{getCamelCaseFromLower(tag.title)}
{getCamelCaseFromLower(title)}
</Link>
))}
</div>
Expand Down
5 changes: 0 additions & 5 deletions schema/tag.ts

This file was deleted.

26 changes: 14 additions & 12 deletions server/api/router/tag.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
2 changes: 1 addition & 1 deletion server/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
Expand Down