송용우

Merge commit '5d96fb7e' into develop

...@@ -10,7 +10,7 @@ const Joi = require("joi"); ...@@ -10,7 +10,7 @@ const Joi = require("joi");
10 challengeName: "challengeName" 10 challengeName: "challengeName"
11 } 11 }
12 */ 12 */
13 -exports.getChallenge = async (ctx) => { 13 +exports.getChallengePOST = async (ctx) => {
14 try { 14 try {
15 const { challengeName } = ctx.request.body; 15 const { challengeName } = ctx.request.body;
16 const challenge = await Challenge.findByChallengeName(challengeName); 16 const challenge = await Challenge.findByChallengeName(challengeName);
...@@ -189,3 +189,36 @@ exports.participate = async (ctx) => { ...@@ -189,3 +189,36 @@ exports.participate = async (ctx) => {
189 ctx.throw(500, e); 189 ctx.throw(500, e);
190 } 190 }
191 }; 191 };
192 +
193 +/*
194 +GET /api/challenge/getchallenge?username
195 +*/
196 +exports.getChallengeGET = async (ctx)=>{
197 + try{
198 + const {username} = ctx.request.query;
199 + const user = await User.findByUsername(username);
200 + const user_id=user._id;
201 + const groups = await Group.find();
202 + const userIncludedGroups = [];
203 + for(let i=0;i<groups.length;i++){
204 + if(groups[i].members.includes(user_id)){
205 + userIncludedGroups.push(groups[i]);
206 + }
207 + }
208 + const challengeList = [];
209 + for(let i=0;i<userIncludedGroups.length;i++){
210 + const participations = await Participation.findByGroupId(userIncludedGroups[i]._id);
211 + for(let j=0;j<participations.length;j++){
212 + const session = await Session.findById(participations[j].sessionId);
213 + const challenge = await Challenge.findById(session.challengeId);
214 + if(!challengeList.includes(challenge)){
215 + challengeList.push(challenge);
216 + }
217 + }
218 + }
219 + ctx.body = challengeList.map(c=>c.serialize());
220 + }
221 + catch(e){
222 + ctx.throw(500,e);
223 + }
224 +}
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -2,9 +2,10 @@ const Router = require('koa-router'); ...@@ -2,9 +2,10 @@ const Router = require('koa-router');
2 const challenge = new Router(); 2 const challenge = new Router();
3 const challengeCtrl = require('./challege.ctrl'); 3 const challengeCtrl = require('./challege.ctrl');
4 4
5 -challenge.post("/getchallenge",challengeCtrl.getChallenge); 5 +challenge.post("/getchallenge",challengeCtrl.getChallengePOST);
6 challenge.post("/addchallenge",challengeCtrl.addChallenge); 6 challenge.post("/addchallenge",challengeCtrl.addChallenge);
7 challenge.get("/list/:status",challengeCtrl.list); 7 challenge.get("/list/:status",challengeCtrl.list);
8 challenge.post("/participate",challengeCtrl.participate); 8 challenge.post("/participate",challengeCtrl.participate);
9 +challenge.get("/getchallenge",challengeCtrl.getChallengeGET);
9 10
10 module.exports = challenge; 11 module.exports = challenge;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -6,6 +6,7 @@ const User = require("../../models/user"); ...@@ -6,6 +6,7 @@ const User = require("../../models/user");
6 const Challenge = require("../../models/challenge"); 6 const Challenge = require("../../models/challenge");
7 const Problem = require("../../models/problem"); 7 const Problem = require("../../models/problem");
8 const mongoose = require("mongoose"); 8 const mongoose = require("mongoose");
9 +require('dotenv').config();
9 10
10 const {ObjectId} = mongoose.Types; 11 const {ObjectId} = mongoose.Types;
11 12
...@@ -127,7 +128,25 @@ exports.status=async (ctx)=>{ ...@@ -127,7 +128,25 @@ exports.status=async (ctx)=>{
127 } 128 }
128 const participation=await Participation.findOne({sessionId:sessionId,groupId:groupId}); 129 const participation=await Participation.findOne({sessionId:sessionId,groupId:groupId});
129 const group = await Group.findById(groupId); 130 const group = await Group.findById(groupId);
130 - 131 + for(let i=0;i<group.members.length;i++){
132 + const user=await User.findById(group.members[i]);
133 + await axios.patch(`http://localhost:${process.env.SERVER_PORT}/api/profile/syncBJ`,{username:user.username});
134 + }
135 + for(let i=0;i<group.members.length;i++){
136 + const user=await User.findById(group.members[i]);
137 + let userProblemList = [];
138 + for(let key in user.solvedBJ_date.solvedBJbyDATE){
139 + userProblemList.push(user.solvedBJ_date.solvedBJbyDATE[key]);
140 + }
141 + userProblemList=userProblemList.flat().map(elem=>elem.problem_number);
142 + for(let j=0;j<participation.problems.length;j++){
143 + if(userProblemList.includes(String(participation.problems[i].problemNum))){
144 + participation.problems[i].isSolved=true;
145 + await participation.save();
146 + }
147 + }
148 + }
149 + ctx.body=participation.serialize();
131 } 150 }
132 catch(e){ 151 catch(e){
133 ctx.throw(500,e); 152 ctx.throw(500,e);
......