JJuOn

Implement api/session/createproblem with self selecting problems

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){
......