송용우

Update syncBJ, setprofile API

...@@ -19,17 +19,17 @@ ...@@ -19,17 +19,17 @@
19 ## API Table 19 ## API Table
20 20
21 | group | description | method | URL | Detail | Auth | 21 | group | description | method | URL | Detail | Auth |
22 -| ------- | ------------------------ | ------ | -------------------------- | -------- | --------- | 22 +| ------- | --------------------------- | ------ | ------------------------ | -------- | --------- |
23 | user | 유저 등록 | POST | api/user | 바로가기 | JWT Token | 23 | user | 유저 등록 | POST | api/user | 바로가기 | JWT Token |
24 | user | 유저 삭제 | DELETE | api/user:id | 바로가기 | JWT Token | 24 | user | 유저 삭제 | DELETE | api/user:id | 바로가기 | JWT Token |
25 | user | 특정 유저 조회 | GET | api/user:id | 바로가기 | None | 25 | user | 특정 유저 조회 | GET | api/user:id | 바로가기 | None |
26 | user | 전체 유저 조회 | GET | api/user | 바로가기 | JWT Token | 26 | user | 전체 유저 조회 | GET | api/user | 바로가기 | JWT Token |
27 | friend | 유저 친구 등록 | POST | api/friend | 바로가기 | JWT Token | 27 | friend | 유저 친구 등록 | POST | api/friend | 바로가기 | JWT Token |
28 | friend | 유저의 친구 조회 | GET | api/friend:id | 바로가기 | None | 28 | friend | 유저의 친구 조회 | GET | api/friend:id | 바로가기 | None |
29 -| profile | 유저가 푼 문제 조회 | GET | api/profile/solved:id | 바로가기 | None | 29 +| profile | 유저가 푼 문제 조회(백준) | GET | api/profile/solvedBJ:id | 바로가기 | None |
30 -| profile | 유저가 푼 문제 동기화 | Update | api/profile/solved:id | 바로가기 | None | 30 +| profile | 유저가 푼 문제 동기화(백준) | PATCH | api/profile/syncBJ | 바로가기 | None |
31 -| profile | 유저가 푼 문제 개수 조회 | GET | api/profile/solvednum:id | 바로가기 | None | 31 +| profile | 유저 정보 수정 | POST | api/profile/setprofile | 바로가기 | JWT TOKEN |
32 -| profile | 추천 문제 조회 | GET | api/profile/recommendps:id | 바로가기 | None | 32 +| profile | 추천 문제 조회 | GET | api/profile/recommend:id | 바로가기 | None |
33 | notify | 슬랙 메시지 전송 요청 | POST | api/notify/slack | 바로가기 | Jwt Token | 33 | notify | 슬랙 메시지 전송 요청 | POST | api/notify/slack | 바로가기 | Jwt Token |
34 | auth | 로그인 | POST | api/auth/login | 바로가기 | None | 34 | auth | 로그인 | POST | api/auth/login | 바로가기 | None |
35 | auth | 로그아웃 | GET | api/auth/logout | 바로가기 | JWT Token | 35 | auth | 로그아웃 | GET | api/auth/logout | 바로가기 | JWT Token |
......
1 +::1 - - [12/Jun/2020:15:52:18 +0000] "GET /api/auth/logout HTTP/1.1" 204 - "-" "PostmanRuntime/7.25.0"
2 +::1 - - [12/Jun/2020:16:36:46 +0000] "POST /api/auth/register HTTP/1.1" 200 67 "-" "PostmanRuntime/7.25.0"
3 +::1 - - [12/Jun/2020:16:53:34 +0000] "POST /api/profile/syncBJ HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
4 +::1 - - [12/Jun/2020:16:53:54 +0000] "POST /api/profile/syncBJ HTTP/1.1" 405 18 "-" "PostmanRuntime/7.25.0"
5 +::1 - - [12/Jun/2020:16:54:00 +0000] "PATCH /api/profile/syncBJ HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
6 +::1 - - [13/Jun/2020:06:37:54 +0000] "POST /api/profile/setprofile HTTP/1.1" 400 304 "-" "PostmanRuntime/7.25.0"
7 +::1 - - [13/Jun/2020:06:38:12 +0000] "POST /api/profile/setprofile HTTP/1.1" 400 322 "-" "PostmanRuntime/7.25.0"
8 +::1 - - [13/Jun/2020:06:38:29 +0000] "POST /api/profile/setprofile HTTP/1.1" 400 322 "-" "PostmanRuntime/7.25.0"
9 +::1 - - [13/Jun/2020:06:38:34 +0000] "POST /api/profile/setprofile HTTP/1.1" 400 322 "-" "PostmanRuntime/7.25.0"
10 +::1 - - [13/Jun/2020:06:38:49 +0000] "POST /api/profile/setprofile HTTP/1.1" 400 322 "-" "PostmanRuntime/7.25.0"
11 +::1 - - [13/Jun/2020:06:39:19 +0000] "POST /api/profile/setprofile HTTP/1.1" 400 322 "-" "PostmanRuntime/7.25.0"
12 +::1 - - [13/Jun/2020:06:49:56 +0000] "POST /api/profile/setprofile HTTP/1.1" 400 322 "-" "PostmanRuntime/7.25.0"
13 +::1 - - [13/Jun/2020:06:50:25 +0000] "POST /api/profile/setprofile HTTP/1.1" 400 322 "-" "PostmanRuntime/7.25.0"
14 +::1 - - [13/Jun/2020:06:51:32 +0000] "POST /api/profile/setprofile HTTP/1.1" 200 113 "-" "PostmanRuntime/7.25.0"
15 +::1 - - [13/Jun/2020:06:55:35 +0000] "PATCH /api/profile/syncBJ HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
16 +::1 - - [13/Jun/2020:06:57:25 +0000] "PATCH /api/profile/syncBJ HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
17 +::1 - - [13/Jun/2020:06:59:46 +0000] "PATCH /api/profile/syncBJ HTTP/1.1" 500 21 "-" "PostmanRuntime/7.25.0"
18 +::1 - - [13/Jun/2020:07:00:05 +0000] "PATCH /api/profile/syncBJ HTTP/1.1" 401 12 "-" "PostmanRuntime/7.25.0"
19 +::1 - - [13/Jun/2020:07:00:38 +0000] "PATCH /api/profile/syncBJ HTTP/1.1" 401 12 "-" "PostmanRuntime/7.25.0"
20 +::1 - - [13/Jun/2020:07:04:45 +0000] "PATCH /api/profile/syncBJ HTTP/1.1" 500 21 "-" "PostmanRuntime/7.25.0"
21 +::1 - - [13/Jun/2020:07:07:00 +0000] "PATCH /api/profile/syncBJ HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
22 +::1 - - [13/Jun/2020:07:07:49 +0000] "PATCH /api/profile/syncBJ HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
...@@ -2,12 +2,18 @@ const Koa = require("koa"); ...@@ -2,12 +2,18 @@ const Koa = require("koa");
2 const Router = require("koa-router"); 2 const Router = require("koa-router");
3 const bodyParser = require("koa-bodyparser"); 3 const bodyParser = require("koa-bodyparser");
4 const mongoose = require("mongoose"); 4 const mongoose = require("mongoose");
5 +const fs = require("fs");
6 +const morgan = require("koa-morgan");
5 const jwtMiddleware = require("./src/lib/jwtMiddleware"); 7 const jwtMiddleware = require("./src/lib/jwtMiddleware");
6 const app = new Koa(); 8 const app = new Koa();
7 const router = new Router(); 9 const router = new Router();
10 +const accessLogStream = fs.createWriteStream(__dirname + "/access.log", {
11 + flags: "a",
12 +});
8 require("dotenv").config(); 13 require("dotenv").config();
9 app.use(bodyParser()); 14 app.use(bodyParser());
10 app.use(jwtMiddleware); 15 app.use(jwtMiddleware);
16 +app.use(morgan("combined", { stream: accessLogStream }));
11 const api = require("./src/api"); 17 const api = require("./src/api");
12 const { SERVER_PORT, MONGO_URL } = process.env; 18 const { SERVER_PORT, MONGO_URL } = process.env;
13 19
......
...@@ -1923,6 +1923,14 @@ ...@@ -1923,6 +1923,14 @@
1923 } 1923 }
1924 } 1924 }
1925 }, 1925 },
1926 + "koa-morgan": {
1927 + "version": "1.0.1",
1928 + "resolved": "https://registry.npmjs.org/koa-morgan/-/koa-morgan-1.0.1.tgz",
1929 + "integrity": "sha1-CAUuDODYOdPEMXi5CluzQkvvH5k=",
1930 + "requires": {
1931 + "morgan": "^1.6.1"
1932 + }
1933 + },
1926 "koa-router": { 1934 "koa-router": {
1927 "version": "9.0.1", 1935 "version": "9.0.1",
1928 "resolved": "https://registry.npmjs.org/koa-router/-/koa-router-9.0.1.tgz", 1936 "resolved": "https://registry.npmjs.org/koa-router/-/koa-router-9.0.1.tgz",
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
17 "jsonwebtoken": "^8.5.1", 17 "jsonwebtoken": "^8.5.1",
18 "koa": "^2.12.0", 18 "koa": "^2.12.0",
19 "koa-bodyparser": "^4.3.0", 19 "koa-bodyparser": "^4.3.0",
20 + "koa-morgan": "^1.0.1",
20 "koa-router": "^9.0.1", 21 "koa-router": "^9.0.1",
21 "mongoose": "^5.9.17", 22 "mongoose": "^5.9.17",
22 "morgan": "^1.10.0", 23 "morgan": "^1.10.0",
......
1 const Joi = require("joi"); 1 const Joi = require("joi");
2 const User = require("../../models/user"); 2 const User = require("../../models/user");
3 +const Profile = require("../../models/profile");
3 /* 4 /*
4 POST /api/auth/register 5 POST /api/auth/register
5 { 6 {
...@@ -27,10 +28,14 @@ exports.register = async (ctx) => { ...@@ -27,10 +28,14 @@ exports.register = async (ctx) => {
27 ctx.status = 409; 28 ctx.status = 409;
28 return; 29 return;
29 } 30 }
31 + const profile = new Profile({
32 + username,
33 + });
30 const user = new User({ 34 const user = new User({
31 username, 35 username,
32 }); 36 });
33 await user.setPassword(password); 37 await user.setPassword(password);
38 + await profile.save();
34 await user.save(); 39 await user.save();
35 ctx.body = user.serialize(); 40 ctx.body = user.serialize();
36 41
......
1 const Router = require("koa-router"); 1 const Router = require("koa-router");
2 const profile = new Router(); 2 const profile = new Router();
3 +const profileCtrl = require("./profile.ctrl");
3 4
4 profile.post("/solved:id"); 5 profile.post("/solved:id");
5 profile.get("/solvednum:id"); 6 profile.get("/solvednum:id");
6 -profile.get("recommendps:id"); 7 +profile.get("/recommendps:id");
7 - 8 +profile.patch("/syncBJ", profileCtrl.syncBJ);
9 +profile.post("/setprofile", profileCtrl.setProfile);
8 module.exports = profile; 10 module.exports = profile;
......
1 +const Profile = require("../../models/profile");
2 +const mongoose = require("mongoose");
3 +const getBJ = require("../../util/getBJ");
4 +const Joi = require("joi");
5 +
6 +const { ObjectId } = mongoose.Types;
7 +
8 +exports.checkObjectId = (ctx, next) => {
9 + const { username } = ctx.request.body;
10 + if (!ObjectId.isValid(username)) {
11 + ctx.status = 400;
12 + return;
13 + }
14 + return next();
15 +};
16 +/*
17 +POST /api/proflie/setprofile
18 +{
19 + username: "username",
20 + userBJID: "userBJID",
21 + friendList: [String],
22 +}
23 + */
24 +exports.setProfile = async (ctx) => {
25 + const schema = Joi.object()
26 + .keys({
27 + username: Joi.string(),
28 + userBJID: Joi.string(),
29 + //freindList: Joi.array().items(Joi.string()),
30 + })
31 + .unknown();
32 +
33 + const result = Joi.validate(ctx.request.body, schema);
34 + if (result.error) {
35 + ctx.status = 400;
36 + ctx.body = result.error;
37 + return;
38 + }
39 +
40 + try {
41 + const profile = await Profile.findOneAndUpdate(
42 + { username: ctx.request.body.username },
43 + ctx.request.body,
44 + {
45 + new: true,
46 + }
47 + ).exec();
48 +
49 + if (!profile) {
50 + ctx.status = 404;
51 + return;
52 + }
53 + ctx.body = profile;
54 + } catch (e) {
55 + ctx.throw(500, e);
56 + }
57 +};
58 +/*
59 +PATCH /api/proflie/syncBJ
60 +{
61 + username: 'userid'
62 +}
63 + */
64 +exports.syncBJ = async function (ctx) {
65 + const { username } = ctx.request.body;
66 +
67 + if (!username) {
68 + ctx.status = 401;
69 + return;
70 + }
71 +
72 + try {
73 + const profile = await Profile.findByUsername(username);
74 + if (!profile) {
75 + ctx.status = 401;
76 + return;
77 + }
78 + const BJID = await profile.getBJID();
79 + let BJdata = await getBJ.getBJ(BJID);
80 + const updateprofile = await Profile.findOneAndUpdate(
81 + { username: username },
82 + { solvedBJ: BJdata },
83 + { new: true }
84 + ).exec();
85 + ctx.body = updateprofile;
86 + } catch (e) {
87 + ctx.throw(500, e);
88 + }
89 +};
1 +const mongoose = require("mongoose");
2 +
3 +const { Schema } = mongoose;
4 +
5 +const ProfileSchema = new Schema({
6 + username: { type: String, required: true, unique: true },
7 + userBJID: String,
8 + solvedBJ: Object,
9 + friendList: [String],
10 +});
11 +ProfileSchema.statics.findByUsername = function (username) {
12 + return this.findOne({ username });
13 +};
14 +ProfileSchema.methods.getBJID = function () {
15 + return this.userBJID;
16 +};
17 +
18 +ProfileSchema.methods.serialize = function () {
19 + const data = this.toJSON();
20 + return data;
21 +};
22 +const Profile = mongoose.model("Profile", ProfileSchema);
23 +module.exports = Profile;
...@@ -3,7 +3,6 @@ const cheerio = require("cheerio"); ...@@ -3,7 +3,6 @@ const cheerio = require("cheerio");
3 const StringToDate = require("./StringToDate"); 3 const StringToDate = require("./StringToDate");
4 /* 4 /*
5 ToDO 5 ToDO
6 -- 유저 네임 검증
7 - 예외 처리 6 - 예외 처리
8 */ 7 */
9 exports.getBJ = async function (userid) { 8 exports.getBJ = async function (userid) {
......