Skip to content

Commit d0698ee

Browse files
committed
feat: character list page
1 parent c5a336f commit d0698ee

File tree

24 files changed

+244
-328
lines changed

24 files changed

+244
-328
lines changed

app/api/character/all/route.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ import { NextRequest, NextResponse } from 'next/server'
33
import { prisma } from '~/prisma/index'
44
import { getCharSchema } from '~/validations/char'
55
import { kunParseGetQuery } from '~/app/api/utils/parseQuery'
6+
import type { PatchCharacter } from '~/types/api/character'
67

78
export const getChar = async (input: z.infer<typeof getCharSchema>) => {
89
const { page, limit } = input
910
const offset = (page - 1) * limit
10-
const [chars, total] = await Promise.all([
11+
const [data, total] = await Promise.all([
1112
prisma.patch_char.findMany({
1213
take: limit,
1314
skip: offset,
@@ -25,12 +26,29 @@ export const getChar = async (input: z.infer<typeof getCharSchema>) => {
2526
}),
2627
prisma.patch_char.count()
2728
])
29+
30+
const chars: PatchCharacter[] = data.map((c) => ({
31+
id: c.id,
32+
image: c.image,
33+
gender: c.gender,
34+
role: c.role,
35+
roles: c.roles,
36+
name: {
37+
'zh-cn': c.name_zh_cn,
38+
'ja-jp': c.name_ja_jp,
39+
'en-us': c.name_en_us
40+
}
41+
}))
42+
2843
return { chars, total }
2944
}
3045

3146
export const GET = async (req: NextRequest) => {
3247
const input = kunParseGetQuery(req, getCharSchema)
33-
if (typeof input === 'string') return NextResponse.json(input)
48+
if (typeof input === 'string') {
49+
return NextResponse.json(input)
50+
}
51+
3452
const response = await getChar(input)
3553
return NextResponse.json(response)
3654
}

app/api/character/route.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server'
22
import { z } from 'zod'
33
import { prisma } from '~/prisma'
44
import { kunParseGetQuery } from '../utils/parseQuery'
5+
import type { PatchCharacterDetail } from '~/types/api/character'
56

67
const characterIdSchema = z.object({ characterId: z.coerce.number().min(1) })
78

@@ -12,7 +13,9 @@ export const getCharacterById = async (
1213
const char = await prisma.patch_char.findUnique({
1314
where: { id: characterId }
1415
})
15-
if (!char) return '未找到角色'
16+
if (!char) {
17+
return '未找到角色'
18+
}
1619

1720
const aliases = await prisma.patch_char_alias
1821
.findMany({ where: { patch_char_id: characterId }, select: { name: true } })
@@ -24,7 +27,9 @@ export const getCharacterById = async (
2427
})
2528
const patches = patchRelations.map((pr) => ({
2629
id: pr.patch.id,
27-
name: pr.patch.name,
30+
name_zh_cn: pr.patch.name_zh_cn,
31+
name_ja_jp: pr.patch.name_ja_jp,
32+
name_en_us: pr.patch.name_en_us,
2833
banner: pr.patch.banner
2934
}))
3035

@@ -34,12 +39,16 @@ export const getCharacterById = async (
3439
gender: char.gender,
3540
role: char.role,
3641
roles: char.roles,
37-
name_zh_cn: char.name_zh_cn,
38-
name_ja_jp: char.name_ja_jp,
39-
name_en_us: char.name_en_us,
40-
description_zh_cn: char.description_zh_cn,
41-
description_ja_jp: char.description_ja_jp,
42-
description_en_us: char.description_en_us,
42+
name: {
43+
'zh-cn': char.name_zh_cn,
44+
'ja-jp': char.name_ja_jp,
45+
'en-us': char.name_en_us
46+
},
47+
description: {
48+
'zh-cn': char.description_zh_cn,
49+
'ja-jp': char.description_ja_jp,
50+
'en-us': char.description_en_us
51+
},
4352
birthday: char.birthday,
4453
height: char.height,
4554
weight: char.weight,
@@ -50,15 +59,24 @@ export const getCharacterById = async (
5059
age: char.age,
5160
infobox: char.infobox,
5261
alias: aliases,
53-
patches
54-
}
62+
patches: patches.map((p) => ({
63+
id: p.id,
64+
name: {
65+
'zh-cn': p.name_zh_cn,
66+
'ja-jp': p.name_ja_jp,
67+
'en-us': p.name_en_us
68+
},
69+
banner: p.banner
70+
}))
71+
} satisfies PatchCharacterDetail
5572
}
5673

5774
export const GET = async (req: NextRequest) => {
5875
const input = kunParseGetQuery(req, characterIdSchema)
5976
if (typeof input === 'string') {
6077
return NextResponse.json(input)
6178
}
79+
6280
const res = await getCharacterById(input)
6381
return NextResponse.json(res)
6482
}

app/api/character/search/route.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { NextRequest, NextResponse } from 'next/server'
33
import { prisma } from '~/prisma/index'
44
import { searchCharSchema } from '~/validations/char'
55
import { kunParsePostBody } from '~/app/api/utils/parseQuery'
6+
import type { PatchCharacter } from '~/types/api/character'
67

78
export const searchChar = async (input: z.infer<typeof searchCharSchema>) => {
89
const { query } = input
9-
const chars = await prisma.patch_char.findMany({
10+
const data = await prisma.patch_char.findMany({
1011
where: {
1112
OR: query.map((term) => ({
1213
OR: [
@@ -29,6 +30,20 @@ export const searchChar = async (input: z.infer<typeof searchCharSchema>) => {
2930
name_en_us: true
3031
}
3132
})
33+
34+
const chars: PatchCharacter[] = data.map((c) => ({
35+
id: c.id,
36+
image: c.image,
37+
gender: c.gender,
38+
role: c.role,
39+
roles: c.roles,
40+
name: {
41+
'zh-cn': c.name_zh_cn,
42+
'ja-jp': c.name_ja_jp,
43+
'en-us': c.name_en_us
44+
}
45+
}))
46+
3247
return chars
3348
}
3449

app/api/patch/detail/route.ts

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,11 +127,6 @@ export const getPatchDetail = async (
127127
'ja-jp': c.name_ja_jp,
128128
'en-us': c.name_en_us
129129
},
130-
description: {
131-
'zh-cn': c.description_zh_cn,
132-
'ja-jp': c.description_ja_jp,
133-
'en-us': c.description_en_us
134-
},
135130
infobox: c.infobox
136131
})),
137132
person: patch.person_rel
@@ -144,11 +139,6 @@ export const getPatchDetail = async (
144139
'zh-cn': p.name_zh_cn,
145140
'ja-jp': p.name_ja_jp,
146141
'en-us': p.name_en_us
147-
},
148-
description: {
149-
'zh-cn': p.description_zh_cn,
150-
'ja-jp': p.description_ja_jp,
151-
'en-us': p.description_en_us
152142
}
153143
})),
154144
created: String(patch.created),

app/api/patch/get.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,11 @@ export const getPatchById = async (
5050
'ja-jp': patch.name_ja_jp,
5151
'en-us': patch.name_en_us
5252
},
53-
introduction: patch.introduction,
53+
introductionMarkdown: {
54+
'zh-cn': patch.introduction_zh_cn,
55+
'ja-jp': patch.introduction_ja_jp,
56+
'en-us': patch.introduction_en_us
57+
},
5458
banner: patch.banner,
5559
status: patch.status,
5660
view: patch.view,
@@ -63,6 +67,7 @@ export const getPatchById = async (
6367
resourceUpdateTime: patch.resource_update_time,
6468
content_limit: patch.content_limit,
6569
cover: patch.cover,
70+
released: patch.released,
6671
user: {
6772
id: patch.user.id,
6873
name: patch.user.name,

app/api/patch/introduction/route.ts

Lines changed: 0 additions & 81 deletions
This file was deleted.

app/character/page.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export const revalidate = 5
99
export const metadata: Metadata = kunMetadata
1010

1111
export default async function CharIndex() {
12-
const response = await kunGetActions({ page: 1, limit: 100 })
12+
const response = await kunGetActions({ page: 1, limit: 72 })
1313
if (typeof response === 'string') {
1414
return <ErrorComponent error={response} />
1515
}

app/patch/[id]/actions.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { z } from 'zod'
55
import { verifyHeaderCookie } from '~/utils/actions/verifyHeaderCookie'
66
import { safeParseSchema } from '~/utils/actions/safeParseSchema'
77
import { getPatchById } from '~/app/api/patch/get'
8-
import { getPatchIntroduction } from '~/app/api/patch/introduction/route'
98
import { getPatchDetail } from '~/app/api/patch/detail/route'
109
import { getPatchContributor } from '~/app/api/patch/contributor/route'
1110
import { updatePatchViews } from '~/app/api/patch/views/put'
@@ -28,18 +27,6 @@ export const kunGetPatchActions = cache(
2827
}
2928
)
3029

31-
export const kunGetPatchIntroductionActions = cache(
32-
async (params: z.infer<typeof patchIdSchema>) => {
33-
const input = safeParseSchema(patchIdSchema, params)
34-
if (typeof input === 'string') {
35-
return input
36-
}
37-
38-
const response = await getPatchIntroduction(input)
39-
return response
40-
}
41-
)
42-
4330
export const kunGetContributorActions = cache(
4431
async (params: z.infer<typeof patchIdSchema>) => {
4532
const input = safeParseSchema(patchIdSchema, params)

app/patch/[id]/layout.tsx

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
import { ErrorComponent } from '~/components/error/ErrorComponent'
2-
import {
3-
kunGetPatchActions,
4-
kunGetPatchIntroductionActions,
5-
kunUpdatePatchViewsActions
6-
} from './actions'
2+
import { kunGetPatchActions, kunUpdatePatchViewsActions } from './actions'
73
import { PatchContainer } from '~/components/patch/Container'
84
import { verifyHeaderCookie } from '~/utils/actions/verifyHeaderCookie'
95

@@ -23,7 +19,6 @@ export default async function Kun({ params, children }: Props) {
2319
kunGetPatchActions({
2420
patchId: Number(id)
2521
}),
26-
kunGetPatchIntroductionActions({ patchId: Number(id) }),
2722
kunUpdatePatchViewsActions({ patchId: Number(id) })
2823
])
2924
if (typeof patch === 'string') {
@@ -37,7 +32,7 @@ export default async function Kun({ params, children }: Props) {
3732
const payload = await verifyHeaderCookie()
3833

3934
return (
40-
<PatchContainer patch={patch} intro={intro} uid={payload?.uid}>
35+
<PatchContainer patch={patch} uid={payload?.uid}>
4136
{children}
4237
</PatchContainer>
4338
)

0 commit comments

Comments
 (0)