송용우

Fixed Problems of api/challenge/list

......@@ -2,7 +2,7 @@ const Challenge = require("../../models/challenge");
const Session = require("../../models/session");
const Participation = require("../../models/participation");
const Group = require("../../models/group");
const User = require('../../models/user');
const User = require("../../models/user");
const Joi = require("joi");
/*POST /api/challenge/getChallenge
......@@ -39,7 +39,7 @@ exports.addChallenge = async (ctx) => {
startDate: Joi.date(),
endDate: Joi.date(),
durationPerSession: Joi.string(),
goalPerSession: Joi.number()
goalPerSession: Joi.number(),
})
.unknown();
const result = Joi.validate(ctx.request.body, schema);
......@@ -73,54 +73,50 @@ exports.addChallenge = async (ctx) => {
});
await challenge.save();
const newChallenge=await Challenge.findByChallengeName(challengeName);
const newChallenge_id=newChallenge._id;
const timeStep=Number(durationPerSession.slice(0,-1))
if(typeof(startDate)=='string'){
startDate=new Date(startDate);
const newChallenge = await Challenge.findByChallengeName(challengeName);
const newChallenge_id = newChallenge._id;
const timeStep = Number(durationPerSession.slice(0, -1));
if (typeof startDate == "string") {
startDate = new Date(startDate);
}
if(typeof(endDate)=='string'){
endDate=new Date(endDate);
if (typeof endDate == "string") {
endDate = new Date(endDate);
}
for(let s_date=new Date(startDate);s_date<endDate;){
let e_date=new Date(s_date);
if(durationPerSession[durationPerSession.length-1]==='d'){
console.log('day');
e_date.setDate(s_date.getDate()+timeStep);
}
else if(durationPerSession[durationPerSession.length-1]==='w'){
console.log('week');
e_date.setDate(s_date.getDate()+timeStep*7);
}
else if(durationPerSession[durationPerSession.length-1]==='m'){
console.log('month');
e_date.setMonth(s_date.getMonth()+timeStep);
for (let s_date = new Date(startDate); s_date < endDate; ) {
let e_date = new Date(s_date);
if (durationPerSession[durationPerSession.length - 1] === "d") {
console.log("day");
e_date.setDate(s_date.getDate() + timeStep);
} else if (durationPerSession[durationPerSession.length - 1] === "w") {
console.log("week");
e_date.setDate(s_date.getDate() + timeStep * 7);
} else if (durationPerSession[durationPerSession.length - 1] === "m") {
console.log("month");
e_date.setMonth(s_date.getMonth() + timeStep);
}
e_date.setMinutes(e_date.getMinutes()-1);
if(e_date>endDate){
e_date.setMinutes(e_date.getMinutes() - 1);
if (e_date > endDate) {
break;
}
let status="";
if (s_date>new Date()){
status="enrolled";
}
else if (s_date<=new Date() && new Date() <= e_date){
status="progress";
}
else{
status="end";
let status = "";
if (s_date > new Date()) {
status = "enrolled";
} else if (s_date <= new Date() && new Date() <= e_date) {
status = "progress";
} else {
status = "end";
}
console.log(`start:${s_date}\nend:${e_date}`);
const session=new Session({
challengeId:newChallenge_id,
sessionStartDate:s_date,
sessionEndDate:e_date,
status:status,
const session = new Session({
challengeId: newChallenge_id,
sessionStartDate: s_date,
sessionEndDate: e_date,
status: status,
});
await session.save();
s_date=new Date(e_date);
s_date.setMinutes(s_date.getMinutes()+1);
s_date = new Date(e_date);
s_date.setMinutes(s_date.getMinutes() + 1);
}
ctx.body = challenge.serialize();
} catch (e) {
......@@ -128,24 +124,21 @@ exports.addChallenge = async (ctx) => {
}
};
/* GET /api/challenge/list/:status
parameter status can be in ['all','enrolled','progress','end']
*/
exports.list = async (ctx) => {
try{
try {
const status = ctx.params.status;
if (status!=='all'){
const challenges = await Challenge.find({status:status});
ctx.body = challenges.serialize();
}
else {
if (status !== "all") {
const challenges = await Challenge.find({ status: status });
ctx.body = challenges;
} else {
const challenges = await Challenge.find({});
ctx.body = challenges.serialize();
ctx.body = challenges;
}
}
catch(e){
ctx.throw(500,e);
} catch (e) {
ctx.throw(500, e);
}
};
......@@ -156,42 +149,41 @@ exports.list = async (ctx) => {
}
*/
exports.participate=async (ctx)=>{
try{
/*
exports.participate = async (ctx) => {
try {
/*
TODO: access token validation,
recommend:get username from access_token
*/
console.log(ctx.request.body);
const {username,challengeName}=ctx.request.body;
const challenge=await Challenge.findByChallengeName(challengeName);
const challenge_id=challenge._id;
const user=await User.findByUsername(username);
const user_id=user._id;
const newGroup=new Group({
groupName:`${user.username}${challengeName} 그룹`,
members:[user_id],
const { username, challengeName } = ctx.request.body;
const challenge = await Challenge.findByChallengeName(challengeName);
const challenge_id = challenge._id;
const user = await User.findByUsername(username);
const user_id = user._id;
const newGroup = new Group({
groupName: `${user.username}${challengeName} 그룹`,
members: [user_id],
});
let newGroup_id=""
await newGroup.save(async (err,product)=>{
if(err){
let newGroup_id = "";
await newGroup.save(async (err, product) => {
if (err) {
throw err;
}
newGroup_id=product._id;
const sessions=await Session.findByChallengeId(challenge_id);
newGroup_id = product._id;
const sessions = await Session.findByChallengeId(challenge_id);
sessions.forEach(async (elem) => {
const newParticipation=new Participation({
sessionId:elem._id,
groupId:newGroup_id,
problems:[],
const newParticipation = new Participation({
sessionId: elem._id,
groupId: newGroup_id,
problems: [],
});
await newParticipation.save();
ctx.body=newParticipation.serialize();
ctx.body = newParticipation.serialize();
});
});
}
catch(e){
} catch (e) {
console.error(e);
ctx.throw(500,e);
ctx.throw(500, e);
}
};
\ No newline at end of file
};
......
......@@ -2,49 +2,52 @@ const mongoose = require("mongoose");
const { Schema } = mongoose;
const ChallengeSchema=new Schema({
challengeName: {type: String, required: true},
startDate: {type: Object, required: true},
endDate: {type: Object, required: true},
durationPerSession: {type: String, required: true}, // '1d' means one day per session, '2w' means 2 weeks per session, '3m' means 3 months per session.
goalPerSession: {type: Number, required:true}, // number of problems for one session
status: { type: String }
},{
collection: 'challenge'
});
ChallengeSchema.statics.findByChallengeName=function(challengeName){
return this.findOne({challengeName:challengeName});
}
ChallengeSchema.methods.getChallengeName=function(){
return this.challengeName;
}
ChallengeSchema.methods.getStartDate=function(){
return this.startDate;
}
ChallengeSchema.methods.getEndDate=function(){
return this.endDate;
}
ChallengeSchema.method.getDurationPerSession=function(){
return this.durationPerSession;
}
ChallengeSchema.methods.getGoalPerSession=function(){
return this.goalPerSession;
}
ChallengeSchema.methods.getStatus=function(){
return this.status;
}
ChallengeSchema.methods.serialize=function(){
let challengeJSON = this.toJSON();
return challengeJSON;
}
const Challenge = mongoose.model('Challenge', ChallengeSchema);
module.exports = Challenge;
\ No newline at end of file
const ChallengeSchema = new Schema(
{
challengeName: { type: String, required: true },
startDate: { type: Object, required: true },
endDate: { type: Object, required: true },
durationPerSession: { type: String, required: true }, // '1d' means one day per session, '2w' means 2 weeks per session, '3m' means 3 months per session.
goalPerSession: { type: Number, required: true }, // number of problems for one session
status: { type: String },
},
{
collection: "challenge",
}
);
ChallengeSchema.statics.findByChallengeName = function (challengeName) {
return this.findOne({ challengeName: challengeName });
};
ChallengeSchema.methods.getChallengeName = function () {
return this.challengeName;
};
ChallengeSchema.methods.getStartDate = function () {
return this.startDate;
};
ChallengeSchema.methods.getEndDate = function () {
return this.endDate;
};
ChallengeSchema.methods.getDurationPerSession = function () {
return this.durationPerSession;
};
ChallengeSchema.methods.getGoalPerSession = function () {
return this.goalPerSession;
};
ChallengeSchema.methods.getStatus = function () {
return this.status;
};
ChallengeSchema.methods.serialize = function () {
let challengeJSON = this.toJSON();
return challengeJSON;
};
const Challenge = mongoose.model("Challenge", ChallengeSchema);
module.exports = Challenge;
......