Skip to content

Commit dd0c73d

Browse files
committed
fix: patch views count error
1 parent 97354d6 commit dd0c73d

File tree

4 files changed

+55
-32
lines changed

4 files changed

+55
-32
lines changed

app/api/patch/get.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,6 @@ export const getPatchById = async (
4141
return '未找到对应补丁'
4242
}
4343

44-
await prisma.patch.update({
45-
where: { id: patch.id },
46-
data: { view: { increment: 1 } }
47-
})
48-
4944
const response: Patch = {
5045
id: patch.id,
5146
vndbId: patch.vndb_id,

app/api/patch/views/put.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { prisma } from '~/prisma/index'
2+
3+
export const updatePatchViews = async (patchId: number) => {
4+
await prisma.patch.update({
5+
where: { id: patchId },
6+
data: { view: { increment: 1 } }
7+
})
8+
}

app/patch/[id]/actions.ts

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,62 @@
11
'use server'
22

3+
import { cache } from 'react'
34
import { z } from 'zod'
45
import { verifyHeaderCookie } from '~/utils/actions/verifyHeaderCookie'
56
import { safeParseSchema } from '~/utils/actions/safeParseSchema'
67
import { getPatchById } from '~/app/api/patch/get'
78
import { getPatchIntroduction } from '~/app/api/patch/introduction/route'
89
import { getPatchContributor } from '~/app/api/patch/contributor/route'
10+
import { updatePatchViews } from '~/app/api/patch/views/put'
911

1012
const patchIdSchema = z.object({
1113
patchId: z.coerce.number().min(1).max(9999999)
1214
})
1315

14-
export const kunGetPatchActions = async (
15-
params: z.infer<typeof patchIdSchema>
16-
) => {
17-
const input = safeParseSchema(patchIdSchema, params)
18-
if (typeof input === 'string') {
19-
return input
16+
export const kunGetPatchActions = cache(
17+
async (params: z.infer<typeof patchIdSchema>) => {
18+
const input = safeParseSchema(patchIdSchema, params)
19+
if (typeof input === 'string') {
20+
return input
21+
}
22+
const payload = await verifyHeaderCookie()
23+
24+
const response = await getPatchById(input, payload?.uid ?? 0)
25+
return response
2026
}
21-
const payload = await verifyHeaderCookie()
27+
)
2228

23-
const response = await getPatchById(input, payload?.uid ?? 0)
24-
return response
25-
}
29+
export const kunGetPatchIntroductionActions = cache(
30+
async (params: z.infer<typeof patchIdSchema>) => {
31+
const input = safeParseSchema(patchIdSchema, params)
32+
if (typeof input === 'string') {
33+
return input
34+
}
2635

27-
export const kunGetPatchIntroductionActions = async (
28-
params: z.infer<typeof patchIdSchema>
29-
) => {
30-
const input = safeParseSchema(patchIdSchema, params)
31-
if (typeof input === 'string') {
32-
return input
36+
const response = await getPatchIntroduction(input)
37+
return response
3338
}
39+
)
3440

35-
const response = await getPatchIntroduction(input)
36-
return response
37-
}
41+
export const kunGetContributorActions = cache(
42+
async (params: z.infer<typeof patchIdSchema>) => {
43+
const input = safeParseSchema(patchIdSchema, params)
44+
if (typeof input === 'string') {
45+
return input
46+
}
47+
48+
const response = await getPatchContributor(input)
49+
return response
50+
}
51+
)
3852

39-
export const kunGetContributorActions = async (
53+
export const kunUpdatePatchViewsActions = async (
4054
params: z.infer<typeof patchIdSchema>
4155
) => {
4256
const input = safeParseSchema(patchIdSchema, params)
4357
if (typeof input === 'string') {
4458
return input
4559
}
4660

47-
const response = await getPatchContributor(input)
48-
return response
61+
await updatePatchViews(input.patchId)
4962
}

app/patch/[id]/layout.tsx

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { ErrorComponent } from '~/components/error/ErrorComponent'
2-
import { kunGetPatchActions, kunGetPatchIntroductionActions } from './actions'
2+
import {
3+
kunGetPatchActions,
4+
kunGetPatchIntroductionActions,
5+
kunUpdatePatchViewsActions
6+
} from './actions'
37
import { generateKunMetadataTemplate } from './metadata'
48
import { PatchContainer } from '~/components/patch/Container'
59
import { verifyHeaderCookie } from '~/utils/actions/verifyHeaderCookie'
@@ -30,14 +34,17 @@ export default async function Kun({ params, children }: Props) {
3034
return <ErrorComponent error={'提取页面参数错误'} />
3135
}
3236

33-
const patch = await kunGetPatchActions({
34-
patchId: Number(id)
35-
})
37+
const [patch, intro] = await Promise.all([
38+
kunGetPatchActions({
39+
patchId: Number(id)
40+
}),
41+
kunGetPatchIntroductionActions({ patchId: Number(id) }),
42+
kunUpdatePatchViewsActions({ patchId: Number(id) })
43+
])
3644
if (typeof patch === 'string') {
3745
return <ErrorComponent error={patch} />
3846
}
3947

40-
const intro = await kunGetPatchIntroductionActions({ patchId: Number(id) })
4148
if (typeof intro === 'string') {
4249
return <ErrorComponent error={intro} />
4350
}

0 commit comments

Comments
 (0)