Skip to content

Commit

Permalink
Ranking 로직 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
moonelysian committed Feb 2, 2020
1 parent a7ab771 commit 5ca53a7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 44 deletions.
27 changes: 2 additions & 25 deletions src/controllers/ChallengeController.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { getAchievement } from '../utils/AchievementCalculator';
import { transformRecords } from './RecordController';
import { DuplicateChallengeError } from '../errors/DuplicateChallengeError';
import { NotFoundChallengeError } from '../errors/NotFoundChallengeError';
import { Ranking } from '../utils/Ranking';
import { Ranking } from './RankingController';
import { QuotaError } from '../errors/QuotaError';

const getChallenges = async function(req ,res){
Expand All @@ -26,27 +26,6 @@ const getChallenges = async function(req ,res){
res.send(result)
}

const getChallenge = async function(req, res, next){
const user = req.user;
const id = req.params.challengeId;
const challenge = await Challenge.findOne({ where: { id: id } });
if (challenge) {
const ranks = await Ranking(challenge);
const baseChallenge = await challenge.getBaseChallenge();
const challengeWithMeta = {
baseChallenge,
ranks,
achievement: await getAchievement(challenge, req.user),
challengersNumber: (await baseChallenge.getChallenges()).length,
}
res.send({ data: transformChallenge(challengeWithMeta) });
}
else {
next(new NotFoundChallengeError('존재하지 않는 challenge'));
return;
}
}

const getChallengeRecords = async function(req, res){
const challengeId = req.params.challengeId
const user = req.user
Expand Down Expand Up @@ -156,8 +135,7 @@ const transformChallenge = (challenge) => ({
object_unit: challenge.baseChallenge.objectUnit,
exercise_type: challenge.baseChallenge.exerciseType,
quota: challenge.baseChallenge.quota,
},
ranks: challenge.ranks ? challenge.ranks : undefined,
}
});

const transformChallenges = (challenges) => challenges.map(transformChallenge);
Expand All @@ -173,7 +151,6 @@ const hasSameChallenge = async (user, baseChallenge) => {

export default {
getChallenges,
getChallenge,
getChallengeRecords,
createChallenge,
deleteChallenge
Expand Down
34 changes: 34 additions & 0 deletions src/controllers/RankingController.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { getAchievement } from "../utils/AchievementCalculator"
import { Challenge } from "../models";

const getRanking = async function(req, res, next){
const id = req.params.challengeId;
const challenge = await Challenge.findOne({ where: { id: id } });
if (challenge) {
const ranks = await Ranking(challenge);
res.send({ data: {
ranks: ranks
}});
}
else {
next(new NotFoundChallengeError('존재하지 않는 challenge'));
return;
}
}

const Ranking = async (challenge) => {
const baseChallenge = await challenge.getBaseChallenge();
const challenges = await baseChallenge.getChallenges({ include: ['user'] });
const achivements = await Promise.all(challenges.map(async (item) => {
const userAchivement = await getAchievement(item, item.user);
return {
user: item.user,
achivement: userAchivement,
}
}))
return achivements.sort((a, b) => b.achivement - a.achivement)
}

export {
getRanking
}
1 change: 0 additions & 1 deletion src/routes/challenge.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import ChallengeController from '../controllers/ChallengeController';
const router = Router();

router.get('', ChallengeController.getChallenges)
router.get('/:challengeId', ChallengeController.getChallenge)
router.get('/:challengeId/records', ChallengeController.getChallengeRecords)
router.post('', ChallengeController.createChallenge)
router.delete('/:challengeId', ChallengeController.deleteChallenge)
Expand Down
2 changes: 2 additions & 0 deletions src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import user from './user';
import auth from './auth';
import badge from './badge';
import job from './job';
import rank from './rank';

const router = Router();

Expand All @@ -21,5 +22,6 @@ router.use('/challenges', challenge);
router.use('/records', record);
router.use('/user', user);
router.use('/badges', badge);
router.use('/rank', rank);

export default router;
8 changes: 8 additions & 0 deletions src/routes/rank.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Router } from 'express';
import { getRanking } from '../controllers/RankingController';

const router = Router();

router.get('/challenge/:challengeId', getRanking);

export default router;
18 changes: 0 additions & 18 deletions src/utils/Ranking.js

This file was deleted.

0 comments on commit 5ca53a7

Please sign in to comment.