송용우

Update profile API to use User model

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");
4 /* 3 /*
5 POST /api/auth/register 4 POST /api/auth/register
6 { 5 {
...@@ -28,14 +27,10 @@ exports.register = async (ctx) => { ...@@ -28,14 +27,10 @@ exports.register = async (ctx) => {
28 ctx.status = 409; 27 ctx.status = 409;
29 return; 28 return;
30 } 29 }
31 - const profile = new Profile({
32 - username,
33 - });
34 const user = new User({ 30 const user = new User({
35 username, 31 username,
36 }); 32 });
37 await user.setPassword(password); 33 await user.setPassword(password);
38 - await profile.save();
39 await user.save(); 34 await user.save();
40 ctx.body = user.serialize(); 35 ctx.body = user.serialize();
41 36
......
1 -const Profile = require("../../models/profile"); 1 +const User = require("../../models/user");
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");
...@@ -16,6 +16,7 @@ exports.checkObjectId = (ctx, next) => { ...@@ -16,6 +16,7 @@ exports.checkObjectId = (ctx, next) => {
16 } 16 }
17 return next(); 17 return next();
18 }; 18 };
19 +
19 /*POST /api/profile/getprofile 20 /*POST /api/profile/getprofile
20 { 21 {
21 username: "username" 22 username: "username"
...@@ -24,7 +25,7 @@ exports.checkObjectId = (ctx, next) => { ...@@ -24,7 +25,7 @@ exports.checkObjectId = (ctx, next) => {
24 exports.getProfile = async (ctx) => { 25 exports.getProfile = async (ctx) => {
25 try { 26 try {
26 const { username } = ctx.request.body; 27 const { username } = ctx.request.body;
27 - const profile = await Profile.findByUsername(username); 28 + const profile = await User.findByUsername(username);
28 if (!profile) { 29 if (!profile) {
29 ctx.status = 401; 30 ctx.status = 401;
30 return; 31 return;
...@@ -58,7 +59,7 @@ exports.setProfile = async (ctx) => { ...@@ -58,7 +59,7 @@ exports.setProfile = async (ctx) => {
58 } 59 }
59 60
60 try { 61 try {
61 - const profile = await Profile.findOneAndUpdate( 62 + const profile = await User.findOneAndUpdate(
62 { username: ctx.request.body.username }, 63 { username: ctx.request.body.username },
63 ctx.request.body, 64 ctx.request.body,
64 { 65 {
...@@ -90,7 +91,7 @@ exports.syncBJ = async function (ctx) { ...@@ -90,7 +91,7 @@ exports.syncBJ = async function (ctx) {
90 } 91 }
91 92
92 try { 93 try {
93 - const profile = await Profile.findByUsername(username); 94 + const profile = await User.findByUsername(username);
94 if (!profile) { 95 if (!profile) {
95 ctx.status = 401; 96 ctx.status = 401;
96 return; 97 return;
...@@ -98,7 +99,7 @@ exports.syncBJ = async function (ctx) { ...@@ -98,7 +99,7 @@ exports.syncBJ = async function (ctx) {
98 const BJID = await profile.getBJID(); 99 const BJID = await profile.getBJID();
99 let BJdata = await getBJ.getBJ(BJID); 100 let BJdata = await getBJ.getBJ(BJID);
100 let BJdata_date = await analyzeBJ.analyzeBJ(BJdata); 101 let BJdata_date = await analyzeBJ.analyzeBJ(BJdata);
101 - const updateprofile = await Profile.findOneAndUpdate( 102 + const updateprofile = await User.findOneAndUpdate(
102 { username: username }, 103 { username: username },
103 { solvedBJ: BJdata, solvedBJ_date: BJdata_date }, 104 { solvedBJ: BJdata, solvedBJ_date: BJdata_date },
104 { new: true } 105 { new: true }
...@@ -123,7 +124,7 @@ exports.recommend = async (ctx) => { ...@@ -123,7 +124,7 @@ exports.recommend = async (ctx) => {
123 return; 124 return;
124 } 125 }
125 try { 126 try {
126 - const profile = await Profile.findByUsername(username); 127 + const profile = await User.findByUsername(username);
127 if (!profile) { 128 if (!profile) {
128 ctx.status = 401; 129 ctx.status = 401;
129 return; 130 return;
......