Toggle navigation
Toggle navigation
This project
Loading...
Sign in
송용우
/
oss-Jaksimsamil
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Snippets
Network
Create a new issue
Builds
Commits
Issue Boards
Authored by
JJuOn
2020-08-27 16:18:25 +0900
Browse Files
Options
Browse Files
Download
Email Patches
Plain Diff
Commit
eb980f4aa3a60b8f2cc70549b40e4777c09ed600
eb980f4a
1 parent
638ffb08
Edit api/challenge/addchallenge
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
92 additions
and
3 deletions
jaksimsamil-server/src/api/challenge/challege.ctrl.js
jaksimsamil-server/src/api/challenge/challege.ctrl.js
View file @
eb980f4
const
Challenge
=
require
(
"../../models/challenge"
);
const
Session
=
require
(
"../../models/session"
);
const
Participation
=
require
(
"../../models/participation"
);
const
Group
=
require
(
"../../models/group"
);
const
User
=
require
(
'../../models/user'
);
const
Joi
=
require
(
"joi"
);
/*POST /api/challenge/getChallenge
{
...
...
@@ -8,7 +13,7 @@ const Joi = require("joi");
exports
.
getChallenge
=
async
(
ctx
)
=>
{
try
{
const
{
challengeName
}
=
ctx
.
request
.
body
;
const
challenge
=
await
Challenge
.
findByChallengeName
(
challengeName
);
const
challenge
=
await
Challenge
.
findByChallengeName
(
challengeName
)
.
select
(
'-_id'
)
;
if
(
!
challenge
)
{
ctx
.
status
=
401
;
return
;
...
...
@@ -53,7 +58,7 @@ exports.addChallenge = async (ctx) => {
}
=
ctx
.
request
.
body
;
try
{
const
isChallengeExist
=
await
Challenge
.
findByChallengeName
(
challengeName
);
const
isChallengeExist
=
await
Challenge
.
findByChallengeName
(
challengeName
)
.
select
(
'-_id'
)
;
if
(
isChallengeExist
)
{
ctx
.
status
=
409
;
...
...
@@ -68,11 +73,95 @@ exports.addChallenge = async (ctx) => {
});
await
challenge
.
save
();
const
newChallenge
=
await
Challenge
.
findByChallengeName
(
challengeName
);
const
newChallenge_id
=
newChallenge
.
_id
;
const
timeStep
=
Number
(
durationPerSession
.
slice
(
0
,
-
1
))
for
(
let
s_date
=
startDate
,
e_date
=
s_date
;
s_date
<
endDate
;){
if
(
durationPerSession
[
durationPerSession
.
length
-
1
]
===
'd'
){
e_date
.
setDate
(
s_date
.
getDate
()
+
timeStep
);
}
else
if
(
durationPerSession
[
durationPerSession
.
length
-
1
]
===
'w'
){
e_date
.
setDate
(
s_date
.
getDate
()
+
timeStep
*
7
);
}
else
if
(
durationPerSession
[
durationPerSession
.
length
-
1
]
===
'm'
){
e_date
.
setMonth
(
s_date
.
getMonth
()
+
timeStep
);
}
e_date
.
setMinutes
(
e_date
.
getMinutes
()
-
1
);
if
(
e_date
>
endDate
){
break
;
}
let
status
=
""
;
if
(
s_date
>
new
Date
()){
status
=
"enrolled"
;
}
else
if
(
s_date
<=
new
Date
()
&&
new
Date
()
<=
e_date
){
status
=
"progress"
;
}
else
{
status
=
"end"
;
}
const
session
=
new
Session
({
challengeId
:
newChallenge_id
,
sessionStartDate
:
s_date
,
sessionEndDate
:
e_date
,
status
:
status
,
});
await
session
.
save
();
s_date
=
e_date
;
s_date
.
setMinutes
(
s_date
.
getMinutes
()
+
1
);
}
ctx
.
body
=
challenge
();
}
catch
(
e
)
{
ctx
.
throw
(
500
,
e
);
}
};
/* GET /api/challenge/list?status
query string status can be in ['all','enrolled','progress','end']
*/
exports
.
list
=
async
(
ctx
)
=>
{
try
{
const
status
=
ctx
.
qs
.
status
;
if
(
status
!==
'all'
){
const
challenges
=
await
Challenge
.
find
({
status
:
status
}).
select
(
'-_id'
);
ctx
.
body
=
challenges
;
}
else
{
const
challenges
=
await
Challenge
.
find
({}).
select
(
'-_id'
);
ctx
.
body
=
challenges
;
}
}
catch
(
e
){
ctx
.
throw
(
500
,
e
);
}
};
/* POST /api/challenge/participate
{
username: 'username',
challengeName: 'challengename'
}
*/
exports
.
participate
=
async
(
ctx
)
=>
{
try
{
/*
TODO: How to handle group?
TODO: access token validation,
recommend:get username from access_token
*/
const
{
username
,
challengeName
}
=
ctx
.
body
;
const
challenge
=
await
Challenge
.
findByChallengeName
(
challengeName
);
const
challenge_id
=
challenge
.
_id
;
const
user
=
await
User
.
findByUsername
(
username
);
const
user_id
=
user
.
_id
;
const
newGroup
=
new
Group
({
members
:[
user_id
],
});
newGroup
.
save
();
}
catch
(
e
){
ctx
.
throw
(
500
,
e
);
}
};
\ No newline at end of file
...
...
Please
register
or
login
to post a comment