Skip to content

Commit

Permalink
현재 진행 중인 챌린지 상태 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
wook-hyung committed Dec 16, 2023
1 parent da9e782 commit 5812f69
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
9 changes: 7 additions & 2 deletions controllers/challengeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,17 @@ const challengeController = {
challengeId,
});

const myResult = result.data.filter((obj) => obj.id === myId)[0];
const otherResult = result.data.filter((obj) => obj.id !== myId)[0];

if (result.success) {
return res.status(200).json({
success: true,
message: '챌린지 조회 요청에 성공했습니다.',
data: result.data,
myId,
data: {
myResult,
otherResult,
},
});
} else {
return res.status(400).json({
Expand Down
28 changes: 24 additions & 4 deletions models/memberModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,33 @@ const memberModel = {
const connection = await pool.getConnection();

try {
const [rows, fields] = await connection.query(
'SELECT m.id, m.nickname, m.champion, cc.is_authenticate, cc.participation_count, ch.name AS challenge_name, ch.category, ch.reward FROM member m JOIN (SELECT cp.member_id, MAX(cc.created_at) AS max_created_at FROM challenge_participant cp JOIN challenge_certification cc ON cp.member_id = cc.member_id AND cp.challenge_id = cc.challenge_id WHERE cp.challenge_id IN (SELECT cp_inner.challenge_id FROM challenge_participant cp_inner WHERE cp_inner.member_id IN (SELECT cp_sub.member_id FROM challenge_participant cp_sub WHERE cp_sub.challenge_id = ?)) GROUP BY cp.member_id) AS latest_certification ON m.id = latest_certification.member_id JOIN challenge_certification cc ON latest_certification.member_id = cc.member_id AND latest_certification.max_created_at = cc.created_at JOIN challenge ch ON cc.challenge_id = ch.id;',
const [rows] = await connection.query(
`
SELECT
ch.name AS challengeName,
ch.category,
ch.reward,
m.id,
m.nickname,
m.champion,
COUNT(cc.member_id) AS certificationCount
FROM
challenge ch
JOIN
challenge_participant cp ON ch.id = cp.challenge_id
JOIN
member m ON cp.member_id = m.id
LEFT JOIN
challenge_certification cc ON cp.member_id = cc.member_id AND ch.id = cc.challenge_id AND cc.is_authenticate = true
WHERE
ch.id = ?
GROUP BY
m.id;
`,
[challengeId]
);

return rows;
} catch (err) {
return err;
} finally {
connection.release();
}
Expand Down

0 comments on commit 5812f69

Please sign in to comment.