Skip to content

Instantly share code, notes, and snippets.

@Sampath-Lokuge
Created September 19, 2018 20:14
Show Gist options
  • Save Sampath-Lokuge/6fbfca86b7264144e58850859f778b7e to your computer and use it in GitHub Desktop.
Save Sampath-Lokuge/6fbfca86b7264144e58850859f778b7e to your computer and use it in GitHub Desktop.
handlers
const mongoose = require("mongoose")
mongoose.Promise = global.Promise
const config = require("../../config")[process.env.NODE_ENV || "development"]
const { prepareApiError, validateJwt } = require("../../lib")
const Playlist = require("../../models/playlist")
const Comment = require("../../models/comment")
module.exports = function (api) {
return {
get(req) {
mongoose.connect(config.mongoConnectionUrl, { useMongoClient: true })
return Playlist
.findOne({})
.exec()
.then((playlist) => {
mongoose.disconnect()
return Promise.resolve({ error: false, playlist })
})
.catch((e) => {
mongoose.disconnect()
const err = prepareApiError(e)
return new api.ApiResponse({ error: true, reason: err.reason }, { "Content-Type": "application/json" }, err.errorCode)
})
},
getcomment(req) {
const commentId = req.pathParams.id
mongoose.connect(config.mongoConnectionUrl, { useMongoClient: true })
return Comment
.findOne({ _id: commentId })
.populate("_commentBy", "_id email phone name profilePicUrl")
.exec()
.then((comment) => {
mongoose.disconnect()
return Promise.resolve({ error: false, comment })
})
.catch((error) => {
mongoose.disconnect()
const err = prepareApiError(error)
return new api.ApiResponse({ error: true, reason: err.reason }, { "Content-Type": "application/json" }, err.errorCode)
})
},
find(req) {
const vId = req.pathParams.id
mongoose.connect(config.mongoConnectionUrl, { useMongoClient: true })
return Comment
.find({ videoId: vId })
.populate("_commentBy", "_id email phone name profilePicUrl")
.exec()
.then((comments) => {
mongoose.disconnect()
comments.sort((a, b) => {
if (a.commentNo > b.commentNo) {
return -1
}
if (a.commentNo < b.commentNo) {
return 1
}
return 0
})
return Promise.resolve({ error: false, comments })
})
.catch((error) => {
mongoose.disconnect()
const err = prepareApiError(error)
return new api.ApiResponse({ error: true, reason: err.reason }, { "Content-Type": "application/json" }, err.errorCode)
})
},
post(req) {
if (!req.normalizedHeaders.authorization) {
return new api.ApiResponse({ error: true, reason: "Authorization Required" }, { "Content-Type": "application/json" }, 401)
}
const data = req.body
let useId
mongoose.connect(config.mongoConnectionUrl, { useMongoClient: true })
const token = req.normalizedHeaders.authorization.split(" ").pop()
return validateJwt(token)
.then((decoded) => {
useId = decoded.id
return Comment
.find({ videoId: data.videoId })
.exec()
})
.then((allComment) => {
data.commentNo = allComment.length + 1
data._commentBy = useId
const commentDetails = new Comment(data)
return commentDetails.save()
})
.then(savedComment => savedComment
.populate("_commentBy", "_id email phone name profilePicUrl")
.execPopulate())
.then((populatedComment) => {
mongoose.disconnect()
return Promise.resolve({ error: false, detail: populatedComment })
})
.catch((error) => {
mongoose.disconnect()
const err = prepareApiError(error)
return new api.ApiResponse({ error: true, reason: err.reason }, { "Content-Type": "application/json" }, err.errorCode)
})
},
put(req) {
if (!req.normalizedHeaders.authorization) {
return new api.ApiResponse({ error: true, reason: "Authorization Required" }, { "Content-Type": "application/json" }, 401)
}
const data = req.body
const commentId = req.pathParams.id
let useId
mongoose.connect(config.mongoConnectionUrl, { useMongoClient: true })
const token = req.normalizedHeaders.authorization.split(" ").pop()
return validateJwt(token)
.then((decoded) => {
useId = decoded.id
return Comment
.findOne({ _id: commentId, _commentBy: useId })
.exec()
})
.then((commentDetails) => {
if (commentDetails === null) {
throw new Error("No Such Comment!!")
} else if (data.commentText !== undefined) {
commentDetails.commentText = data.commentText
}
return commentDetails.save()
})
.then(savedComment => savedComment
.populate("_commentBy", "_id email phone name profilePicUrl")
.execPopulate())
.then((populatedComment) => {
mongoose.disconnect()
return Promise.resolve({ error: false, Comment: populatedComment, msg: "Comment details successfully edited" })
})
.catch((error) => {
mongoose.disconnect()
const err = prepareApiError(error)
return new api.ApiResponse({ error: true, reason: err.reason }, { "Content-Type": "application/json" }, err.errorCode)
})
},
delete(req) {
if (!req.normalizedHeaders.authorization) {
return new api.ApiResponse({ error: true, reason: "Authorization Required" }, { "Content-Type": "application/json" }, 401)
}
const commentId = req.pathParams.id
let useId
mongoose.connect(config.mongoConnectionUrl, { useMongoClient: true })
const token = req.normalizedHeaders.authorization.split(" ").pop()
return validateJwt(token)
.then((decoded) => {
useId = decoded.id
return Comment
.remove({ _id: commentId, _commentBy: useId })
.exec()
})
.then(() => {
mongoose.disconnect()
return Promise.resolve({ error: false, message: "Deleted successfully!!" })
})
.catch((error) => {
mongoose.disconnect()
const err = prepareApiError(error)
return new api.ApiResponse({ error: true, reason: err.reason, message: "Could not be deleted" }, { "Content-Type": "application/json" }, err.errorCode)
})
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment