|
| 1 | +import prisma from '~/prisma/prisma' |
| 2 | +import { getUserRatingSchema } from '~/validations/user' |
| 3 | +import type { GalgameRatingCard } from '~/types/api/galgame-rating' |
| 4 | +import type { Prisma } from '@prisma/client' |
| 5 | + |
| 6 | +export default defineEventHandler(async (event) => { |
| 7 | + const input = kunParseGetQuery(event, getUserRatingSchema) |
| 8 | + if (typeof input === 'string') { |
| 9 | + return kunError(event, input) |
| 10 | + } |
| 11 | + |
| 12 | + const nsfw = getNSFWCookie(event) |
| 13 | + const { userId, page, limit } = input |
| 14 | + const skip = (page - 1) * limit |
| 15 | + |
| 16 | + const where: Prisma.galgame_ratingWhereInput = { |
| 17 | + user_id: userId, |
| 18 | + galgame: { |
| 19 | + content_limit: nsfw === 'sfw' ? 'sfw' : undefined |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + const [rows, totalCount] = await Promise.all([ |
| 24 | + prisma.galgame_rating.findMany({ |
| 25 | + take: limit, |
| 26 | + skip, |
| 27 | + where, |
| 28 | + orderBy: { created: 'desc' }, |
| 29 | + include: { |
| 30 | + user: { select: { id: true, name: true, avatar: true } }, |
| 31 | + galgame: { |
| 32 | + select: { |
| 33 | + id: true, |
| 34 | + banner: true, |
| 35 | + name_en_us: true, |
| 36 | + name_ja_jp: true, |
| 37 | + name_zh_cn: true, |
| 38 | + name_zh_tw: true, |
| 39 | + content_limit: true |
| 40 | + } |
| 41 | + }, |
| 42 | + _count: { |
| 43 | + select: { |
| 44 | + like: true |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + }), |
| 49 | + prisma.galgame_rating.count({ where }) |
| 50 | + ]) |
| 51 | + |
| 52 | + const ratingData: GalgameRatingCard[] = rows.map((r) => ({ |
| 53 | + id: r.id, |
| 54 | + user: r.user, |
| 55 | + recommend: r.recommend, |
| 56 | + overall: r.overall, |
| 57 | + view: r.view, |
| 58 | + galgameType: r.galgame_type, |
| 59 | + play_status: r.play_status, |
| 60 | + short_summary: r.short_summary, |
| 61 | + spoiler_level: r.spoiler_level, |
| 62 | + art: r.art, |
| 63 | + story: r.story, |
| 64 | + music: r.music, |
| 65 | + character: r.character, |
| 66 | + route: r.route, |
| 67 | + system: r.system, |
| 68 | + voice: r.voice, |
| 69 | + replay_value: r.replay_value, |
| 70 | + likeCount: r._count.like, |
| 71 | + created: r.created, |
| 72 | + updated: r.updated, |
| 73 | + galgame: { |
| 74 | + id: r.galgame.id, |
| 75 | + contentLimit: r.galgame.content_limit, |
| 76 | + name: { |
| 77 | + 'en-us': r.galgame.name_en_us, |
| 78 | + 'ja-jp': r.galgame.name_ja_jp, |
| 79 | + 'zh-cn': r.galgame.name_zh_cn, |
| 80 | + 'zh-tw': r.galgame.name_zh_tw |
| 81 | + } |
| 82 | + } |
| 83 | + })) |
| 84 | + |
| 85 | + return { ratingData, totalCount } |
| 86 | +}) |
0 commit comments