-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bdb3c03
commit d0a2574
Showing
14 changed files
with
260 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import useSWR from 'swr'; | ||
|
||
import fetcher from '@/libs/fetcher'; | ||
|
||
const usePost = (postId: string) => { | ||
const { data, error, isLoading, mutate } = useSWR(postId ? `/api/posts/${postId}` : null, fetcher); | ||
|
||
return { | ||
data, | ||
error, | ||
isLoading, | ||
mutate | ||
} | ||
}; | ||
|
||
export default usePost; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import useSWR from 'swr'; | ||
|
||
import fetcher from '@/libs/fetcher'; | ||
|
||
const usePosts = (userId?: string) => { | ||
const url = userId ? `/api/posts?userId=${userId}` : '/api/posts'; | ||
const { data, error, isLoading, mutate } = useSWR(url, fetcher); | ||
|
||
return { | ||
data, | ||
error, | ||
isLoading, | ||
mutate | ||
} | ||
}; | ||
|
||
export default usePosts; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import prisma from "@/libs/prismadb"; | ||
import serverAuth from "@/libs/serverAuth"; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method !== 'GET') { | ||
return res.status(405).end(); | ||
} | ||
|
||
try { | ||
await serverAuth(req); | ||
|
||
const { postId } = req.query; | ||
|
||
if (!postId || typeof postId !== 'string') { | ||
throw new Error('Invalid ID'); | ||
} | ||
|
||
const post = await prisma.post.findUnique({ | ||
where: { | ||
id: postId, | ||
}, | ||
include: { | ||
user: true | ||
} | ||
}); | ||
|
||
return res.status(200).json(post); | ||
} catch (error) { | ||
console.log(error); | ||
return res.status(400).end(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
|
||
import serverAuth from "@/libs/serverAuth"; | ||
import prisma from "@/libs/prismadb"; | ||
|
||
export default async function handler(req: NextApiRequest, res: NextApiResponse) { | ||
if (req.method !== 'POST' && req.method !== 'GET') { | ||
return res.status(405).end(); | ||
} | ||
|
||
try { | ||
const { currentUser } = await serverAuth(req); | ||
|
||
if (req.method === 'POST') { | ||
const { body } = req.body; | ||
|
||
const post = await prisma.post.create({ | ||
data: { | ||
body, | ||
userId: currentUser.id | ||
} | ||
}); | ||
|
||
return res.status(200).json(post); | ||
} | ||
|
||
if (req.method === 'GET') { | ||
const { userId } = req.query; | ||
|
||
console.log({ userId }) | ||
|
||
let posts; | ||
|
||
if (userId && typeof userId === 'string') { | ||
posts = await prisma.post.findMany({ | ||
where: { | ||
userId | ||
}, | ||
include: { | ||
user: true | ||
}, | ||
orderBy: { | ||
createdAt: 'desc' | ||
} | ||
}); | ||
} else { | ||
posts = await prisma.post.findMany({ | ||
include: { | ||
user: true | ||
}, | ||
orderBy: { | ||
createdAt: 'desc' | ||
} | ||
}); | ||
} | ||
|
||
return res.status(200).json(posts); | ||
} | ||
} catch (error) { | ||
console.log(error); | ||
return res.status(400).end(); | ||
} | ||
} |
Oops, something went wrong.