송용우

Update Challenge API

...@@ -27,7 +27,7 @@ POST http://facerain.dcom.club/profile/getprofile ...@@ -27,7 +27,7 @@ POST http://facerain.dcom.club/profile/getprofile
27 ## API Table 27 ## API Table
28 28
29 | group | description | method | URL | Detail | Auth | 29 | group | description | method | URL | Detail | Auth |
30 -| ------- | -------------------------------------- | ------ | ----------------------- | -------------------------------------- | --------- | 30 +| --------- | -------------------------------------- | ------ | -------------------------- | -------------------------------------- | --------- |
31 | profile | 유저가 푼 문제 조회(백준) | GET | api/profile/solvedBJ:id | [바로가기](/src/api/profile/README.md) | None | 31 | profile | 유저가 푼 문제 조회(백준) | GET | api/profile/solvedBJ:id | [바로가기](/src/api/profile/README.md) | None |
32 | profile | 유저가 푼 문제 동기화(백준) | PATCH | api/profile/syncBJ | [바로가기](/src/api/profile/README.md) | None | 32 | profile | 유저가 푼 문제 동기화(백준) | PATCH | api/profile/syncBJ | [바로가기](/src/api/profile/README.md) | None |
33 | profile | 유저 정보 수정 | POST | api/profile/setprofile | [바로가기](/src/api/profile/README.md) | JWT TOKEN | 33 | profile | 유저 정보 수정 | POST | api/profile/setprofile | [바로가기](/src/api/profile/README.md) | JWT TOKEN |
...@@ -40,3 +40,4 @@ POST http://facerain.dcom.club/profile/getprofile ...@@ -40,3 +40,4 @@ POST http://facerain.dcom.club/profile/getprofile
40 | auth | 로그아웃 | POST | api/auth/logout | [바로가기](/src/api/auth/README.md) | JWT Token | 40 | auth | 로그아웃 | POST | api/auth/logout | [바로가기](/src/api/auth/README.md) | JWT Token |
41 | auth | 회원가입 | POST | api/auth/register | [바로가기](/src/api/auth/README.md) | None | 41 | auth | 회원가입 | POST | api/auth/register | [바로가기](/src/api/auth/README.md) | None |
42 | auth | 로그인 확인 | GET | api/auth/check | [바로가기](/src/api/auth/README.md) | None | 42 | auth | 로그인 확인 | GET | api/auth/check | [바로가기](/src/api/auth/README.md) | None |
43 +| challenge | 특정 챌린지 조회(이름) | POST | api/challenge/getChallenge | [바로가기]() | None |
......
1 +const Challenge = require("../../models/challenge");
2 +const Joi = require("joi");
3 +/*POST /api/challenge/getChallenge
4 +{
5 + challengeName: "challengeName"
6 +}
7 +*/
8 +exports.getChallenge = async (ctx) => {
9 + try {
10 + const { challengeName } = ctx.request.body;
11 + const challenge = await Challenge.findByChallengeName(challengeName);
12 + if (!challenge) {
13 + ctx.status = 401;
14 + return;
15 + }
16 + ctx.body = challenge;
17 + } catch (e) {
18 + ctx.throw(500, e);
19 + }
20 +};
21 +/*POST /api/challenge/addChallenge
22 +{
23 + challengeName: "challengeName",
24 + startDate: Date Object,
25 + endDate: Date Object,
26 + durationPerSession: "2w", // '1d' means one day per session, '2w' means 2 weeks per session, '3m' means 3 months per session.
27 + goalPerSession: 3,
28 + groups: [{'name1', 'name2'}]
29 +}
30 +*/
31 +exports.addChallenge = async (ctx) => {
32 + const schema = Joi.object()
33 + .keys({
34 + challengeName: Joi.string(),
35 + startDate: Joi.date(),
36 + endDate: Joi.date(),
37 + durationPerSession: Joi.string(),
38 + goalPerSession: Joi.number(),
39 + groups: Joi.array().items(Joi.string()),
40 + })
41 + .unknown();
42 + const result = Joi.validate(ctx.request.body, schema);
43 +
44 + if (result.error) {
45 + ctx.status = 400;
46 + ctx.body = result.error;
47 + return;
48 + }
49 + const {
50 + challengeName,
51 + startDate,
52 + endDate,
53 + durationPerSession,
54 + goalPerSession,
55 + } = ctx.request.body;
56 +
57 + try {
58 + const isChallengeExist = await Challenge.findByChallengeName(challengeName);
59 +
60 + if (isChallengeExist) {
61 + ctx.status = 409;
62 + return;
63 + }
64 + const challenge = new Challenge({
65 + challengeName,
66 + startDate,
67 + endDate,
68 + durationPerSession,
69 + goalPerSession,
70 + });
71 +
72 + await challenge.save();
73 + ctx.body = challenge();
74 + } catch (e) {
75 + ctx.throw(500, e);
76 + }
77 + /*
78 + TODO: How to handle group?
79 + */
80 +};
...@@ -50,7 +50,6 @@ exports.setProfile = async (ctx) => { ...@@ -50,7 +50,6 @@ exports.setProfile = async (ctx) => {
50 //freindList: Joi.array().items(Joi.string()), 50 //freindList: Joi.array().items(Joi.string()),
51 }) 51 })
52 .unknown(); 52 .unknown();
53 - console.log(ctx.request.body);
54 const result = Joi.validate(ctx.request.body, schema); 53 const result = Joi.validate(ctx.request.body, schema);
55 if (result.error) { 54 if (result.error) {
56 ctx.status = 400; 55 ctx.status = 400;
......