Skip to content

Commit

Permalink
Merge pull request #76 from TeamHARA/feat/pagination
Browse files Browse the repository at this point in the history
[FIX] 고민목록 조회하기 API에 pagination 추가
  • Loading branch information
leGit-y authored Nov 17, 2023
2 parents 95b2895 + 9365324 commit c19dbb2
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 17 deletions.
3 changes: 2 additions & 1 deletion src/controller/worryController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
41 changes: 34 additions & 7 deletions src/repository/worryRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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:{
Expand All @@ -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:{
Expand All @@ -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'
}
})
}
Expand All @@ -190,7 +217,7 @@ const findWorryListByTemplate = async(templateId: number,userId: number) => {
}
},
orderBy:{
created_at: 'asc'
updated_at: 'desc'
}
})
}
Expand All @@ -199,11 +226,11 @@ export default {
createWorry,
updateWorry,
deleteWorry,
// deleteWorryByUserId,
findWorryById,
createFinalAnswer,
updateDeadline,
findWorryListSolved,
findAllWorryListSolved,
findWorryListUnsolved,
findWorryListByTemplate

Expand Down
19 changes: 14 additions & 5 deletions src/router/worryRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
8 changes: 4 additions & 4 deletions src/service/worryService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);

Expand Down

0 comments on commit c19dbb2

Please sign in to comment.