From 0be1c507dc72f754ef49357cc9f3234614263181 Mon Sep 17 00:00:00 2001 From: leGit-y Date: Fri, 17 Nov 2023 14:27:25 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[FEAT]=20=EA=B3=A0=EB=AF=BC=EB=AA=A9?= =?UTF-8?q?=EB=A1=9D=20=EC=A1=B0=ED=9A=8C=ED=95=98=EA=B8=B0=20API=20?= =?UTF-8?q?=EC=97=90=20pagination=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/controller/worryController.ts | 3 ++- src/repository/worryRepository.ts | 41 +++++++++++++++++++++++++------ src/router/worryRouter.ts | 19 ++++++++++---- src/service/worryService.ts | 8 +++--- 4 files changed, 54 insertions(+), 17 deletions(-) diff --git a/src/controller/worryController.ts b/src/controller/worryController.ts index 489290d..fd24459 100644 --- a/src/controller/worryController.ts +++ b/src/controller/worryController.ts @@ -94,9 +94,10 @@ const patchDeadline = async (req: Request, res: Response, next: NextFunction) => const getWorryList = async (req: Request, res: Response, next: NextFunction) => { try { const { isSolved } = req.params; + const { page, limit } = req.query; const { userId }= req.body; - const data = await worryService.getWorryList(+isSolved,userId); + const data = await worryService.getWorryList(+isSolved, +page!, +limit!, userId); return res.status(sc.OK).send(success(statusCode.OK, rm.GET_WORRY_LIST_SUCCESS,data)); diff --git a/src/repository/worryRepository.ts b/src/repository/worryRepository.ts index 6ade0d5..b4e7651 100644 --- a/src/repository/worryRepository.ts +++ b/src/repository/worryRepository.ts @@ -130,7 +130,7 @@ const updateDeadline = async(deadlineUpdateDAO: deadlineUpdateDAO) => { }) } -const findWorryListSolved = async(userId: number) => { +const findAllWorryListSolved = async(userId: number) => { return await prisma.worry.findMany({ select:{ @@ -148,12 +148,37 @@ const findWorryListSolved = async(userId: number) => { } }, orderBy:{ - created_at: 'asc' + updated_at: 'desc' } }) } -const findWorryListUnsolved = async(userId: number) => { +const findWorryListSolved = async(userId: number, page: number, limit: number) => { + + return await prisma.worry.findMany({ + select:{ + id:true, + user_id:true, + template_id:true, + title:true, + created_at:true, + updated_at:true + }, + where: { + user_id: userId, + final_answer:{ + not: null + } + }, + skip: (page - 1) * limit, + take: limit, + orderBy:{ + updated_at: 'desc' + } + }) +} + +const findWorryListUnsolved = async(userId: number, page: number, limit: number) => { return await prisma.worry.findMany({ select:{ @@ -165,9 +190,11 @@ const findWorryListUnsolved = async(userId: number) => { where: { user_id: userId, final_answer: null - }, + }, + skip: (page - 1) * limit, + take: limit, orderBy:{ - created_at: 'asc' + created_at: 'desc' } }) } @@ -190,7 +217,7 @@ const findWorryListByTemplate = async(templateId: number,userId: number) => { } }, orderBy:{ - created_at: 'asc' + created_at: 'desc' } }) } @@ -199,11 +226,11 @@ export default { createWorry, updateWorry, deleteWorry, - // deleteWorryByUserId, findWorryById, createFinalAnswer, updateDeadline, findWorryListSolved, + findAllWorryListSolved, findWorryListUnsolved, findWorryListByTemplate diff --git a/src/router/worryRouter.ts b/src/router/worryRouter.ts index 80ecc4c..518b329 100644 --- a/src/router/worryRouter.ts +++ b/src/router/worryRouter.ts @@ -77,13 +77,22 @@ router.patch("/deadline", -router.get("/list/:isSolved", - auth, - validate, - worryController.getWorryList, +// router.get("/list/:isSolved", +// auth, +// validate, +// worryController.getWorryList, +// ); + +router.get("/:isSolved/list", +auth, +[ + query('page').notEmpty().withMessage("query string 에 'page' 값이 존재하지 않습니다"), + query('limit').notEmpty().withMessage("query string 에 'limit' 값이 존재하지 않습니다") +], +validate, +worryController.getWorryList, ); - export default router; diff --git a/src/service/worryService.ts b/src/service/worryService.ts index 9588886..d73c6a5 100644 --- a/src/service/worryService.ts +++ b/src/service/worryService.ts @@ -219,12 +219,12 @@ const patchDeadline =async (deadlineUpdateDTO: deadlineUpdateDTO) => { } -const getWorryList =async (isSolved: number, userId: number) => { +const getWorryList =async (isSolved: number, page: number, limit: number, userId: number) => { let worry = null; if(isSolved) - worry = await worryRepository.findWorryListSolved(userId); + worry = await worryRepository.findWorryListSolved(userId,page,limit); else - worry = await worryRepository.findWorryListUnsolved(userId); + worry = await worryRepository.findWorryListUnsolved(userId,page,limit); if (!worry) { throw new ClientException(rm.GET_WORRY_LIST_FAIL); @@ -247,7 +247,7 @@ const getWorryList =async (isSolved: number, userId: number) => { const getWorryListByTemplate =async (templateId: number, userId: number) => { let worry; if(templateId == 0) - worry = await worryRepository.findWorryListSolved(userId); + worry = await worryRepository.findAllWorryListSolved(userId); else worry = await worryRepository.findWorryListByTemplate(templateId,userId); From 93653243466a55098a88169ae313d17b07e93d31 Mon Sep 17 00:00:00 2001 From: leGit-y Date: Fri, 17 Nov 2023 14:39:23 +0900 Subject: [PATCH 2/2] =?UTF-8?q?[FIX]=20=EC=A0=95=EB=A0=AC=EA=B8=B0?= =?UTF-8?q?=EC=A4=80=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/repository/worryRepository.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/repository/worryRepository.ts b/src/repository/worryRepository.ts index b4e7651..93de1f4 100644 --- a/src/repository/worryRepository.ts +++ b/src/repository/worryRepository.ts @@ -217,7 +217,7 @@ const findWorryListByTemplate = async(templateId: number,userId: number) => { } }, orderBy:{ - created_at: 'desc' + updated_at: 'desc' } }) }