Skip to content

Commit

Permalink
현재 진행 중인 챌린지 전체 조회 API
Browse files Browse the repository at this point in the history
  • Loading branch information
wook-hyung committed Dec 15, 2023
1 parent 7314283 commit e5fe493
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
20 changes: 20 additions & 0 deletions controllers/challengeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,26 @@ const challengeController = {
});
}
},

getChallenges: async (req, res) => {
const memberId = getUserIdFromJwt(req.headers.authorization);

const result = await challengeService.getChallenges(memberId);

if (result.success) {
return res.status(200).json({
success: true,
message: '챌린지 조회 요청에 성공했습니다.',
data: result.data,
});
} else {
return res.status(400).json({
success: false,
message: '챌린지 조회 요청에 실패했습니다.',
err: result.err.message,
});
}
},
};

module.exports = {
Expand Down
12 changes: 12 additions & 0 deletions models/challengeModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ const challengeModel = {
const [rows, fields] = await connection.query('SELECT * FROM challenge WHERE id = ?', [challengeId]);
return rows;
},

findChallengesByMemberId: async ({ memberId }) => {
const connection = await pool.getConnection();

// 현재 진행 중인 챌린지 전체 조회 (status: PROGRESS)
const [rows, fields] = await connection.query(
'SELECT * FROM challenge WHERE id IN (SELECT challenge_id FROM challenge_participant WHERE member_id = ?) AND challenge_status = "PROGRESS"',
[memberId]
);

return rows;
},
};

module.exports = {
Expand Down
23 changes: 23 additions & 0 deletions routes/challengeRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,29 @@ router.post('/', challengeController.createChallenge);
*/
router.post('/approve', challengeController.approveChallenge);

/**
* @swagger
* paths:
* /api/challenges:
* get:
* tags: [Challenges]
* summary: "유저의 현재 진행 중인 챌린지 전체 조회"
* description: "유저의 현재 진행 중인 챌린지 전체 조회"
* responses:
* "200":
* description: "챌린지 조회 요청에 성공했습니다."
* content:
* application/json:
* schema:
* type: object
* properties:
* success:
* type: boolean
* message:
* type: string
*/
router.get('/', challengeController.getUpcomingChallenge);

/**
* @swagger
* paths:
Expand Down
18 changes: 18 additions & 0 deletions services/challengeService.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,24 @@ const challengeService = {
};
}
},

getChallenges: async ({ memberId }) => {
try {
const result = await challengeModel.findChallengesByMemberId({
memberId,
});

return {
success: true,
data: result,
};
} catch (err) {
return {
success: false,
err,
};
}
},
};

module.exports = {
Expand Down

0 comments on commit e5fe493

Please sign in to comment.