Skip to content

Commit 0d0dbfa

Browse files
committed
feat: post
1 parent ad82598 commit 0d0dbfa

File tree

11 files changed

+259
-109
lines changed

11 files changed

+259
-109
lines changed

apps/web/src/actions/auth/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export const signInWithCredentials = async (email: string, password: string) =>
1919
}
2020

2121
export const signInWithGithub = async () => {
22-
console.log("signInWithGithub...")
2322
await signIn("github")
2423
}
2524

@@ -41,7 +40,7 @@ export const signUp = async (
4140
await createUser({
4241
email,
4342
password: hashedPassword,
44-
username: email.split("@")[0],
43+
name: email.split("@")[0],
4544
})
4645

4746
// create verification code

apps/web/src/actions/protect/postAction.ts

Lines changed: 51 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ import { redirect } from "next/navigation"
55

66
import prisma, {
77
createPost,
8+
PostCreateInputParams,
89
PostOnUserType,
910
PostStatus,
10-
Session,
11-
TCreatePostInput,
11+
Prisma,
1212
TPostItem,
1313
updatePost,
14-
updatePostStatus,
1514
} from "database"
1615
import { toast } from "react-toastify"
1716
import * as z from "zod"
1817

1918
import { auth } from "@/configs/auth"
2019
import APP_ROUTES from "@/constants/routes"
21-
import { ActionState, validatedActionWithUser } from "@/libs/validationAction"
20+
import { ActionState } from "@/libs/validationAction"
2221
import { TUserItem, userSelect } from "@/types/users"
2322

2423
export const getTotalActions = async ({
@@ -149,17 +148,17 @@ export const removeRelation = async ({
149148

150149
export const getLikers = async ({ postId }: { postId: string }) => {
151150
try {
152-
const likers: TUserItem[] = await prisma.user.findMany({
151+
const likers = (await prisma.user.findMany({
153152
where: {
154-
postOnUser: {
153+
postOnUsers: {
155154
some: {
156155
postId,
157156
type: PostOnUserType.LIKE,
158157
},
159158
},
160159
},
161160
select: userSelect,
162-
})
161+
})) as unknown as TUserItem[]
163162

164163
return {
165164
data: likers,
@@ -175,15 +174,24 @@ export async function onTogglePost(
175174
_
176175
): Promise<{ post: TPostItem }> {
177176
try {
178-
const { data } = await updatePostStatus(
177+
// Implementation for post toggle status
178+
if (!prevState.post?.id || !prevState.post?.author?.id) {
179+
throw "Invalid post data"
180+
}
181+
182+
const result = await updatePost(
179183
prevState.post.id,
180-
prevState.post.postStatus === PostStatus.DRAFT ? PostStatus.PUBLISHED : PostStatus.DRAFT,
181-
prevState.post?.author?.id
184+
{
185+
postStatus:
186+
prevState.post.postStatus === PostStatus.DRAFT ? PostStatus.PUBLISHED : PostStatus.DRAFT,
187+
},
188+
prevState.post.author.id
182189
)
183190

184-
return { post: data }
191+
return { post: result.data }
185192
} catch (error) {
186193
toast.error(error)
194+
return { post: prevState.post }
187195
} finally {
188196
revalidatePath(`/post/${prevState.post.slug}`)
189197
}
@@ -195,15 +203,44 @@ export const handleCreateUpdatePost = async ({
195203
userId,
196204
}: {
197205
postId: string
198-
data: TCreatePostInput
206+
data: PostCreateInputParams
199207
userId: string
200208
}) => {
201209
let newPostId = postId
202210
try {
203211
if (postId) {
204-
await updatePost(postId, data, userId)
212+
// For updates, transform the data to PostUpdateInput manually
213+
const updateData = {
214+
title: data.title,
215+
content: data.content,
216+
isPinned: data.isPinned,
217+
isLocked: data.isLocked,
218+
postStatus: data.postStatus,
219+
postType: data.postType,
220+
} as Prisma.PostUpdateInput
221+
222+
// Handle category connection if provided
223+
if (data.categoryId) {
224+
updateData.category = {
225+
connect: { id: data.categoryId },
226+
}
227+
}
228+
229+
// Handle media connection if provided
230+
if (data.mediaId) {
231+
updateData.media = {
232+
connect: { id: data.mediaId },
233+
}
234+
}
235+
236+
await updatePost(postId, updateData, userId)
205237
} else {
206-
const post = await createPost(data, userId)
238+
// For creation, directly use createPost with our params
239+
// It will handle the transformation internally
240+
const post = await createPost({
241+
...data,
242+
authorId: userId,
243+
})
207244
newPostId = post?.data?.slug
208245
}
209246
} catch (error) {
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { Metadata } from "next"
22

3-
import { getPost } from "database"
3+
import { findPostBySlugOrId } from "database"
44

55
import PostForm from "@/molecules/post-form"
66

77
export async function generateMetadata(props): Promise<Metadata> {
88
const params = await props.params
9-
const post = await getPost({ postIdOrSlug: params?.postId as string })
9+
const post = await findPostBySlugOrId(params?.postId)
1010

1111
return {
1212
title: post?.data?.title,
@@ -16,7 +16,7 @@ export async function generateMetadata(props): Promise<Metadata> {
1616

1717
export default async function Page(props: { params: Promise<{ postId: string }> }) {
1818
const params = await props.params
19-
const post = await getPost({ postIdOrSlug: params?.postId as string })
19+
const post = await findPostBySlugOrId(params?.postId)
2020

2121
return <PostForm post={post?.data} />
2222
}

apps/web/src/app/[lang]/(public-fullwidth)/posts/[postId]/page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ import BookmarkButton from "@/molecules/posts/post-item/bookmark-button"
99

1010
import "./tocbot.css"
1111

12-
import { getPost, PostStatus } from "database"
12+
import { findPostBySlugOrId, PostStatus } from "database"
1313

1414
import { auth } from "@/configs/auth"
1515
import { TSearchParams } from "@/types"
1616

1717
export async function generateMetadata(props): Promise<Metadata> {
1818
const params = await props.params
19-
const post = await getPost({ postIdOrSlug: params?.postId })
19+
const post = await findPostBySlugOrId(params?.postId)
2020

2121
return {
2222
title: post?.data?.title,
@@ -30,7 +30,7 @@ export default async function Page(props: {
3030
}) {
3131
const searchParams = await props.searchParams
3232
const params = await props.params
33-
const post = await getPost({ postIdOrSlug: params?.postId })
33+
const post = await findPostBySlugOrId(params?.postId)
3434
const session = await auth()
3535

3636
if (

apps/web/src/app/api/public/post/[postIdOrSlug]/route.ts

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,23 @@
11
import { NextRequest } from "next/server"
22

3-
import prisma from "database"
3+
import prisma, { findPostBySlugOrId } from "database"
44

55
export async function GET(
66
request: NextRequest,
77
props: { params: Promise<{ postIdOrSlug: string }> }
88
) {
99
const params = await props.params
1010
try {
11-
const post = await prisma.post.findUnique({
12-
where: {
13-
// postStatus: PostStatus.PUBLISHED,
14-
// id: params.postIdOrSlug,
15-
slug: params.postIdOrSlug,
16-
// OR: [
17-
// {
18-
// id: params.postIdOrSlug,
19-
// },
20-
// {
21-
// slug: params.postIdOrSlug,
22-
// },
23-
// ],
24-
},
25-
// select: postSelect,
26-
})
11+
const result = await findPostBySlugOrId(params.postIdOrSlug)
2712

28-
if (!post)
13+
if (!result || !result.data) {
2914
return Response.json({
3015
status: 404,
3116
message: "Post not found",
3217
})
18+
}
3319

34-
return Response.json(post, { status: 200 })
20+
return Response.json(result.data, { status: 200 })
3521
} catch (error) {
3622
return Response.error()
3723
}

0 commit comments

Comments
 (0)