11import prisma from '../middlewares/prisma.js' ;
22
3- export const createComment = async ( req , res , next ) => {
3+ export const getCommentByArticle = async ( req , res , next ) => {
44 try {
5- const { content, articleId } = req . body ;
6-
7- if ( ! content ) {
8- return res . status ( 400 ) . json ( { success : false , message : 'Content is required' } ) ;
9- }
5+ const { id } = req . params ;
6+ const cursor = req . query . cursor ;
7+ const limit = parseInt ( req . query . limit ) || 10 ;
8+ const direction = req . query . direction || 'next' ; // 'next' or 'prev'
109
11- const comment = await prisma . comment . create ( { data : { content, articleId } } ) ;
12- res . status ( 201 ) . json ( { success : true , data : comment } ) ;
13- } catch ( error ) {
14- next ( error ) ;
15- }
16- } ;
10+ const validLimit = Math . min ( Math . max ( 1 , limit ) , 50 ) ; // 1-50 사이로 제한
1711
18- export const updateComment = async ( req , res , next ) => {
19- try {
20- const { id } = req . params ;
21- const { content } = req . body ;
12+ let whereCondition = { articleId : id } ;
13+ let orderBy = { createdAt : 'desc' } ;
2214
23- if ( ! id ) {
24- return res . status ( 400 ) . json ( { success : false , message : 'Id is required' } ) ;
15+ // cursor가 있는 경우
16+ if ( cursor ) {
17+ const cursorDate = new Date ( cursor ) ;
18+ if ( direction === 'prev' ) {
19+ // 이전 페이지: cursor보다 이전 데이터
20+ whereCondition . createdAt = { lt : cursorDate } ;
21+ orderBy = { createdAt : 'desc' } ;
22+ } else {
23+ // 다음 페이지: cursor보다 이후 데이터
24+ whereCondition . createdAt = { gt : cursorDate } ;
25+ orderBy = { createdAt : 'asc' } ;
26+ }
2527 }
2628
27- const comment = await prisma . comment . update ( { where : { id } , data : { content } } ) ;
28- res . status ( 200 ) . json ( { success : true , data : comment } ) ;
29- } catch ( error ) {
30- next ( error ) ;
31- }
32- } ;
29+ const comments = await prisma . comment . findMany ( {
30+ where : whereCondition ,
31+ take : validLimit + 1 , // 한 개 더 가져와서 hasNextPage 확인
32+ orderBy,
33+ select : {
34+ id : true ,
35+ content : true ,
36+ createdAt : true ,
37+ updatedAt : true ,
38+ } ,
39+ } ) ;
3340
34- export const deleteComment = async ( req , res , next ) => {
35- try {
36- const { id } = req . params ;
41+ // hasNextPage 확인
42+ const hasNextPage = comments . length > validLimit ;
43+ if ( hasNextPage ) {
44+ comments . pop ( ) ; // 마지막 요소 제거
45+ }
3746
38- if ( ! id ) {
39- return res . status ( 400 ) . json ( { success : false , message : 'Id is required' } ) ;
47+ // direction이 'prev'인 경우 결과를 다시 역순으로 정렬
48+ if ( direction === 'prev' ) {
49+ comments . reverse ( ) ;
4050 }
4151
42- await prisma . comment . delete ( { where : { id } } ) ;
43- res . status ( 200 ) . json ( { success : true , data : null } ) ;
44- } catch ( error ) {
45- next ( error ) ;
46- }
47- } ;
52+ // cursor 정보 계산
53+ const nextCursor =
54+ comments . length > 0 ? comments [ comments . length - 1 ] . createdAt . toISOString ( ) : null ;
55+ const prevCursor = comments . length > 0 ? comments [ 0 ] . createdAt . toISOString ( ) : null ;
4856
49- export const getComment = async ( req , res , next ) => {
50- try {
51- const comment = await prisma . comment . findMany ( ) ;
52- res . status ( 200 ) . json ( { success : true , data : comment } ) ;
57+ res . status ( 200 ) . json ( {
58+ success : true ,
59+ data : comments ,
60+ pagination : {
61+ hasNextPage,
62+ hasPrevPage : ! ! cursor ,
63+ nextCursor,
64+ prevCursor,
65+ limit : validLimit ,
66+ direction,
67+ } ,
68+ } ) ;
5369 } catch ( error ) {
5470 next ( error ) ;
5571 }
5672} ;
5773
58- export const getCommentByArticle = async ( req , res , next ) => {
74+ export const getCommentByProduct = async ( req , res , next ) => {
5975 try {
6076 const { id } = req . params ;
6177 const cursor = req . query . cursor ;
6278 const limit = parseInt ( req . query . limit ) || 10 ;
6379 const direction = req . query . direction || 'next' ; // 'next' or 'prev'
6480
65- if ( ! id ) {
66- return res . status ( 400 ) . json ( { success : false , message : 'Id is required' } ) ;
67- }
68-
6981 const validLimit = Math . min ( Math . max ( 1 , limit ) , 50 ) ; // 1-50 사이로 제한
7082
71- let whereCondition = { articleId : id } ;
83+ let whereCondition = { productId : id } ;
7284 let orderBy = { createdAt : 'desc' } ;
7385
74- // cursor가 있는 경우
7586 if ( cursor ) {
7687 const cursorDate = new Date ( cursor ) ;
7788 if ( direction === 'prev' ) {
78- // 이전 페이지: cursor보다 이전 데이터
7989 whereCondition . createdAt = { lt : cursorDate } ;
8090 orderBy = { createdAt : 'desc' } ;
8191 } else {
82- // 다음 페이지: cursor보다 이후 데이터
8392 whereCondition . createdAt = { gt : cursorDate } ;
8493 orderBy = { createdAt : 'asc' } ;
8594 }
@@ -97,18 +106,15 @@ export const getCommentByArticle = async (req, res, next) => {
97106 } ,
98107 } ) ;
99108
100- // hasNextPage 확인
101109 const hasNextPage = comments . length > validLimit ;
102110 if ( hasNextPage ) {
103111 comments . pop ( ) ; // 마지막 요소 제거
104112 }
105113
106- // direction이 'prev'인 경우 결과를 다시 역순으로 정렬
107114 if ( direction === 'prev' ) {
108115 comments . reverse ( ) ;
109116 }
110117
111- // cursor 정보 계산
112118 const nextCursor =
113119 comments . length > 0 ? comments [ comments . length - 1 ] . createdAt . toISOString ( ) : null ;
114120 const prevCursor = comments . length > 0 ? comments [ 0 ] . createdAt . toISOString ( ) : null ;
0 commit comments