Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitd980ce4

Browse files
committed
feat: adding a auxiliary table for patch rating computing
1 parent413a07d commitd980ce4

File tree

17 files changed

+345
-99
lines changed

17 files changed

+345
-99
lines changed

‎app/api/edit/create.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,11 @@ export const createGalgame = async (
5656
data:{banner:imageLink}
5757
})
5858

59+
// Ensure rating_stat row exists for this patch
60+
awaitprisma.patch_rating_stat.create({
61+
data:{patch_id:newId}
62+
})
63+
5964
if(alias.length){
6065
constaliasData=alias.map((name)=>({
6166
name,

‎app/api/galgame/route.ts‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,21 @@ export const getGalgame = async (
101101
])
102102

103103
constgalgames:GalgameCard[]=data.map((gal)=>({
104-
...gal,
104+
id:gal.id,
105+
uniqueId:gal.unique_id,
106+
name:gal.name,
107+
banner:gal.banner,
108+
view:gal.view,
109+
download:gal.download,
110+
type:gal.type,
111+
language:gal.language,
112+
platform:gal.platform,
105113
tags:gal.tag.map((t)=>t.tag.name).slice(0,3),
106-
uniqueId:gal.unique_id
114+
created:gal.created,
115+
_count:gal._count,
116+
averageRating:gal.rating_stat?.avg_overall
117+
?Math.round(gal.rating_stat.avg_overall*10)/10
118+
:0
107119
}))
108120

109121
return{ galgames, total}

‎app/api/home/route.ts‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ export const getHomeData = async (
4444
constgalgames:GalgameCard[]=data.map((gal)=>({
4545
...gal,
4646
tags:gal.tag.map((t)=>t.tag.name).slice(0,3),
47-
uniqueId:gal.unique_id
47+
uniqueId:gal.unique_id,
48+
averageRating:gal.rating_stat?.avg_overall
49+
?Math.round(gal.rating_stat.avg_overall*10)/10
50+
:0
4851
}))
4952

5053
constresources:HomeResource[]=resourcesData.map((resource)=>({
@@ -67,7 +70,8 @@ export const getHomeData = async (
6770
id:resource.user.id,
6871
name:resource.user.name,
6972
avatar:resource.user.avatar,
70-
patchCount:resource.user._count.patch_resource
73+
patchCount:resource.user._count.patch_resource,
74+
role:resource.user.role
7175
}
7276
}))
7377

‎app/api/patch/get.ts‎

Lines changed: 42 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { prisma } from '~/prisma/index'
33
import{getKv,setKv}from'~/lib/redis'
44
import{PATCH_CACHE_DURATION}from'~/config/cache'
55
import{roundOneDecimal}from'~/utils/rating/average'
6-
import{KUN_GALGAME_RATING_RECOMMEND_CONST}from'~/constants/galgame'
76
importtype{Patch}from'~/types/api/patch'
87

98
constCACHE_KEY='patch'
@@ -60,46 +59,9 @@ export const getPatchById = async (
6059
return'未找到对应 Galgame'
6160
}
6261

63-
const[ratingAgg,recommendGroups,overallGroups]=awaitprisma.$transaction(
64-
[
65-
prisma.patch_rating.aggregate({
66-
where:{patch_id:patch.id},
67-
_avg:{overall:true},
68-
_count:{_all:true}
69-
}),
70-
prisma.patch_rating.groupBy({
71-
by:['recommend'],
72-
where:{patch_id:patch.id},
73-
_count:{_all:true},
74-
orderBy:{recommend:'asc'}
75-
}),
76-
prisma.patch_rating.groupBy({
77-
by:['overall'],
78-
where:{patch_id:patch.id},
79-
_count:{_all:true},
80-
orderBy:{overall:'asc'}
81-
})
82-
]
83-
)
84-
85-
constrecommendCountMap:Record<string,number>={}
86-
for(constkofKUN_GALGAME_RATING_RECOMMEND_CONST){
87-
recommendCountMap[k]=0
88-
}
89-
for(constgofrecommendGroups){
90-
constc=(gasany)._count?._all??0
91-
recommendCountMap[g.recommend]=c
92-
}
93-
94-
constscoreCountMap:Record<number,number>={}
95-
for(lets=1;s<=10;s++)scoreCountMap[s]=0
96-
for(constgofoverallGroupsasany[]){
97-
constscore=g.overallasnumber
98-
constc=g._count?._all??0
99-
if(typeofscore==='number'&&score>=1&&score<=10){
100-
scoreCountMap[score]=c
101-
}
102-
}
62+
conststat=awaitprisma.patch_rating_stat.findUnique({
63+
where:{patch_id:patch.id}
64+
})
10365

10466
constresponse:Patch={
10567
id:patch.id,
@@ -118,21 +80,45 @@ export const getPatchById = async (
11880
alias:patch.alias.map((a)=>a.name),
11981
isFavorite:patch.favorite_folder.length>0,
12082
contentLimit:patch.content_limit,
121-
ratingSummary:{
122-
average:roundOneDecimal(ratingAgg._avg.overall),
123-
count:ratingAgg._count._all,
124-
histogram:Array.from({length:10},(_,i)=>({
125-
score:i+1,
126-
count:scoreCountMap[i+1]
127-
})),
128-
recommend:{
129-
strong_no:recommendCountMap.strong_no,
130-
no:recommendCountMap.no,
131-
neutral:recommendCountMap.neutral,
132-
yes:recommendCountMap.yes,
133-
strong_yes:recommendCountMap.strong_yes
134-
}
135-
},
83+
ratingSummary:stat
84+
?{
85+
average:roundOneDecimal(stat.avg_overall),
86+
count:stat.count,
87+
histogram:[
88+
{score:1,count:stat.o1},
89+
{score:2,count:stat.o2},
90+
{score:3,count:stat.o3},
91+
{score:4,count:stat.o4},
92+
{score:5,count:stat.o5},
93+
{score:6,count:stat.o6},
94+
{score:7,count:stat.o7},
95+
{score:8,count:stat.o8},
96+
{score:9,count:stat.o9},
97+
{score:10,count:stat.o10}
98+
],
99+
recommend:{
100+
strong_no:stat.rec_strong_no,
101+
no:stat.rec_no,
102+
neutral:stat.rec_neutral,
103+
yes:stat.rec_yes,
104+
strong_yes:stat.rec_strong_yes
105+
}
106+
}
107+
:{
108+
average:0,
109+
count:0,
110+
histogram:Array.from({length:10},(_,i)=>({
111+
score:i+1,
112+
count:0
113+
})),
114+
recommend:{
115+
strong_no:0,
116+
no:0,
117+
neutral:0,
118+
yes:0,
119+
strong_yes:0
120+
}
121+
},
136122
user:{
137123
id:patch.user.id,
138124
name:patch.user.name,

‎app/api/patch/rating/create.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import{z}from'zod'
22
import{prisma}from'~/prisma/index'
33
import{patchRatingCreateSchema}from'~/validations/patch'
4+
import{recomputePatchRatingStat}from'./stat'
45
importtype{KunPatchRating}from'~/types/api/galgame'
56

67
exportconstcreatePatchRating=async(
@@ -51,6 +52,8 @@ export const createPatchRating = async (
5152
}
5253
})
5354

55+
awaitrecomputePatchRatingStat(patchId)
56+
5457
return{
5558
id:data.id,
5659
uniqueId:data.patch.unique_id,

‎app/api/patch/rating/delete.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import{z}from'zod'
22
import{prisma}from'~/prisma/index'
3+
import{recomputePatchRatingStat}from'./stat'
34

45
constratingIdSchema=z.object({
56
ratingId:z.coerce.number({message:'ID 不正确'}).min(1).max(9999999)
@@ -22,5 +23,8 @@ export const deletePatchRating = async (
2223
}
2324

2425
awaitprisma.patch_rating.delete({where:{id:input.ratingId}})
26+
27+
awaitrecomputePatchRatingStat(rating.patch_id)
28+
2529
return{}
2630
}

‎app/api/patch/rating/stat.ts‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import{prisma}from'~/prisma/index'
2+
3+
// Recompute and upsert rating statistics for a patch
4+
exportconstrecomputePatchRatingStat=async(patchId:number)=>{
5+
constagg=awaitprisma.patch_rating.aggregate({
6+
where:{patch_id:patchId},
7+
_avg:{overall:true},
8+
_count:{_all:true}
9+
})
10+
11+
// Recommend counts (explicit counts to avoid complex typings)
12+
const[strong_no,no,neutral,yes,strong_yes]=awaitPromise.all([
13+
prisma.patch_rating.count({
14+
where:{patch_id:patchId,recommend:'strong_no'}
15+
}),
16+
prisma.patch_rating.count({
17+
where:{patch_id:patchId,recommend:'no'}
18+
}),
19+
prisma.patch_rating.count({
20+
where:{patch_id:patchId,recommend:'neutral'}
21+
}),
22+
prisma.patch_rating.count({
23+
where:{patch_id:patchId,recommend:'yes'}
24+
}),
25+
prisma.patch_rating.count({
26+
where:{patch_id:patchId,recommend:'strong_yes'}
27+
})
28+
])
29+
30+
// Overall histogram 1..10
31+
consthistCounts=awaitPromise.all(
32+
Array.from({length:10},(_,i)=>
33+
prisma.patch_rating.count({
34+
where:{patch_id:patchId,overall:i+1}
35+
})
36+
)
37+
)
38+
39+
awaitprisma.patch_rating_stat.upsert({
40+
where:{patch_id:patchId},
41+
create:{
42+
patch_id:patchId,
43+
avg_overall:agg._avg.overall??0,
44+
count:agg._count._all??0,
45+
rec_strong_no:strong_no,
46+
rec_no:no,
47+
rec_neutral:neutral,
48+
rec_yes:yes,
49+
rec_strong_yes:strong_yes,
50+
o1:histCounts[0],
51+
o2:histCounts[1],
52+
o3:histCounts[2],
53+
o4:histCounts[3],
54+
o5:histCounts[4],
55+
o6:histCounts[5],
56+
o7:histCounts[6],
57+
o8:histCounts[7],
58+
o9:histCounts[8],
59+
o10:histCounts[9]
60+
},
61+
update:{
62+
avg_overall:agg._avg.overall??0,
63+
count:agg._count._all??0,
64+
rec_strong_no:strong_no,
65+
rec_no:no,
66+
rec_neutral:neutral,
67+
rec_yes:yes,
68+
rec_strong_yes:strong_yes,
69+
o1:histCounts[0],
70+
o2:histCounts[1],
71+
o3:histCounts[2],
72+
o4:histCounts[3],
73+
o5:histCounts[4],
74+
o6:histCounts[5],
75+
o7:histCounts[6],
76+
o8:histCounts[7],
77+
o9:histCounts[8],
78+
o10:histCounts[9]
79+
}
80+
})
81+
}

‎app/api/patch/rating/update.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import{z}from'zod'
22
import{prisma}from'~/prisma/index'
33
import{patchRatingUpdateSchema}from'~/validations/patch'
4+
import{recomputePatchRatingStat}from'./stat'
45
importtype{KunPatchRating}from'~/types/api/galgame'
56

67
exportconstupdatePatchRating=async(
@@ -59,6 +60,8 @@ export const updatePatchRating = async (
5960
}
6061
})
6162

63+
awaitrecomputePatchRatingStat(patchId)
64+
6265
return{
6366
id:data.id,
6467
uniqueId:data.patch.unique_id,

‎app/api/search/route.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,10 @@ export const searchGalgame = async (
150150
constgalgames:GalgameCard[]=data.map((gal)=>({
151151
...gal,
152152
tags:gal.tag.map((t)=>t.tag.name).slice(0,3),
153-
uniqueId:gal.unique_id
153+
uniqueId:gal.unique_id,
154+
averageRating:gal.rating_stat?.avg_overall
155+
?Math.round(gal.rating_stat.avg_overall*10)/10
156+
:0
154157
}))
155158

156159
return{ galgames, total}

‎app/api/tag/galgame/route.ts‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ export const getPatchByTag = async (
3131
])
3232

3333
constpatches=data.map((p)=>p.patch)
34-
constgalgames=patches.map((gal)=>({
34+
constgalgames:GalgameCard[]=patches.map((gal)=>({
3535
...gal,
3636
tags:gal.tag.map((t)=>t.tag.name).slice(0,3),
37-
uniqueId:gal.unique_id
37+
uniqueId:gal.unique_id,
38+
averageRating:gal.rating_stat?.avg_overall
39+
?Math.round(gal.rating_stat.avg_overall*10)/10
40+
:0
3841
}))
3942

4043
return{ galgames, total}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp