@@ -494,55 +494,61 @@ export const getMemberPromptsRepo = async (
494494) => {
495495 const whereCondition : any = { user_id : memberId } ;
496496
497- // 커서가 있으면 해당 ID보다 작은 것들만 조회 (최신순이므로)
498497 if ( cursor ) {
499498 whereCondition . prompt_id = { lt : cursor } ;
500499 }
501500
502- // limit + 1개를 가져와서 다음 페이지 존재 여부 확인
503501 const prompts = await prisma . prompt . findMany ( {
504502 where : whereCondition ,
505- select : {
506- prompt_id : true ,
507- title : true ,
508- created_at : true ,
509- downloads : true ,
510- views : true ,
511- is_free : true ,
512- price : true ,
513- description : true ,
503+ include : {
514504 images : {
515505 select : {
516506 image_url : true ,
517- order_index : true ,
518507 } ,
519- orderBy : { order_index : "asc" } ,
520- take : 1 ,
508+ orderBy : {
509+ order_index : "asc" ,
510+ } ,
521511 } ,
522- reviews : {
512+ user : {
523513 select : {
524- rating : true ,
525- content : true ,
514+ user_id : true ,
515+ nickname : true ,
516+ profileImage : {
517+ select : {
518+ url : true ,
519+ } ,
520+ } ,
526521 } ,
527- orderBy : { created_at : "desc" } ,
528- take : 1 ,
529522 } ,
530523 models : {
531- select : {
524+ include : {
532525 model : {
533- select : { name : true } ,
526+ select : {
527+ name : true ,
528+ } ,
534529 } ,
535530 } ,
536531 } ,
532+ reviews : {
533+ select : {
534+ rating : true ,
535+ content : true ,
536+ } ,
537+ orderBy : {
538+ created_at : "desc" ,
539+ } ,
540+ } ,
537541 categories : {
538542 select : {
539543 category : {
540- select : { name : true } ,
544+ select : {
545+ name : true ,
546+ } ,
541547 } ,
542548 } ,
543549 } ,
544550 } ,
545- orderBy : { prompt_id : "desc" } , // 최신순 (ID 내림차순)
551+ orderBy : { prompt_id : "desc" } ,
546552 take : limit + 1 ,
547553 } ) ;
548554
@@ -552,23 +558,29 @@ export const getMemberPromptsRepo = async (
552558 ? resultPrompts [ resultPrompts . length - 1 ] . prompt_id
553559 : null ;
554560
555- // 응답 포맷 변환
556- const formatted = resultPrompts . map ( ( p ) => ( {
557- prompt_id : p . prompt_id ,
558- title : p . title ,
559- image_url : p . images ?. [ 0 ] ?. image_url || null ,
560- created_at : p . created_at ,
561- downloads : p . downloads ,
562- views : p . views ,
563- is_free : p . is_free ? 1 : 0 ,
564- price : p . price ,
565- description : p . description ,
566- reviews : p . reviews ?. [ 0 ]
567- ? { rating : p . reviews [ 0 ] . rating , content : p . reviews [ 0 ] . content }
568- : null ,
569- models : p . models . map ( ( m : any ) => m . model . name ) ,
570- categories : p . categories . map ( ( c : any ) => c . category . name ) ,
571- } ) ) ;
561+ const formatted = resultPrompts . map ( ( p ) => {
562+ const review_count = p . reviews . length ;
563+ const review_rating_avg =
564+ review_count > 0
565+ ? p . reviews . reduce ( ( sum , review ) => sum + review . rating , 0 ) /
566+ review_count
567+ : 0 ;
568+
569+ const { user, reviews, categories, ...restOfP } = p ;
570+
571+ return {
572+ ...restOfP ,
573+ user : {
574+ user_id : user . user_id ,
575+ nickname : user . nickname ,
576+ profileImage : user . profileImage ?. url || null ,
577+ } ,
578+ review_count : review_count ,
579+ review_rating_avg : parseFloat ( review_rating_avg . toFixed ( 1 ) ) ,
580+ reviews : reviews . length > 0 ? reviews [ 0 ] : null ,
581+ categories : categories . map ( ( c ) => c . category . name ) ,
582+ } ;
583+ } ) ;
572584
573585 return {
574586 prompts : formatted ,
0 commit comments