JJuOn

Implement GET /api/chalenge/getchallenge

...@@ -10,7 +10,7 @@ const Joi = require("joi"); ...@@ -10,7 +10,7 @@ const Joi = require("joi");
10 challengeName: "challengeName" 10 challengeName: "challengeName"
11 } 11 }
12 */ 12 */
13 -exports.getChallenge = async (ctx) => { 13 +exports.getChallengePOST = async (ctx) => {
14 try { 14 try {
15 const { challengeName } = ctx.request.body; 15 const { challengeName } = ctx.request.body;
16 const challenge = await Challenge.findByChallengeName(challengeName); 16 const challenge = await Challenge.findByChallengeName(challengeName);
...@@ -189,3 +189,36 @@ exports.participate = async (ctx) => { ...@@ -189,3 +189,36 @@ exports.participate = async (ctx) => {
189 ctx.throw(500, e); 189 ctx.throw(500, e);
190 } 190 }
191 }; 191 };
192 +
193 +/*
194 +GET /api/challenge/getchallenge?username
195 +*/
196 +exports.getChallengeGET = async (ctx)=>{
197 + try{
198 + const {username} = ctx.request.query;
199 + const user = await User.findByUsername(username);
200 + const user_id=user._id;
201 + const groups = await Group.find();
202 + const userIncludedGroups = [];
203 + for(let i=0;i<groups.length;i++){
204 + if(groups[i].members.includes(user_id)){
205 + userIncludedGroups.push(groups[i]);
206 + }
207 + }
208 + const challengeList = [];
209 + for(let i=0;i<userIncludedGroups.length;i++){
210 + const participations = await Participation.findByGroupId(userIncludedGroups[i]._id);
211 + for(let j=0;j<participations.length;j++){
212 + const session = await Session.findById(participations[j].sessionId);
213 + const challenge = await Challenge.findById(session.challengeId);
214 + if(!challengeList.includes(challenge)){
215 + challengeList.push(challenge);
216 + }
217 + }
218 + }
219 + ctx.body = challengeList.map(c=>c.serialize());
220 + }
221 + catch(e){
222 + ctx.throw(500,e);
223 + }
224 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -2,9 +2,10 @@ const Router = require('koa-router'); ...@@ -2,9 +2,10 @@ const Router = require('koa-router');
2 const challenge = new Router(); 2 const challenge = new Router();
3 const challengeCtrl = require('./challege.ctrl'); 3 const challengeCtrl = require('./challege.ctrl');
4 4
5 -challenge.post("/getchallenge",challengeCtrl.getChallenge); 5 +challenge.post("/getchallenge",challengeCtrl.getChallengePOST);
6 challenge.post("/addchallenge",challengeCtrl.addChallenge); 6 challenge.post("/addchallenge",challengeCtrl.addChallenge);
7 challenge.get("/list/:status",challengeCtrl.list); 7 challenge.get("/list/:status",challengeCtrl.list);
8 challenge.post("/participate",challengeCtrl.participate); 8 challenge.post("/participate",challengeCtrl.participate);
9 +challenge.get("/getchallenge",challengeCtrl.getChallengeGET);
9 10
10 module.exports = challenge; 11 module.exports = challenge;
...\ No newline at end of file ...\ No newline at end of file
......