Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
bhh9860 committed Dec 16, 2023
2 parents 6ce1c7f + dcf630d commit 446569d
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 3 deletions.
20 changes: 20 additions & 0 deletions controllers/challengeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,26 @@ const challengeController = {
}
},

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

const result = await challengeService.getInprogressChallenges({ 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,
});
}
},

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

Expand Down
24 changes: 22 additions & 2 deletions models/challengeModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const challengeModel = {
}
},

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

try {
Expand All @@ -98,8 +98,13 @@ const challengeModel = {
const connection = await pool.getConnection();

try {
// const [rows, fields] = await connection.query(
// `SELECT COUNT(DISTINCT cT.id) FROM challenge cT LEFT JOIN challenge_participant cpT ON cT.id = cpT.challenge_id LEFT JOIN member m ON cpT.member_id = m.id WHERE m.id = 22 AND cT.challenge_status = "PROGRESS";`,
// [memberId]
// );

const [rows, fields] = await connection.query(
`SELECT COUNT(DISTINCT cT.id) FROM challenge cT LEFT JOIN challenge_certification ccT ON cT.id = ccT.challenge_id LEFT JOIN member m ON ccT.member_id = m.id WHERE m.id = ? AND cT.challenge_status = "PROGRESS";`,
`SELECT COUNT(DISTINCT cT.id) FROM challenge cT LEFT JOIN challenge_participant cpT ON cT.id = cpT.challenge_id LEFT JOIN member m ON cpT.member_id = m.id LEFT JOIN challenge_certification ccT ON cT.id = ccT.challenge_id WHERE m.id = 22 AND cT.challenge_status = 'PROGRESS' AND (ccT.is_authenticate IS NULL OR (ccT.is_authenticate = FALSE AND ccT.updated_at < CURDATE()));`,
[memberId]
);

Expand All @@ -123,6 +128,21 @@ const challengeModel = {
connection.release();
}
},

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

try {
const [rows, fields] = await connection.query(
'SELECT * FROM challenge WHERE id IN (SELECT challenge_id FROM challenge_participant WHERE member_id = ?)',
[memberId]
);

return rows;
} finally {
connection.release();
}
},
};

module.exports = {
Expand Down
25 changes: 24 additions & 1 deletion routes/challengeRouter.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,29 @@ router.post('/approve', challengeController.approveChallenge);
* /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.getChallenges);

/**
* @swagger
* paths:
* /api/challenges/in-progress:
* get:
* tags: [Challenges]
* summary: "유저의 현재 진행 중인 챌린지 전체 조회"
* description: "유저의 현재 진행 중인 챌린지 전체 조회"
* responses:
Expand All @@ -132,7 +155,7 @@ router.post('/approve', challengeController.approveChallenge);
* message:
* type: string
*/
router.get('/', challengeController.getChallenges);
router.get('/in-progress', challengeController.getInprogressChallenges);

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

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

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

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

0 comments on commit 446569d

Please sign in to comment.