Skip to content

Commit

Permalink
weekend fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
davt49 committed Jul 8, 2019
2 parents 09e83bf + 67c21b1 commit ee44223
Show file tree
Hide file tree
Showing 8 changed files with 760 additions and 51 deletions.
2 changes: 0 additions & 2 deletions config/router.js
Expand Up @@ -25,8 +25,6 @@ router.route('/gems')


// chats routes
router.route('/chats/:chatId/likes')
.get(secure, chats.like)

router.route('/chats/:chatId/comments/:commentId')
.delete(secure, chats.commentDelete)
Expand Down
46 changes: 26 additions & 20 deletions controllers/chats.js
@@ -1,4 +1,7 @@
const Chat = require('../models/chat')
require('dotenv').config()
const key = process.env.YANDEX_API_KEY
const axios = require('axios')

// <<< CHAT >>>
// INDEX
Expand All @@ -11,60 +14,63 @@ function indexRoute(req, res) {

// SHOW
function showRoute(req, res) {
req.body.user = req.currentUser

Chat
.find(req.params.id)
.findById(req.params.chatId)
.then(chat => {
if (req.currentUser.lang === 'vi') {
chat.comments.map(comment => {
axios.get(encodeURI(`https://translate.yandex.net/api/v1.5/tr.json/translate?key=${key}&text=${comment.text}&lang=en-vi`))
.then(comment => {
console.log(comment.data.text.join('+'))
})
})
}
console.log(chat.comments)
return chat.comments
})
.then(chat => res.status(200).json(chat))
.catch(err => res.status(404).json(err))
.catch(err => res.status(400).json(err))
}

//<<< CHAT COMMENTS >>>
// COMMENT: CREATE
function commentCreateRoute(req, res) {
req.body.user = req.currentUser
Chat
.create(req.body)
.populate('user')
.findById(req.params.chatId)
.then(chat => {
if (!chat) res.status(404).json({ message: 'Not found' })
chat.comments.push(req.body)
chat.save()
})
.then(chat => res.status(201).json(chat))
.chatch(err => res.status(422).json(err))
.catch(err => res.status(422).json(err))
}


// COMMENT: DELETE
function commentDeleteRoute(req, res) {
req.body.user = req.currentUser
Chat
.findById(req.params.id)
.findById(req.params.chatId)
.populate('user')
.then(chat => {
if (!chat) res.status(404).json({ message: 'Not found' })
const comment = chat.comments.id(req.params.commentId)
if (!comment) res.status(404).json({ message: 'Not found' })
if (!comment.user.equals(req.currentUser._id)) return res.status(401).json({ message: 'Unauthorized' })
comment.remove()
return chat.save()
})
.then(() => res.status(200).json({ message: 'Comment deleted' }))
.catch(err => res.status(422).json(err))
}

// <<< CHAT LIKES >>>
// LIKE
function likeRoute(req, res) {
Chat
.findById(req.params.id)
.populate('user')
.then(chat => {
if (!chat) res.status(404).json({ message: 'Not found' })
return chat.likes.push( { user: {} } )
})
}

module.exports = {
index: indexRoute,
show: showRoute,
commentCreate: commentCreateRoute,
commentDelete: commentDeleteRoute,
like: likeRoute
commentDelete: commentDeleteRoute
}
36 changes: 25 additions & 11 deletions controllers/gems.js
Expand Up @@ -10,7 +10,7 @@ function indexRoute(req, res) {

function showRoute(req, res) {
Gem
.findById(req.params.id)
.findById(req.params.gemId)
.populate('user')
.populate('comments.user')
.then(gem => {
Expand All @@ -21,38 +21,49 @@ function showRoute(req, res) {
}

function createRoute(req, res) {
req.body.user = req.currentUser
Gem
.create(req.body)
.then(gem => res.status(201).json(gem))
.catch(err => res.status(422).json(err))
.catch(err => {
console.log(err)
return res.status(422).json(err)
})
}

function editRoute(req, res) {
req.body.user = req.currentUser
Gem
.findById(req.params.id)
.findById(req.params.gemId)
.then(gem => {
console.log('here')
if (!gem) return res.status(404).json({ message: 'Not Found' })
console.log(gem.user)
console.log(req.currentUser)
if (!gem.user.equals(req.currentUser._id)) return res.status(401).json({ message: 'Unauthorized' })
Object.assign(gem, req.body)
return gem.save()
})
.then(gem => res.status(202).json(gem))
.then(gem => res.status(200).json(gem))
.catch(err => res.status(422).json(err))
}

function deleteRoute(req, res) {
req.body.user = req.currentUser
Gem
.findById(req.params.id)
.findByIdAndRemove(req.params.gemId)
.then(gem => {
if (!gem) return res.status(404).json({ message: 'Not Found' })
return gem.remove()
if (!gem.user.equals(req.currentUser._id)) return res.status(401).json({ message: 'Unauthorized' })
})
.then(() => res.status(204).json({ message: 'Deleted successfully ' }))
.catch(err => res.status(422).json(err))
}

function commentCreateRoute(req, res) {
req.body.user = req.currentUser
Gem
.findById(req.params.id)
.findById(req.params.gemId)
.then(gem => {
if (!gem) return res.status(404).json({ message: 'Not Found' })
gem.comments.push(req.body)
Expand All @@ -64,26 +75,29 @@ function commentCreateRoute(req, res) {


function commentDeleteRoute(req, res) {
req.body.user = req.currentUser
Gem
.findById(req.params.id)
.findById(req.params.gemId)
.then(gem => {
if (!gem) return res.status(404).json({ message: 'Not Found' })
const comment = gem.comments.id(req.params.commentId)
if (!comment) return res.status(404).json({ message: 'Not Found' })
if (!comment.user.equals(req.currentUser._id)) return res.status(401).json({ message: 'Unauthorized' })
comment.remove()
return gem.save()
})
.then(() => res.status(204).json({ message: 'Deleted successfully ' }))
.then(() => res.status(200).json({ message: 'Deleted successfully ' }))
.catch(err => res.status(401).json(err))
}

function likeRoute(req, res) {
req.body.user = req.currentUser
Gem
.findById(req.params.id)
.findById(req.params.gemId)
.populate('user')
.then(gem => {
if (!gem) return res.status(404).json({ message: 'Not Found' })
gem.likes.push({ user: {} })
gem.likes.push({ user: req.currentUser })
return gem.save()
})
.then(gem => res.status(200).json(gem))
Expand Down
6 changes: 6 additions & 0 deletions lib/logger.js
@@ -0,0 +1,6 @@
function logger (req, res, next) {
console.log(`${req.method} Request to ${req.url}`)
next()
}

module.exports = logger
4 changes: 3 additions & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "sei-project-03",
"version": "1.0.0",
"version": "1.0.0ççç",
"main": "index.js",
"license": "MIT",
"scripts": {
Expand All @@ -23,6 +23,7 @@
"file-loader": "^4.0.0",
"html-webpack-plugin": "^3.2.0",
"node-sass": "^4.12.0",
"nyc": "^14.1.1",
"sass-loader": "^7.1.0",
"style-loader": "^0.23.1",
"url-loader": "^2.0.1",
Expand All @@ -37,6 +38,7 @@
"body-parser": "^1.19.0",
"bulma": "^0.7.5",
"chai": "^4.2.0",
"dotenv": "^8.0.0",
"express": "^4.17.1",
"jsonwebtoken": "^8.5.1",
"mocha": "^6.1.4",
Expand Down

0 comments on commit ee44223

Please sign in to comment.