JJuOn

Edit api/challenge/addchallenge

1 const Challenge = require("../../models/challenge"); 1 const Challenge = require("../../models/challenge");
2 +const Session = require("../../models/session");
3 +const Participation = require("../../models/participation");
4 +const Group = require("../../models/group");
5 +const User = require('../../models/user');
6 +
2 const Joi = require("joi"); 7 const Joi = require("joi");
3 /*POST /api/challenge/getChallenge 8 /*POST /api/challenge/getChallenge
4 { 9 {
...@@ -8,7 +13,7 @@ const Joi = require("joi"); ...@@ -8,7 +13,7 @@ const Joi = require("joi");
8 exports.getChallenge = async (ctx) => { 13 exports.getChallenge = async (ctx) => {
9 try { 14 try {
10 const { challengeName } = ctx.request.body; 15 const { challengeName } = ctx.request.body;
11 - const challenge = await Challenge.findByChallengeName(challengeName); 16 + const challenge = await Challenge.findByChallengeName(challengeName).select('-_id');
12 if (!challenge) { 17 if (!challenge) {
13 ctx.status = 401; 18 ctx.status = 401;
14 return; 19 return;
...@@ -53,7 +58,7 @@ exports.addChallenge = async (ctx) => { ...@@ -53,7 +58,7 @@ exports.addChallenge = async (ctx) => {
53 } = ctx.request.body; 58 } = ctx.request.body;
54 59
55 try { 60 try {
56 - const isChallengeExist = await Challenge.findByChallengeName(challengeName); 61 + const isChallengeExist = await Challenge.findByChallengeName(challengeName).select('-_id');
57 62
58 if (isChallengeExist) { 63 if (isChallengeExist) {
59 ctx.status = 409; 64 ctx.status = 409;
...@@ -68,11 +73,95 @@ exports.addChallenge = async (ctx) => { ...@@ -68,11 +73,95 @@ exports.addChallenge = async (ctx) => {
68 }); 73 });
69 74
70 await challenge.save(); 75 await challenge.save();
76 +
77 + const newChallenge=await Challenge.findByChallengeName(challengeName);
78 + const newChallenge_id=newChallenge._id;
79 + const timeStep=Number(durationPerSession.slice(0,-1))
80 + for(let s_date=startDate,e_date=s_date;s_date<endDate;){
81 + if(durationPerSession[durationPerSession.length-1]==='d'){
82 + e_date.setDate(s_date.getDate()+timeStep);
83 + }
84 + else if(durationPerSession[durationPerSession.length-1]==='w'){
85 + e_date.setDate(s_date.getDate()+timeStep*7);
86 + }
87 + else if(durationPerSession[durationPerSession.length-1]==='m'){
88 + e_date.setMonth(s_date.getMonth()+timeStep);
89 + }
90 + e_date.setMinutes(e_date.getMinutes()-1);
91 + if(e_date>endDate){
92 + break;
93 + }
94 + let status="";
95 + if (s_date>new Date()){
96 + status="enrolled";
97 + }
98 + else if (s_date<=new Date() && new Date() <= e_date){
99 + status="progress";
100 + }
101 + else{
102 + status="end";
103 + }
104 + const session=new Session({
105 + challengeId:newChallenge_id,
106 + sessionStartDate:s_date,
107 + sessionEndDate:e_date,
108 + status:status,
109 + });
110 + await session.save();
111 + s_date=e_date;
112 + s_date.setMinutes(s_date.getMinutes()+1);
113 + }
71 ctx.body = challenge(); 114 ctx.body = challenge();
72 } catch (e) { 115 } catch (e) {
73 ctx.throw(500, e); 116 ctx.throw(500, e);
74 } 117 }
118 +};
119 +
120 +
121 +/* GET /api/challenge/list?status
122 +query string status can be in ['all','enrolled','progress','end']
123 +*/
124 +exports.list = async (ctx) => {
125 + try{
126 + const status = ctx.qs.status;
127 + if (status!=='all'){
128 + const challenges = await Challenge.find({status:status}).select('-_id');
129 + ctx.body = challenges;
130 + }
131 + else {
132 + const challenges = await Challenge.find({}).select('-_id');
133 + ctx.body = challenges;
134 + }
135 + }
136 + catch(e){
137 + ctx.throw(500,e);
138 + }
139 +};
140 +
141 +/* POST /api/challenge/participate
142 +{
143 + username: 'username',
144 + challengeName: 'challengename'
145 +}
146 +*/
147 +
148 +exports.participate=async (ctx)=>{
149 + try{
75 /* 150 /*
76 - TODO: How to handle group? 151 + TODO: access token validation,
152 + recommend:get username from access_token
77 */ 153 */
78 -}; 154 + const {username,challengeName}=ctx.body;
155 + const challenge=await Challenge.findByChallengeName(challengeName);
156 + const challenge_id=challenge._id;
157 + const user=await User.findByUsername(username);
158 + const user_id=user._id;
159 + const newGroup=new Group({
160 + members:[user_id],
161 + });
162 + newGroup.save();
163 + }
164 + catch(e){
165 + ctx.throw(500,e);
166 + }
167 +};
...\ No newline at end of file ...\ No newline at end of file
......