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) => { ...@@ -132,10 +132,10 @@ exports.list = async (ctx) => {
132 const status = ctx.params.status; 132 const status = ctx.params.status;
133 if (status !== "all") { 133 if (status !== "all") {
134 const challenges = await Challenge.find({ status: status }); 134 const challenges = await Challenge.find({ status: status });
135 - ctx.body = challenges.serialize(); 135 + ctx.body = challenges;
136 } else { 136 } else {
137 const challenges = await Challenge.find({}); 137 const challenges = await Challenge.find({});
138 - ctx.body = challenges.serialize(); 138 + ctx.body = challenges;
139 } 139 }
140 } catch (e) { 140 } catch (e) {
141 ctx.throw(500, e); 141 ctx.throw(500, e);
......
1 +const Participation = require("../../models/participation");
2 +const Session = require("../../models/session");
3 +const Group = require("../../models/group");
4 +const User = require("../../models/user");
5 +const mongoose = require("mongoose");
6 +
7 +const {ObjectId} = mongoose.Types;
8 +
1 /* POST /api/session/createproblem/:how 9 /* POST /api/session/createproblem/:how
2 -{ 10 +{
3 - problemList:[Number] 11 + sessionId: ObjectId,
12 + groupId: ObjectId,
13 + problemList:[Number],
4 } 14 }
5 */ 15 */
6 exports.createProblem = async (ctx)=>{ 16 exports.createProblem = async (ctx)=>{
7 try{ 17 try{
18 + let {sessionId,groupId,problemList} = ctx.request.body;
19 + if(typeof(sessionId)==='string'){
20 + sessionId=new ObjectId(sessionId);
21 + }
22 + if(typeof(groupId)==='string'){
23 + groupId=new ObjectId(groupId);
24 + }
25 + if(typeof(problemList)==='string'){
26 + problemList=JSON.parse(problemList);
27 + }
28 + const participation = await Participation.findOne({sessionId:sessionId,groupId:groupId});
29 + const group = await Group.findById(groupId);
8 const how=ctx.params.how; 30 const how=ctx.params.how;
9 if(how==='self'){ 31 if(how==='self'){
10 - 32 + let check = true;
33 + for(let i=0;i<group.members.length;i++){
34 + const user = await User.findById(group.members[i]);
35 + console.log(user);
36 + console.log(typeof(user.solvedBJ_date.solvedBJbyDATE));
37 + let userProblemList = [];
38 + for(let key in user.solvedBJ_date.solvedBJbyDATE){
39 + userProblemList.push(user.solvedBJ_date.solvedBJbyDATE[key]);
40 + }
41 + userProblemList=userProblemList.flat().map(elem=>elem.problem_number);
42 + for(let j=0;j<problemList.length;j++){
43 + if(userProblemList.includes(problemList[j])){
44 + check = false;
45 + break;
46 + }
47 + }
48 + }
49 + if(!check){
50 + ctx.throw('그룹원이 이미 푼 문제는 등록할 수 없습니다.');
51 + return;
52 + }
53 + else{
54 + problemList.map(async problemNum=>await participation.addProblem({problemNum:problemNum,isSolved:false}));
55 + ctx.body=participation.serialize();
56 + }
11 } 57 }
12 else if(how==='recommend'){ 58 else if(how==='recommend'){
13 - 59 + //TODO
14 } 60 }
15 } 61 }
16 catch(e){ 62 catch(e){
......
...@@ -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;
......