From 82aadd94f21645523ca06c6886b6cc01262d4363 Mon Sep 17 00:00:00 2001 From: Max Leiter Date: Sat, 17 Dec 2022 17:30:17 -0800 Subject: [PATCH] Use Prisma type utils --- src/app/(posts)/new/components/new.tsx | 4 ++-- .../post/[id]/components/post-files/index.tsx | 11 +++-------- src/lib/server/prisma.ts | 16 ++++++++-------- src/pages/api/post/index.ts | 4 +--- src/pages/api/post/search.ts | 12 ++++++------ src/pages/api/user/[id].ts | 2 -- 6 files changed, 20 insertions(+), 29 deletions(-) diff --git a/src/app/(posts)/new/components/new.tsx b/src/app/(posts)/new/components/new.tsx index c795702d..34accb3e 100644 --- a/src/app/(posts)/new/components/new.tsx +++ b/src/app/(posts)/new/components/new.tsx @@ -40,7 +40,7 @@ const Post = ({ }) => { const session = useSession() - const parsedPost = JSON.parse(stringifiedInitialPost || "{}") + const parsedPost = JSON.parse(stringifiedInitialPost || "{}") as PostWithFiles const initialPost = parsedPost?.id ? parsedPost : null const { setToast } = useToasts() const router = useRouter() @@ -51,7 +51,7 @@ const Post = ({ const [expiresAt, setExpiresAt] = useState() const defaultDocs: Document[] = initialPost - ? initialPost.files?.map((doc: PostWithFiles["files"][0]) => ({ + ? initialPost.files?.map((doc) => ({ title: doc.title, content: doc.content, id: doc.id diff --git a/src/app/(posts)/post/[id]/components/post-files/index.tsx b/src/app/(posts)/post/[id]/components/post-files/index.tsx index d442017f..7a794e3c 100644 --- a/src/app/(posts)/post/[id]/components/post-files/index.tsx +++ b/src/app/(posts)/post/[id]/components/post-files/index.tsx @@ -67,14 +67,9 @@ const PostFiles = ({ } } }, [isAuthor, post.expiresAt, router]) - + if (isLoading) { - return ( - - ) + return } if (isProtected) { @@ -83,7 +78,7 @@ const PostFiles = ({ return ( <> - {post.files?.map(({ id, content, title, html }: File) => ( + {post.files?.map(({ id, content, title, html }) => ( export type ServerPostWithFilesAndAuthor = Prisma.PostGetPayload export type PostWithFiles = Omit & { - files: Omit & { + files: (Omit & { content: string html: string - }[] + })[] } export type PostWithFilesAndAuthor = Omit< ServerPostWithFilesAndAuthor, "files" > & { - files: Omit & { + files: (Omit & { content: string html: string - }[] + })[] } export const getFilesForPost = async (postId: string) => { @@ -247,7 +247,7 @@ export const getAllUsers = async () => { } }) - return users as UserWithPosts[] + return users } export const searchPosts = async ( @@ -267,7 +267,7 @@ export const searchPosts = async ( OR: [ { title: { - search: query + search: query, }, authorId: userId, visibility: publicOnly ? "public" : undefined @@ -276,8 +276,8 @@ export const searchPosts = async ( files: { some: { content: { - in: Buffer.from(query) - } + in: [Buffer.from(query)] + }, } }, visibility: publicOnly ? "public" : undefined diff --git a/src/pages/api/post/index.ts b/src/pages/api/post/index.ts index 799eab1b..ea774e9b 100644 --- a/src/pages/api/post/index.ts +++ b/src/pages/api/post/index.ts @@ -25,7 +25,7 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) { return res.status(401).json({ error: "Unauthorized" }) } - const files = req.body.files as Omit & { content: string; html: string; }[] + const files = req.body.files as (Omit & { content: string; html: string; })[] const fileTitles = files.map((file) => file.title) const missingTitles = fileTitles.filter((title) => title === "") if (missingTitles.length > 0) { @@ -65,7 +65,6 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) { authorId: session.user.id, files: { create: files.map((file) => { - console.log(file) return { title: file.title, content: Buffer.from(file.content, "utf-8"), @@ -85,7 +84,6 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse) { } }) .catch((error) => { - console.log(error) return res.status(500).json(error) }) diff --git a/src/pages/api/post/search.ts b/src/pages/api/post/search.ts index 5b6229e8..86051a98 100644 --- a/src/pages/api/post/search.ts +++ b/src/pages/api/post/search.ts @@ -1,6 +1,6 @@ import { withMethods } from "@lib/api-middleware/with-methods" import { parseQueryParam } from "@lib/server/parse-query-param" -import { PostWithFiles, searchPosts } from "@lib/server/prisma" +import { searchPosts, ServerPostWithFiles } from "@lib/server/prisma" import { NextApiRequest, NextApiResponse } from "next" import { getSession } from "next-auth/react" @@ -10,21 +10,21 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => { const session = await getSession() const query = parseQueryParam(q) + const user = parseQueryParam(userId) if (!query) { res.status(400).json({ error: "Invalid query" }) return } try { - let posts: PostWithFiles[] - if (session?.user.id === userId || session?.user.role === "admin") { + let posts: ServerPostWithFiles[] + if (session?.user.id === user || session?.user.role === "admin") { posts = await searchPosts(query, { - userId: parseQueryParam(userId), - publicOnly: true + userId: user, }) } else { posts = await searchPosts(query, { - userId: parseQueryParam(userId), + userId: user, publicOnly: true }) } diff --git a/src/pages/api/user/[id].ts b/src/pages/api/user/[id].ts index ddf6b810..b4229fac 100644 --- a/src/pages/api/user/[id].ts +++ b/src/pages/api/user/[id].ts @@ -1,5 +1,3 @@ -// api/user/[id].ts - import { parseQueryParam } from "@lib/server/parse-query-param" import { getUserById } from "@lib/server/prisma" import { NextApiRequest, NextApiResponse } from "next"