Skip to content
Snippets Groups Projects
Commit 47c2a6cd authored by Hyun Woo Jeong's avatar Hyun Woo Jeong
Browse files

see posts in main page

parents 632e102b 7db5ba67
No related branches found
No related tags found
No related merge requests found
......@@ -13,7 +13,71 @@ const articleService = {
async findArticleById(articleId) {
try {
const article = await Article
.findById(articleId)
.populate('author')
.populate('comments.author')
.populate('likes');
return article;
} catch (err) {
throw err;
}
},
async findArticlesByAuthor(authorId) {
try {
const articles = await Article
.find({ author: authorId })
.populate('author')
.populate('likes');
return articles;
} catch (err) {
throw err;
}
},
async deleteArticle(articleId) {
try {
const article = await Article.findByIdAndDelete(articleId);
return article;
} catch (err) {
throw err;
}
},
async createComment(articleId, commentData) {
try {
const article = await Article.findById(articleId);
article.comments.push(commentData);
await article.save();
return article;
} catch (err) {
throw err;
}
},
async deleteComment(articleId, commentId) {
try {
const article = await Article.findById(articleId);
article.comments.pull({ _id: commentId });
await article.save();
return article;
} catch (err) {
throw err;
}
},
async setLike(articleId, userId) {
try {
const article = await Article.findById(articleId);
if(article.likes.includes(userId)) {
article.likes.pull({ _id: userId });
}
else {
article.likes.push(userId);
}
await article.save();
return article;
} catch (err) {
throw err;
}
......
......@@ -11,6 +11,15 @@ const userService = {
}
},
async findUserById(userId) {
try {
const user = await User.findById(userId);
return user;
} catch (err) {
throw err;
}
},
async findUserByEmail(email) {
try {
const user = await User.findOne({ email });
......@@ -19,6 +28,15 @@ const userService = {
throw err;
}
},
async existsByEmail(email) {
try {
const user = await User.exists({ email });
return user !== null;
} catch (err) {
throw err;
}
}
};
export default userService;
\ No newline at end of file
......@@ -53,7 +53,7 @@ app.post('/login', async (req, res) => {
const expires = moment().add(sessionTime.toString(),'m').toDate();
// 정보가 없다면 회원 가입 (강제?)
const user = await userService.findUserByEmail(req.body.email);
const user = await userService.existsByEmail(req.body.email);
if (!user) { // 유저가 없다면 회원 가입 후 세션 생성
let userProfilePicture = req.body.picture || null;
await userService.createUser({
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment