Showing
2 changed files
with
118 additions
and
123 deletions
... | @@ -2,7 +2,7 @@ const Challenge = require("../../models/challenge"); | ... | @@ -2,7 +2,7 @@ const Challenge = require("../../models/challenge"); |
2 | const Session = require("../../models/session"); | 2 | const Session = require("../../models/session"); |
3 | const Participation = require("../../models/participation"); | 3 | const Participation = require("../../models/participation"); |
4 | const Group = require("../../models/group"); | 4 | const Group = require("../../models/group"); |
5 | -const User = require('../../models/user'); | 5 | +const User = require("../../models/user"); |
6 | 6 | ||
7 | const Joi = require("joi"); | 7 | const Joi = require("joi"); |
8 | /*POST /api/challenge/getChallenge | 8 | /*POST /api/challenge/getChallenge |
... | @@ -39,7 +39,7 @@ exports.addChallenge = async (ctx) => { | ... | @@ -39,7 +39,7 @@ exports.addChallenge = async (ctx) => { |
39 | startDate: Joi.date(), | 39 | startDate: Joi.date(), |
40 | endDate: Joi.date(), | 40 | endDate: Joi.date(), |
41 | durationPerSession: Joi.string(), | 41 | durationPerSession: Joi.string(), |
42 | - goalPerSession: Joi.number() | 42 | + goalPerSession: Joi.number(), |
43 | }) | 43 | }) |
44 | .unknown(); | 44 | .unknown(); |
45 | const result = Joi.validate(ctx.request.body, schema); | 45 | const result = Joi.validate(ctx.request.body, schema); |
... | @@ -73,54 +73,50 @@ exports.addChallenge = async (ctx) => { | ... | @@ -73,54 +73,50 @@ exports.addChallenge = async (ctx) => { |
73 | }); | 73 | }); |
74 | 74 | ||
75 | await challenge.save(); | 75 | await challenge.save(); |
76 | - | 76 | + |
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 | - if(typeof(startDate)=='string'){ | 80 | + if (typeof startDate == "string") { |
81 | - startDate=new Date(startDate); | 81 | + startDate = new Date(startDate); |
82 | } | 82 | } |
83 | - if(typeof(endDate)=='string'){ | 83 | + if (typeof endDate == "string") { |
84 | - endDate=new Date(endDate); | 84 | + endDate = new Date(endDate); |
85 | } | 85 | } |
86 | - for(let s_date=new Date(startDate);s_date<endDate;){ | 86 | + for (let s_date = new Date(startDate); s_date < endDate; ) { |
87 | - let e_date=new Date(s_date); | 87 | + let e_date = new Date(s_date); |
88 | - if(durationPerSession[durationPerSession.length-1]==='d'){ | 88 | + if (durationPerSession[durationPerSession.length - 1] === "d") { |
89 | - console.log('day'); | 89 | + console.log("day"); |
90 | - e_date.setDate(s_date.getDate()+timeStep); | 90 | + e_date.setDate(s_date.getDate() + timeStep); |
91 | - } | 91 | + } else if (durationPerSession[durationPerSession.length - 1] === "w") { |
92 | - else if(durationPerSession[durationPerSession.length-1]==='w'){ | 92 | + console.log("week"); |
93 | - console.log('week'); | 93 | + e_date.setDate(s_date.getDate() + timeStep * 7); |
94 | - e_date.setDate(s_date.getDate()+timeStep*7); | 94 | + } else if (durationPerSession[durationPerSession.length - 1] === "m") { |
95 | - } | 95 | + console.log("month"); |
96 | - else if(durationPerSession[durationPerSession.length-1]==='m'){ | 96 | + e_date.setMonth(s_date.getMonth() + timeStep); |
97 | - console.log('month'); | ||
98 | - e_date.setMonth(s_date.getMonth()+timeStep); | ||
99 | } | 97 | } |
100 | - e_date.setMinutes(e_date.getMinutes()-1); | 98 | + e_date.setMinutes(e_date.getMinutes() - 1); |
101 | - if(e_date>endDate){ | 99 | + if (e_date > endDate) { |
102 | break; | 100 | break; |
103 | } | 101 | } |
104 | - let status=""; | 102 | + let status = ""; |
105 | - if (s_date>new Date()){ | 103 | + if (s_date > new Date()) { |
106 | - status="enrolled"; | 104 | + status = "enrolled"; |
107 | - } | 105 | + } else if (s_date <= new Date() && new Date() <= e_date) { |
108 | - else if (s_date<=new Date() && new Date() <= e_date){ | 106 | + status = "progress"; |
109 | - status="progress"; | 107 | + } else { |
110 | - } | 108 | + status = "end"; |
111 | - else{ | ||
112 | - status="end"; | ||
113 | } | 109 | } |
114 | console.log(`start:${s_date}\nend:${e_date}`); | 110 | console.log(`start:${s_date}\nend:${e_date}`); |
115 | - const session=new Session({ | 111 | + const session = new Session({ |
116 | - challengeId:newChallenge_id, | 112 | + challengeId: newChallenge_id, |
117 | - sessionStartDate:s_date, | 113 | + sessionStartDate: s_date, |
118 | - sessionEndDate:e_date, | 114 | + sessionEndDate: e_date, |
119 | - status:status, | 115 | + status: status, |
120 | }); | 116 | }); |
121 | await session.save(); | 117 | await session.save(); |
122 | - s_date=new Date(e_date); | 118 | + s_date = new Date(e_date); |
123 | - s_date.setMinutes(s_date.getMinutes()+1); | 119 | + s_date.setMinutes(s_date.getMinutes() + 1); |
124 | } | 120 | } |
125 | ctx.body = challenge.serialize(); | 121 | ctx.body = challenge.serialize(); |
126 | } catch (e) { | 122 | } catch (e) { |
... | @@ -128,24 +124,21 @@ exports.addChallenge = async (ctx) => { | ... | @@ -128,24 +124,21 @@ exports.addChallenge = async (ctx) => { |
128 | } | 124 | } |
129 | }; | 125 | }; |
130 | 126 | ||
131 | - | ||
132 | /* GET /api/challenge/list/:status | 127 | /* GET /api/challenge/list/:status |
133 | parameter status can be in ['all','enrolled','progress','end'] | 128 | parameter status can be in ['all','enrolled','progress','end'] |
134 | */ | 129 | */ |
135 | exports.list = async (ctx) => { | 130 | exports.list = async (ctx) => { |
136 | - try{ | 131 | + try { |
137 | const status = ctx.params.status; | 132 | const status = ctx.params.status; |
138 | - if (status!=='all'){ | 133 | + if (status !== "all") { |
139 | - const challenges = await Challenge.find({status:status}); | 134 | + const challenges = await Challenge.find({ status: status }); |
140 | - ctx.body = challenges.serialize(); | 135 | + ctx.body = challenges; |
141 | - } | 136 | + } else { |
142 | - else { | ||
143 | const challenges = await Challenge.find({}); | 137 | const challenges = await Challenge.find({}); |
144 | - ctx.body = challenges.serialize(); | 138 | + ctx.body = challenges; |
145 | } | 139 | } |
146 | - } | 140 | + } catch (e) { |
147 | - catch(e){ | 141 | + ctx.throw(500, e); |
148 | - ctx.throw(500,e); | ||
149 | } | 142 | } |
150 | }; | 143 | }; |
151 | 144 | ||
... | @@ -156,42 +149,41 @@ exports.list = async (ctx) => { | ... | @@ -156,42 +149,41 @@ exports.list = async (ctx) => { |
156 | } | 149 | } |
157 | */ | 150 | */ |
158 | 151 | ||
159 | -exports.participate=async (ctx)=>{ | 152 | +exports.participate = async (ctx) => { |
160 | - try{ | 153 | + try { |
161 | - /* | 154 | + /* |
162 | TODO: access token validation, | 155 | TODO: access token validation, |
163 | recommend:get username from access_token | 156 | recommend:get username from access_token |
164 | */ | 157 | */ |
165 | console.log(ctx.request.body); | 158 | console.log(ctx.request.body); |
166 | - const {username,challengeName}=ctx.request.body; | 159 | + const { username, challengeName } = ctx.request.body; |
167 | - const challenge=await Challenge.findByChallengeName(challengeName); | 160 | + const challenge = await Challenge.findByChallengeName(challengeName); |
168 | - const challenge_id=challenge._id; | 161 | + const challenge_id = challenge._id; |
169 | - const user=await User.findByUsername(username); | 162 | + const user = await User.findByUsername(username); |
170 | - const user_id=user._id; | 163 | + const user_id = user._id; |
171 | - const newGroup=new Group({ | 164 | + const newGroup = new Group({ |
172 | - groupName:`${user.username}의 ${challengeName} 그룹`, | 165 | + groupName: `${user.username}의 ${challengeName} 그룹`, |
173 | - members:[user_id], | 166 | + members: [user_id], |
174 | }); | 167 | }); |
175 | - let newGroup_id="" | 168 | + let newGroup_id = ""; |
176 | - await newGroup.save(async (err,product)=>{ | 169 | + await newGroup.save(async (err, product) => { |
177 | - if(err){ | 170 | + if (err) { |
178 | throw err; | 171 | throw err; |
179 | } | 172 | } |
180 | - newGroup_id=product._id; | 173 | + newGroup_id = product._id; |
181 | - const sessions=await Session.findByChallengeId(challenge_id); | 174 | + const sessions = await Session.findByChallengeId(challenge_id); |
182 | sessions.forEach(async (elem) => { | 175 | sessions.forEach(async (elem) => { |
183 | - const newParticipation=new Participation({ | 176 | + const newParticipation = new Participation({ |
184 | - sessionId:elem._id, | 177 | + sessionId: elem._id, |
185 | - groupId:newGroup_id, | 178 | + groupId: newGroup_id, |
186 | - problems:[], | 179 | + problems: [], |
187 | }); | 180 | }); |
188 | await newParticipation.save(); | 181 | await newParticipation.save(); |
189 | - ctx.body=newParticipation.serialize(); | 182 | + ctx.body = newParticipation.serialize(); |
190 | }); | 183 | }); |
191 | }); | 184 | }); |
192 | - } | 185 | + } catch (e) { |
193 | - catch(e){ | ||
194 | console.error(e); | 186 | console.error(e); |
195 | - ctx.throw(500,e); | 187 | + ctx.throw(500, e); |
196 | } | 188 | } |
197 | -}; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
189 | +}; | ... | ... |
... | @@ -2,49 +2,52 @@ const mongoose = require("mongoose"); | ... | @@ -2,49 +2,52 @@ const mongoose = require("mongoose"); |
2 | 2 | ||
3 | const { Schema } = mongoose; | 3 | const { Schema } = mongoose; |
4 | 4 | ||
5 | -const ChallengeSchema=new Schema({ | ||
6 | - challengeName: {type: String, required: true}, | ||
7 | - startDate: {type: Object, required: true}, | ||
8 | - endDate: {type: Object, required: true}, | ||
9 | - durationPerSession: {type: String, required: true}, // '1d' means one day per session, '2w' means 2 weeks per session, '3m' means 3 months per session. | ||
10 | - goalPerSession: {type: Number, required:true}, // number of problems for one session | ||
11 | - status: { type: String } | ||
12 | -},{ | ||
13 | - collection: 'challenge' | ||
14 | -}); | ||
15 | - | ||
16 | -ChallengeSchema.statics.findByChallengeName=function(challengeName){ | ||
17 | - return this.findOne({challengeName:challengeName}); | ||
18 | -} | ||
19 | - | ||
20 | -ChallengeSchema.methods.getChallengeName=function(){ | ||
21 | - return this.challengeName; | ||
22 | -} | ||
23 | - | ||
24 | -ChallengeSchema.methods.getStartDate=function(){ | ||
25 | - return this.startDate; | ||
26 | -} | ||
27 | - | ||
28 | -ChallengeSchema.methods.getEndDate=function(){ | ||
29 | - return this.endDate; | ||
30 | -} | ||
31 | - | ||
32 | -ChallengeSchema.method.getDurationPerSession=function(){ | ||
33 | - return this.durationPerSession; | ||
34 | -} | ||
35 | - | ||
36 | -ChallengeSchema.methods.getGoalPerSession=function(){ | ||
37 | - return this.goalPerSession; | ||
38 | -} | ||
39 | - | ||
40 | -ChallengeSchema.methods.getStatus=function(){ | ||
41 | - return this.status; | ||
42 | -} | ||
43 | - | ||
44 | -ChallengeSchema.methods.serialize=function(){ | ||
45 | - let challengeJSON = this.toJSON(); | ||
46 | - return challengeJSON; | ||
47 | -} | ||
48 | - | ||
49 | -const Challenge = mongoose.model('Challenge', ChallengeSchema); | ||
50 | -module.exports = Challenge; | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
5 | +const ChallengeSchema = new Schema( | ||
6 | + { | ||
7 | + challengeName: { type: String, required: true }, | ||
8 | + startDate: { type: Object, required: true }, | ||
9 | + endDate: { type: Object, required: true }, | ||
10 | + durationPerSession: { type: String, required: true }, // '1d' means one day per session, '2w' means 2 weeks per session, '3m' means 3 months per session. | ||
11 | + goalPerSession: { type: Number, required: true }, // number of problems for one session | ||
12 | + status: { type: String }, | ||
13 | + }, | ||
14 | + { | ||
15 | + collection: "challenge", | ||
16 | + } | ||
17 | +); | ||
18 | + | ||
19 | +ChallengeSchema.statics.findByChallengeName = function (challengeName) { | ||
20 | + return this.findOne({ challengeName: challengeName }); | ||
21 | +}; | ||
22 | + | ||
23 | +ChallengeSchema.methods.getChallengeName = function () { | ||
24 | + return this.challengeName; | ||
25 | +}; | ||
26 | + | ||
27 | +ChallengeSchema.methods.getStartDate = function () { | ||
28 | + return this.startDate; | ||
29 | +}; | ||
30 | + | ||
31 | +ChallengeSchema.methods.getEndDate = function () { | ||
32 | + return this.endDate; | ||
33 | +}; | ||
34 | + | ||
35 | +ChallengeSchema.methods.getDurationPerSession = function () { | ||
36 | + return this.durationPerSession; | ||
37 | +}; | ||
38 | + | ||
39 | +ChallengeSchema.methods.getGoalPerSession = function () { | ||
40 | + return this.goalPerSession; | ||
41 | +}; | ||
42 | + | ||
43 | +ChallengeSchema.methods.getStatus = function () { | ||
44 | + return this.status; | ||
45 | +}; | ||
46 | + | ||
47 | +ChallengeSchema.methods.serialize = function () { | ||
48 | + let challengeJSON = this.toJSON(); | ||
49 | + return challengeJSON; | ||
50 | +}; | ||
51 | + | ||
52 | +const Challenge = mongoose.model("Challenge", ChallengeSchema); | ||
53 | +module.exports = Challenge; | ... | ... |
-
Please register or login to post a comment