송용우

Update recommend API

...@@ -3,7 +3,6 @@ import { useDispatch, useSelector } from 'react-redux'; ...@@ -3,7 +3,6 @@ import { useDispatch, useSelector } from 'react-redux';
3 import { withRouter } from 'react-router-dom'; 3 import { withRouter } from 'react-router-dom';
4 import HomeForm from '../../components/home/HomeForm'; 4 import HomeForm from '../../components/home/HomeForm';
5 import { getPROFILE } from '../../modules/profile'; 5 import { getPROFILE } from '../../modules/profile';
6 -import { analyzeBJ } from '../../lib/util/analyzeBJ';
7 const HomeContainer = ({ history }) => { 6 const HomeContainer = ({ history }) => {
8 const dispatch = useDispatch(); 7 const dispatch = useDispatch();
9 const { user, profile } = useSelector(({ user, profile }) => ({ 8 const { user, profile } = useSelector(({ user, profile }) => ({
......
...@@ -19,7 +19,7 @@ ...@@ -19,7 +19,7 @@
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 |
...@@ -30,7 +30,7 @@ ...@@ -30,7 +30,7 @@
30 | profile | 유저가 푼 문제 동기화(백준) | PATCH | api/profile/syncBJ | 바로가기 | None | 30 | profile | 유저가 푼 문제 동기화(백준) | PATCH | api/profile/syncBJ | 바로가기 | None |
31 | profile | 유저 정보 수정 | POST | api/profile/setprofile | 바로가기 | JWT TOKEN | 31 | profile | 유저 정보 수정 | POST | api/profile/setprofile | 바로가기 | JWT TOKEN |
32 | profile | 유저 정보 받아오기 | POST | api/profile/getprofile | 바로가기 | JWT | 32 | profile | 유저 정보 받아오기 | POST | api/profile/getprofile | 바로가기 | JWT |
33 -| profile | 추천 문제 조회 | GET | api/profile/recommend:id | 바로가기 | None | 33 +| profile | 추천 문제 조회 | POST | api/profile/recommend | 바로가기 | None |
34 | notify | 슬랙 메시지 전송 요청 | POST | api/notify/ | 34 | notify | 슬랙 메시지 전송 요청 | POST | api/notify/ |
35 | slack | 바로가기 | Jwt Token | 35 | slack | 바로가기 | Jwt Token |
36 | auth | 로그인 | POST | api/auth/login | 바로가기 | None | 36 | auth | 로그인 | POST | api/auth/login | 바로가기 | None |
......
...@@ -1286,3 +1286,28 @@ ...@@ -1286,3 +1286,28 @@
1286 ::ffff:127.0.0.1 - - [24/Jun/2020:11:31:05 +0000] "POST /api/profile/getprofile HTTP/1.1" 200 16266 "http://localhost:3000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54" 1286 ::ffff:127.0.0.1 - - [24/Jun/2020:11:31:05 +0000] "POST /api/profile/getprofile HTTP/1.1" 200 16266 "http://localhost:3000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54"
1287 ::ffff:127.0.0.1 - - [24/Jun/2020:11:39:21 +0000] "POST /api/profile/getprofile HTTP/1.1" 401 12 "http://localhost:3000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54" 1287 ::ffff:127.0.0.1 - - [24/Jun/2020:11:39:21 +0000] "POST /api/profile/getprofile HTTP/1.1" 401 12 "http://localhost:3000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54"
1288 ::ffff:127.0.0.1 - - [24/Jun/2020:11:39:22 +0000] "POST /api/profile/getprofile HTTP/1.1" 200 16266 "http://localhost:3000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54" 1288 ::ffff:127.0.0.1 - - [24/Jun/2020:11:39:22 +0000] "POST /api/profile/getprofile HTTP/1.1" 200 16266 "http://localhost:3000/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54"
1289 +::ffff:127.0.0.1 - - [24/Jun/2020:13:41:48 +0000] "PATCH /api/profile/syncBJ HTTP/1.1" 200 33281 "http://localhost:3000/setting" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54"
1290 +::ffff:127.0.0.1 - - [24/Jun/2020:13:42:48 +0000] "PATCH /api/profile/syncBJ HTTP/1.1" 200 33281 "http://localhost:3000/setting" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36 Edg/83.0.478.54"
1291 +::1 - - [24/Jun/2020:13:52:15 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1292 +::1 - - [24/Jun/2020:13:53:07 +0000] "POST /api/profile/recommend HTTP/1.1" 500 21 "-" "PostmanRuntime/7.25.0"
1293 +::1 - - [24/Jun/2020:13:54:22 +0000] "POST /api/profile/recommend HTTP/1.1" 500 21 "-" "PostmanRuntime/7.25.0"
1294 +::1 - - [24/Jun/2020:13:54:40 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1295 +::1 - - [24/Jun/2020:13:55:32 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1296 +::1 - - [24/Jun/2020:13:57:11 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1297 +::1 - - [24/Jun/2020:13:57:49 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1298 +::1 - - [24/Jun/2020:13:58:26 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1299 +::ffff:127.0.0.1 - - [24/Jun/2020:13:58:49 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1300 +::1 - - [24/Jun/2020:13:58:59 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1301 +::1 - - [24/Jun/2020:13:59:43 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1302 +::1 - - [24/Jun/2020:14:00:09 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1303 +::1 - - [24/Jun/2020:14:02:01 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1304 +::1 - - [24/Jun/2020:14:02:52 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1305 +::1 - - [24/Jun/2020:14:03:53 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1306 +::1 - - [24/Jun/2020:14:04:43 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1307 +::1 - - [24/Jun/2020:14:05:13 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1308 +::1 - - [24/Jun/2020:14:07:09 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1309 +::1 - - [24/Jun/2020:14:08:00 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1310 +::1 - - [24/Jun/2020:14:09:35 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1311 +::1 - - [24/Jun/2020:14:10:57 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1312 +::1 - - [24/Jun/2020:14:11:19 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
1313 +::1 - - [24/Jun/2020:14:12:04 +0000] "POST /api/profile/recommend HTTP/1.1" 404 9 "-" "PostmanRuntime/7.25.0"
......
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 const profileCtrl = require("./profile.ctrl");
4 -
5 profile.post("/solved:id"); 4 profile.post("/solved:id");
6 profile.get("/solvednum:id"); 5 profile.get("/solvednum:id");
7 -profile.get("/recommendps:id"); 6 +profile.post("/recommend", profileCtrl.recommend);
8 profile.patch("/syncBJ", profileCtrl.syncBJ); 7 profile.patch("/syncBJ", profileCtrl.syncBJ);
9 profile.post("/setprofile", profileCtrl.setProfile); 8 profile.post("/setprofile", profileCtrl.setProfile);
10 profile.post("/getprofile", profileCtrl.getProfile); 9 profile.post("/getprofile", profileCtrl.getProfile);
......
...@@ -2,6 +2,9 @@ const Profile = require("../../models/profile"); ...@@ -2,6 +2,9 @@ const Profile = require("../../models/profile");
2 const mongoose = require("mongoose"); 2 const mongoose = require("mongoose");
3 const getBJ = require("../../util/getBJ"); 3 const getBJ = require("../../util/getBJ");
4 const Joi = require("joi"); 4 const Joi = require("joi");
5 +const analyzeBJ = require("../../util/analyzeBJ");
6 +const compareBJ = require("../../util/compareBJ");
7 +const problem_set = require("../../data/problem_set");
5 8
6 const { ObjectId } = mongoose.Types; 9 const { ObjectId } = mongoose.Types;
7 10
...@@ -95,9 +98,10 @@ exports.syncBJ = async function (ctx) { ...@@ -95,9 +98,10 @@ exports.syncBJ = async function (ctx) {
95 } 98 }
96 const BJID = await profile.getBJID(); 99 const BJID = await profile.getBJID();
97 let BJdata = await getBJ.getBJ(BJID); 100 let BJdata = await getBJ.getBJ(BJID);
101 + let BJdata_date = await analyzeBJ.analyzeBJ(BJdata);
98 const updateprofile = await Profile.findOneAndUpdate( 102 const updateprofile = await Profile.findOneAndUpdate(
99 { username: username }, 103 { username: username },
100 - { solvedBJ: BJdata }, 104 + { solvedBJ: BJdata, solvedBJ_date: BJdata_date },
101 { new: true } 105 { new: true }
102 ).exec(); 106 ).exec();
103 ctx.body = updateprofile; 107 ctx.body = updateprofile;
...@@ -105,3 +109,34 @@ exports.syncBJ = async function (ctx) { ...@@ -105,3 +109,34 @@ exports.syncBJ = async function (ctx) {
105 ctx.throw(500, e); 109 ctx.throw(500, e);
106 } 110 }
107 }; 111 };
112 +s;
113 +
114 +/*
115 +POST /api/proflie/recommend
116 +{
117 + username: 'userid'
118 +}
119 + */
120 +exports.recommend = async (ctx) => {
121 + const { username } = ctx.request.body;
122 +
123 + if (!username) {
124 + ctx.status = 401;
125 + return;
126 + }
127 + try {
128 + const profile = await Profile.findByUsername(username);
129 + if (!profile) {
130 + ctx.status = 401;
131 + return;
132 + }
133 + let unsolved_data = compareBJ.compareBJ(
134 + profile.getBJdata(),
135 + problem_set.problem_set
136 + );
137 + ctx.body = compareBJ.randomItem(unsolved_data);
138 + //데이터가 비었을 떄 예외처리 필요
139 + } catch (e) {
140 + ctx.throw(500, e);
141 + }
142 +};
......
1 -export const problem_set = [ 1 +exports.problem_set = [
2 "1517", 2 "1517",
3 "2448", 3 "2448",
4 "1891", 4 "1891",
......
...@@ -6,6 +6,7 @@ const ProfileSchema = new Schema({ ...@@ -6,6 +6,7 @@ const ProfileSchema = new Schema({
6 username: { type: String, required: true, unique: true }, 6 username: { type: String, required: true, unique: true },
7 userBJID: String, 7 userBJID: String,
8 solvedBJ: Object, 8 solvedBJ: Object,
9 + solvedBJ_date: Object,
9 friendList: [String], 10 friendList: [String],
10 slackWebHookURL: String, 11 slackWebHookURL: String,
11 }); 12 });
...@@ -15,6 +16,9 @@ ProfileSchema.statics.findByUsername = function (username) { ...@@ -15,6 +16,9 @@ ProfileSchema.statics.findByUsername = function (username) {
15 ProfileSchema.methods.getBJID = function () { 16 ProfileSchema.methods.getBJID = function () {
16 return this.userBJID; 17 return this.userBJID;
17 }; 18 };
19 +ProfileSchema.methods.getBJdata = function () {
20 + return this.solvedBJ;
21 +};
18 22
19 ProfileSchema.methods.serialize = function () { 23 ProfileSchema.methods.serialize = function () {
20 const data = this.toJSON(); 24 const data = this.toJSON();
......
1 -/* 1 +let moment = require("moment");
2 -2. 현재 날짜와의 차이 =>
3 -3. 오늘 푼 문제 => 앞에서부터 순회하면서 데이트 같은거 찾기
4 -3. 최근 일주일간 푼 문제 수 => 앞에서부터 순회하면서 - 값이
5 -4. 추천 문제 => 정규 셋에서 없는거 찾기
6 -5. 날짜별로 묶기.
7 -데이터베이스에서 처리하자
8 -*/
9 -
10 -let moment = require('moment');
11 2
12 exports.analyzeBJ = function (solvedBJ) { 3 exports.analyzeBJ = function (solvedBJ) {
13 try { 4 try {
14 if (solvedBJ) { 5 if (solvedBJ) {
15 - console.log(solvedBJ[0]);
16 let presentDate = moment(); 6 let presentDate = moment();
17 - let presentDate_str = presentDate.format('YYYYMMDD'); 7 + let presentDate_str = presentDate.format("YYYYMMDD");
18 - let latestDate = moment(solvedBJ[0].solved_date, 'YYYYMMDD'); 8 + let latestDate = moment(solvedBJ[0].solved_date, "YYYYMMDD");
19 - let difflatest = presentDate.diff(latestDate, 'days'); 9 + let difflatest = presentDate.diff(latestDate, "days");
20 10
21 let solvedBJbyDATE = {}; 11 let solvedBJbyDATE = {};
22 for (let i = 0; i < solvedBJ.length; i++) { 12 for (let i = 0; i < solvedBJ.length; i++) {
...@@ -34,13 +24,13 @@ exports.analyzeBJ = function (solvedBJ) { ...@@ -34,13 +24,13 @@ exports.analyzeBJ = function (solvedBJ) {
34 ? solvedBJbyDATE[presentDate_str].length 24 ? solvedBJbyDATE[presentDate_str].length
35 : 0; 25 : 0;
36 let returnOBJ = { 26 let returnOBJ = {
37 - latestDate: latestDate.format('YYYYMMDD'), 27 + latestDate: latestDate.format("YYYYMMDD"),
38 difflatest: difflatest, 28 difflatest: difflatest,
39 latestNum: latestNum, 29 latestNum: latestNum,
40 presentNum: presentNum, 30 presentNum: presentNum,
41 solvedBJbyDATE: solvedBJbyDATE, 31 solvedBJbyDATE: solvedBJbyDATE,
42 }; 32 };
43 - console.log(returnOBJ); 33 +
44 return returnOBJ; 34 return returnOBJ;
45 } 35 }
46 } catch (e) { 36 } catch (e) {
......
1 -/*
2 -집중을 해보자.
3 -새거와 데이터가 있다.
4 -데이터 기준으로 새거에 자료가 있으면 넘어가기
5 -없으면 새 배열에 추가
6 -키만 모아둔 리스트를 만들자.
7 -그렇게 해서
8 -반복은 새거 길이만큼
9 -데이터에 있으면 추가 X
10 -없으면 추가
11 -그렇게 반환
12 -*/
13 -
14 exports.compareBJ = function (solvedBJ_new, problem_set) { 1 exports.compareBJ = function (solvedBJ_new, problem_set) {
15 try { 2 try {
16 let new_obj = []; 3 let new_obj = [];
17 - for (let i = 0; i < solvedBJ.length; i++) { 4 + for (let i = 0; i < solvedBJ_new.length; i++) {
18 - if (solvedBJ_new[i].problem_number in problem_set) { 5 + if (!problem_set.includes(String(solvedBJ_new[i].problem_number))) {
19 new_obj.push(solvedBJ_new[i]); 6 new_obj.push(solvedBJ_new[i]);
20 } 7 }
21 } 8 }
22 - console.log(new_obj); 9 + return new_obj;
23 } catch (e) { 10 } catch (e) {
24 console.log(e); 11 console.log(e);
25 } 12 }
26 }; 13 };
14 +
15 +exports.randomItem = function (a) {
16 + return a[Math.floor(Math.random() * a.length)];
17 +};
......