Use Prisma type utils

This commit is contained in:
Max Leiter 2022-12-17 17:30:17 -08:00
parent 69c482a165
commit 82aadd94f2
6 changed files with 20 additions and 29 deletions

View file

@ -40,7 +40,7 @@ const Post = ({
}) => { }) => {
const session = useSession() const session = useSession()
const parsedPost = JSON.parse(stringifiedInitialPost || "{}") const parsedPost = JSON.parse(stringifiedInitialPost || "{}") as PostWithFiles
const initialPost = parsedPost?.id ? parsedPost : null const initialPost = parsedPost?.id ? parsedPost : null
const { setToast } = useToasts() const { setToast } = useToasts()
const router = useRouter() const router = useRouter()
@ -51,7 +51,7 @@ const Post = ({
const [expiresAt, setExpiresAt] = useState<Date>() const [expiresAt, setExpiresAt] = useState<Date>()
const defaultDocs: Document[] = initialPost const defaultDocs: Document[] = initialPost
? initialPost.files?.map((doc: PostWithFiles["files"][0]) => ({ ? initialPost.files?.map((doc) => ({
title: doc.title, title: doc.title,
content: doc.content, content: doc.content,
id: doc.id id: doc.id

View file

@ -69,12 +69,7 @@ const PostFiles = ({
}, [isAuthor, post.expiresAt, router]) }, [isAuthor, post.expiresAt, router])
if (isLoading) { if (isLoading) {
return ( return <DocumentComponent skeleton={true} initialTab={"preview"} />
<DocumentComponent
skeleton={true}
initialTab={"preview"}
/>
)
} }
if (isProtected) { if (isProtected) {
@ -83,7 +78,7 @@ const PostFiles = ({
return ( return (
<> <>
{post.files?.map(({ id, content, title, html }: File) => ( {post.files?.map(({ id, content, title, html }) => (
<DocumentComponent <DocumentComponent
skeleton={false} skeleton={false}
key={id} key={id}

View file

@ -60,20 +60,20 @@ export type PostWithAuthor = Prisma.PostGetPayload<typeof postWithAuthor>
export type ServerPostWithFilesAndAuthor = Prisma.PostGetPayload<typeof postWithFilesAndAuthor> export type ServerPostWithFilesAndAuthor = Prisma.PostGetPayload<typeof postWithFilesAndAuthor>
export type PostWithFiles = Omit<ServerPostWithFiles, "files"> & { export type PostWithFiles = Omit<ServerPostWithFiles, "files"> & {
files: Omit<ServerPostWithFiles["files"][number], "content" | "html"> & { files: (Omit<ServerPostWithFiles["files"][number], "content" | "html"> & {
content: string content: string
html: string html: string
}[] })[]
} }
export type PostWithFilesAndAuthor = Omit< export type PostWithFilesAndAuthor = Omit<
ServerPostWithFilesAndAuthor, ServerPostWithFilesAndAuthor,
"files" "files"
> & { > & {
files: Omit<ServerPostWithFilesAndAuthor["files"][number], "content" | "html"> & { files: (Omit<ServerPostWithFilesAndAuthor["files"][number], "content" | "html"> & {
content: string content: string
html: string html: string
}[] })[]
} }
export const getFilesForPost = async (postId: 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 ( export const searchPosts = async (
@ -267,7 +267,7 @@ export const searchPosts = async (
OR: [ OR: [
{ {
title: { title: {
search: query search: query,
}, },
authorId: userId, authorId: userId,
visibility: publicOnly ? "public" : undefined visibility: publicOnly ? "public" : undefined
@ -276,8 +276,8 @@ export const searchPosts = async (
files: { files: {
some: { some: {
content: { content: {
in: Buffer.from(query) in: [Buffer.from(query)]
} },
} }
}, },
visibility: publicOnly ? "public" : undefined visibility: publicOnly ? "public" : undefined

View file

@ -25,7 +25,7 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse<any>) {
return res.status(401).json({ error: "Unauthorized" }) return res.status(401).json({ error: "Unauthorized" })
} }
const files = req.body.files as Omit<File, 'content' | 'html'> & { content: string; html: string; }[] const files = req.body.files as (Omit<File, 'content' | 'html'> & { content: string; html: string; })[]
const fileTitles = files.map((file) => file.title) const fileTitles = files.map((file) => file.title)
const missingTitles = fileTitles.filter((title) => title === "") const missingTitles = fileTitles.filter((title) => title === "")
if (missingTitles.length > 0) { if (missingTitles.length > 0) {
@ -65,7 +65,6 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse<any>) {
authorId: session.user.id, authorId: session.user.id,
files: { files: {
create: files.map((file) => { create: files.map((file) => {
console.log(file)
return { return {
title: file.title, title: file.title,
content: Buffer.from(file.content, "utf-8"), content: Buffer.from(file.content, "utf-8"),
@ -85,7 +84,6 @@ async function handlePost(req: NextApiRequest, res: NextApiResponse<any>) {
} }
}) })
.catch((error) => { .catch((error) => {
console.log(error)
return res.status(500).json(error) return res.status(500).json(error)
}) })

View file

@ -1,6 +1,6 @@
import { withMethods } from "@lib/api-middleware/with-methods" import { withMethods } from "@lib/api-middleware/with-methods"
import { parseQueryParam } from "@lib/server/parse-query-param" 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 { NextApiRequest, NextApiResponse } from "next"
import { getSession } from "next-auth/react" import { getSession } from "next-auth/react"
@ -10,21 +10,21 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const session = await getSession() const session = await getSession()
const query = parseQueryParam(q) const query = parseQueryParam(q)
const user = parseQueryParam(userId)
if (!query) { if (!query) {
res.status(400).json({ error: "Invalid query" }) res.status(400).json({ error: "Invalid query" })
return return
} }
try { try {
let posts: PostWithFiles[] let posts: ServerPostWithFiles[]
if (session?.user.id === userId || session?.user.role === "admin") { if (session?.user.id === user || session?.user.role === "admin") {
posts = await searchPosts(query, { posts = await searchPosts(query, {
userId: parseQueryParam(userId), userId: user,
publicOnly: true
}) })
} else { } else {
posts = await searchPosts(query, { posts = await searchPosts(query, {
userId: parseQueryParam(userId), userId: user,
publicOnly: true publicOnly: true
}) })
} }

View file

@ -1,5 +1,3 @@
// api/user/[id].ts
import { parseQueryParam } from "@lib/server/parse-query-param" import { parseQueryParam } from "@lib/server/parse-query-param"
import { getUserById } from "@lib/server/prisma" import { getUserById } from "@lib/server/prisma"
import { NextApiRequest, NextApiResponse } from "next" import { NextApiRequest, NextApiResponse } from "next"