송용우

Merge commit '5d96fb7e' into develop

......@@ -10,7 +10,7 @@ const Joi = require("joi");
challengeName: "challengeName"
}
*/
exports.getChallenge = async (ctx) => {
exports.getChallengePOST = async (ctx) => {
try {
const { challengeName } = ctx.request.body;
const challenge = await Challenge.findByChallengeName(challengeName);
......@@ -189,3 +189,36 @@ exports.participate = async (ctx) => {
ctx.throw(500, e);
}
};
/*
GET /api/challenge/getchallenge?username
*/
exports.getChallengeGET = async (ctx)=>{
try{
const {username} = ctx.request.query;
const user = await User.findByUsername(username);
const user_id=user._id;
const groups = await Group.find();
const userIncludedGroups = [];
for(let i=0;i<groups.length;i++){
if(groups[i].members.includes(user_id)){
userIncludedGroups.push(groups[i]);
}
}
const challengeList = [];
for(let i=0;i<userIncludedGroups.length;i++){
const participations = await Participation.findByGroupId(userIncludedGroups[i]._id);
for(let j=0;j<participations.length;j++){
const session = await Session.findById(participations[j].sessionId);
const challenge = await Challenge.findById(session.challengeId);
if(!challengeList.includes(challenge)){
challengeList.push(challenge);
}
}
}
ctx.body = challengeList.map(c=>c.serialize());
}
catch(e){
ctx.throw(500,e);
}
}
\ No newline at end of file
......
......@@ -2,9 +2,10 @@ const Router = require('koa-router');
const challenge = new Router();
const challengeCtrl = require('./challege.ctrl');
challenge.post("/getchallenge",challengeCtrl.getChallenge);
challenge.post("/getchallenge",challengeCtrl.getChallengePOST);
challenge.post("/addchallenge",challengeCtrl.addChallenge);
challenge.get("/list/:status",challengeCtrl.list);
challenge.post("/participate",challengeCtrl.participate);
challenge.get("/getchallenge",challengeCtrl.getChallengeGET);
module.exports = challenge;
\ No newline at end of file
......
......@@ -6,6 +6,7 @@ const User = require("../../models/user");
const Challenge = require("../../models/challenge");
const Problem = require("../../models/problem");
const mongoose = require("mongoose");
require('dotenv').config();
const {ObjectId} = mongoose.Types;
......@@ -127,7 +128,25 @@ exports.status=async (ctx)=>{
}
const participation=await Participation.findOne({sessionId:sessionId,groupId:groupId});
const group = await Group.findById(groupId);
for(let i=0;i<group.members.length;i++){
const user=await User.findById(group.members[i]);
await axios.patch(`http://localhost:${process.env.SERVER_PORT}/api/profile/syncBJ`,{username:user.username});
}
for(let i=0;i<group.members.length;i++){
const user=await User.findById(group.members[i]);
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<participation.problems.length;j++){
if(userProblemList.includes(String(participation.problems[i].problemNum))){
participation.problems[i].isSolved=true;
await participation.save();
}
}
}
ctx.body=participation.serialize();
}
catch(e){
ctx.throw(500,e);
......