export const postRouter = createTRPCRouter({
create: privateProcedure
.input(z.object({ content: z.string().min(1).max(255) }))
.mutation(async ({ ctx, input }) => {
const authorId = ctx.user.id;
const { success } = await rateLimit.limit(authorId);
if (!success) throw new TRPCError({ code: "TOO_MANY_REQUESTS" });
const post = await ctx.db.post.create({
data: { authorId, content: input.content },
select: { authorId: true, content: true, id: true },
});
return post;
}),
getAll: publicProcedure.query(async ({ ctx }) => {
const posts = await ctx.db.post.findMany({
take: 100,
orderBy: { createdAt: "desc" },
});
const users = (
await clerkClient.users.getUserList({
userId: posts.map((post) => post.authorId),
limit: 100,
})
).map(filterUserForClient);
return posts.map((post) => {
const author = users.find((user) => user.id === post.authorId);
if (!author)
throw new TRPCError({
code: "INTERNAL_SERVER_ERROR",
message: "Author not found",
});
return {
post,
author,
};
});
}),
});