JJuOn

Implement GET /api/chalenge/getchallenge

......@@ -10,7 +10,7 @@ const Joi = require("joi");
challengeName: "challengeName"
}
*/
exports.getChallenge = async (ctx) => {
exports.getChallengePOST = async (ctx) => {
try {
const { challengeName } = ctx.request.body;
const challenge = await Challenge.findByChallengeName(challengeName);
......@@ -189,3 +189,36 @@ exports.participate = async (ctx) => {
ctx.throw(500, e);
}
};
/*
GET /api/challenge/getchallenge?username
*/
exports.getChallengeGET = async (ctx)=>{
try{
const {username} = ctx.request.query;
const user = await User.findByUsername(username);
const user_id=user._id;
const groups = await Group.find();
const userIncludedGroups = [];
for(let i=0;i<groups.length;i++){
if(groups[i].members.includes(user_id)){
userIncludedGroups.push(groups[i]);
}
}
const challengeList = [];
for(let i=0;i<userIncludedGroups.length;i++){
const participations = await Participation.findByGroupId(userIncludedGroups[i]._id);
for(let j=0;j<participations.length;j++){
const session = await Session.findById(participations[j].sessionId);
const challenge = await Challenge.findById(session.challengeId);
if(!challengeList.includes(challenge)){
challengeList.push(challenge);
}
}
}
ctx.body = challengeList.map(c=>c.serialize());
}
catch(e){
ctx.throw(500,e);
}
}
\ No newline at end of file
......
......@@ -2,9 +2,10 @@ const Router = require('koa-router');
const challenge = new Router();
const challengeCtrl = require('./challege.ctrl');
challenge.post("/getchallenge",challengeCtrl.getChallenge);
challenge.post("/getchallenge",challengeCtrl.getChallengePOST);
challenge.post("/addchallenge",challengeCtrl.addChallenge);
challenge.get("/list/:status",challengeCtrl.list);
challenge.post("/participate",challengeCtrl.participate);
challenge.get("/getchallenge",challengeCtrl.getChallengeGET);
module.exports = challenge;
\ No newline at end of file
......