JuWon Seo
Committed by GitHub

Merge pull request #28 from FacerAin/feature/rest_api

Feature/rest api
......@@ -132,10 +132,10 @@ exports.list = async (ctx) => {
const status = ctx.params.status;
if (status !== "all") {
const challenges = await Challenge.find({ status: status });
ctx.body = challenges.serialize();
ctx.body = challenges;
} else {
const challenges = await Challenge.find({});
ctx.body = challenges.serialize();
ctx.body = challenges;
}
} catch (e) {
ctx.throw(500, e);
......
const Participation = require("../../models/participation");
const Session = require("../../models/session");
const Group = require("../../models/group");
const User = require("../../models/user");
const mongoose = require("mongoose");
const {ObjectId} = mongoose.Types;
/* POST /api/session/createproblem/:how
{
problemList:[Number]
sessionId: ObjectId,
groupId: ObjectId,
problemList:[Number],
}
*/
exports.createProblem = async (ctx)=>{
try{
let {sessionId,groupId,problemList} = ctx.request.body;
if(typeof(sessionId)==='string'){
sessionId=new ObjectId(sessionId);
}
if(typeof(groupId)==='string'){
groupId=new ObjectId(groupId);
}
if(typeof(problemList)==='string'){
problemList=JSON.parse(problemList);
}
const participation = await Participation.findOne({sessionId:sessionId,groupId:groupId});
const group = await Group.findById(groupId);
const how=ctx.params.how;
if(how==='self'){
let check = true;
for(let i=0;i<group.members.length;i++){
const user = await User.findById(group.members[i]);
console.log(user);
console.log(typeof(user.solvedBJ_date.solvedBJbyDATE));
let userProblemList = [];
for(let key in user.solvedBJ_date.solvedBJbyDATE){
userProblemList.push(user.solvedBJ_date.solvedBJbyDATE[key]);
}
userProblemList=userProblemList.flat().map(elem=>elem.problem_number);
for(let j=0;j<problemList.length;j++){
if(userProblemList.includes(problemList[j])){
check = false;
break;
}
}
}
if(!check){
ctx.throw('그룹원이 이미 푼 문제는 등록할 수 없습니다.');
return;
}
else{
problemList.map(async problemNum=>await participation.addProblem({problemNum:problemNum,isSolved:false}));
ctx.body=participation.serialize();
}
}
else if(how==='recommend'){
//TODO
}
}
catch(e){
......
......@@ -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(){
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(){
ChallengeSchema.methods.getStartDate = function () {
return this.startDate;
}
};
ChallengeSchema.methods.getEndDate=function(){
ChallengeSchema.methods.getEndDate = function () {
return this.endDate;
}
};
ChallengeSchema.method.getDurationPerSession=function(){
ChallengeSchema.methods.getDurationPerSession = function () {
return this.durationPerSession;
}
};
ChallengeSchema.methods.getGoalPerSession=function(){
ChallengeSchema.methods.getGoalPerSession = function () {
return this.goalPerSession;
}
};
ChallengeSchema.methods.getStatus=function(){
ChallengeSchema.methods.getStatus = function () {
return this.status;
}
};
ChallengeSchema.methods.serialize=function(){
ChallengeSchema.methods.serialize = function () {
let challengeJSON = this.toJSON();
return challengeJSON;
}
};
const Challenge = mongoose.model('Challenge', ChallengeSchema);
const Challenge = mongoose.model("Challenge", ChallengeSchema);
module.exports = Challenge;
......