JJuOn

Add routings and Fix errors

...@@ -49,7 +49,7 @@ exports.addChallenge = async (ctx) => { ...@@ -49,7 +49,7 @@ exports.addChallenge = async (ctx) => {
49 ctx.body = result.error; 49 ctx.body = result.error;
50 return; 50 return;
51 } 51 }
52 - const { 52 + let {
53 challengeName, 53 challengeName,
54 startDate, 54 startDate,
55 endDate, 55 endDate,
...@@ -77,14 +77,24 @@ exports.addChallenge = async (ctx) => { ...@@ -77,14 +77,24 @@ exports.addChallenge = async (ctx) => {
77 const newChallenge=await Challenge.findByChallengeName(challengeName); 77 const newChallenge=await Challenge.findByChallengeName(challengeName);
78 const newChallenge_id=newChallenge._id; 78 const newChallenge_id=newChallenge._id;
79 const timeStep=Number(durationPerSession.slice(0,-1)) 79 const timeStep=Number(durationPerSession.slice(0,-1))
80 - for(let s_date=startDate,e_date=s_date;s_date<endDate;){ 80 + if(typeof(startDate)=='string'){
81 + startDate=new Date(startDate);
82 + }
83 + if(typeof(endDate)=='string'){
84 + endDate=new Date(endDate);
85 + }
86 + for(let s_date=new Date(startDate);s_date<endDate;){
87 + let e_date=new Date(s_date);
81 if(durationPerSession[durationPerSession.length-1]==='d'){ 88 if(durationPerSession[durationPerSession.length-1]==='d'){
89 + console.log('day');
82 e_date.setDate(s_date.getDate()+timeStep); 90 e_date.setDate(s_date.getDate()+timeStep);
83 } 91 }
84 else if(durationPerSession[durationPerSession.length-1]==='w'){ 92 else if(durationPerSession[durationPerSession.length-1]==='w'){
93 + console.log('week');
85 e_date.setDate(s_date.getDate()+timeStep*7); 94 e_date.setDate(s_date.getDate()+timeStep*7);
86 } 95 }
87 else if(durationPerSession[durationPerSession.length-1]==='m'){ 96 else if(durationPerSession[durationPerSession.length-1]==='m'){
97 + console.log('month');
88 e_date.setMonth(s_date.getMonth()+timeStep); 98 e_date.setMonth(s_date.getMonth()+timeStep);
89 } 99 }
90 e_date.setMinutes(e_date.getMinutes()-1); 100 e_date.setMinutes(e_date.getMinutes()-1);
...@@ -101,6 +111,7 @@ exports.addChallenge = async (ctx) => { ...@@ -101,6 +111,7 @@ exports.addChallenge = async (ctx) => {
101 else{ 111 else{
102 status="end"; 112 status="end";
103 } 113 }
114 + console.log(`start:${s_date}\nend:${e_date}`);
104 const session=new Session({ 115 const session=new Session({
105 challengeId:newChallenge_id, 116 challengeId:newChallenge_id,
106 sessionStartDate:s_date, 117 sessionStartDate:s_date,
...@@ -108,10 +119,10 @@ exports.addChallenge = async (ctx) => { ...@@ -108,10 +119,10 @@ exports.addChallenge = async (ctx) => {
108 status:status, 119 status:status,
109 }); 120 });
110 await session.save(); 121 await session.save();
111 - s_date=e_date; 122 + s_date=new Date(e_date);
112 s_date.setMinutes(s_date.getMinutes()+1); 123 s_date.setMinutes(s_date.getMinutes()+1);
113 } 124 }
114 - ctx.body = challenge(); 125 + ctx.body = challenge;
115 } catch (e) { 126 } catch (e) {
116 ctx.throw(500, e); 127 ctx.throw(500, e);
117 } 128 }
...@@ -123,7 +134,7 @@ query string status can be in ['all','enrolled','progress','end'] ...@@ -123,7 +134,7 @@ query string status can be in ['all','enrolled','progress','end']
123 */ 134 */
124 exports.list = async (ctx) => { 135 exports.list = async (ctx) => {
125 try{ 136 try{
126 - const status = ctx.qs.status; 137 + const status = ctx.query.status;
127 if (status!=='all'){ 138 if (status!=='all'){
128 const challenges = await Challenge.find({status:status}).select('-_id'); 139 const challenges = await Challenge.find({status:status}).select('-_id');
129 ctx.body = challenges; 140 ctx.body = challenges;
...@@ -151,7 +162,8 @@ exports.participate=async (ctx)=>{ ...@@ -151,7 +162,8 @@ exports.participate=async (ctx)=>{
151 TODO: access token validation, 162 TODO: access token validation,
152 recommend:get username from access_token 163 recommend:get username from access_token
153 */ 164 */
154 - const {username,challengeName}=ctx.body; 165 + console.log(ctx.request.body);
166 + const {username,challengeName}=ctx.request.body;
155 const challenge=await Challenge.findByChallengeName(challengeName); 167 const challenge=await Challenge.findByChallengeName(challengeName);
156 const challenge_id=challenge._id; 168 const challenge_id=challenge._id;
157 const user=await User.findByUsername(username); 169 const user=await User.findByUsername(username);
...@@ -159,9 +171,25 @@ exports.participate=async (ctx)=>{ ...@@ -159,9 +171,25 @@ exports.participate=async (ctx)=>{
159 const newGroup=new Group({ 171 const newGroup=new Group({
160 members:[user_id], 172 members:[user_id],
161 }); 173 });
162 - newGroup.save(); 174 + let newGroup_id=""
175 + await newGroup.save(async (err,product)=>{
176 + if(err){
177 + throw err;
178 + }
179 + newGroup_id=product._id;
180 + const sessions=await Session.findByChallengeId(challenge_id);
181 + sessions.forEach(async (elem) => {
182 + const newParticipation=new Participation({
183 + sessionId:elem._id,
184 + groupId:newGroup_id,
185 + problems:[],
186 + });
187 + await newParticipation.save();
188 + });
189 + });
163 } 190 }
164 catch(e){ 191 catch(e){
192 + console.error(e);
165 ctx.throw(500,e); 193 ctx.throw(500,e);
166 } 194 }
167 }; 195 };
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -4,5 +4,7 @@ const challengeCtrl = require('./challege.ctrl'); ...@@ -4,5 +4,7 @@ const challengeCtrl = require('./challege.ctrl');
4 4
5 challenge.post("/getchallenge",challengeCtrl.getChallenge); 5 challenge.post("/getchallenge",challengeCtrl.getChallenge);
6 challenge.post("/addchallenge",challengeCtrl.addChallenge); 6 challenge.post("/addchallenge",challengeCtrl.addChallenge);
7 +challenge.get("/list",challengeCtrl.list);
8 +challenge.post("/participate",challengeCtrl.participate);
7 9
8 module.exports = challenge; 10 module.exports = challenge;
...\ No newline at end of file ...\ No newline at end of file
......