Last active
November 2, 2019 11:47
-
-
Save danielsmykowski1/da4c8e9314a46120854b79936b907984 to your computer and use it in GitHub Desktop.
This is the nodejs controller that creates, reads, updates and deletes construction notes. It also shows how to save, update and delete file contents using cloudinary server and how to convert html to pdf in nodejs by using html-pdf package.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const request = require("request"); | |
const fs = require('fs'); | |
const stream = require('stream'); | |
const pdf = require('html-pdf'); | |
const ConstructionNote = require('../models/constructionNote.model'); | |
const cloudinary = require('cloudinary').v2; | |
cloudinary.config({ | |
cloud_name: "your cloud name", | |
api_key: "your api key", | |
api_secret: "your api_secret" | |
}); | |
module.exports = { | |
// get the list of all construction notes | |
getAllConstructionNotes: async (req, res) => { | |
try { | |
let constructionNotes = await ConstructionNote.find().sort({updated: 'desc'}); | |
return res.status(200).json({ | |
message: "Construction Notes fetched successfully", | |
notes: constructionNotes | |
}); | |
} catch (err) { | |
console.log(err); | |
return res.status(500).json({ | |
message: "Internal server error" | |
}); | |
} | |
}, | |
// get a construction note with a specific ID | |
getConstructionNoteById: async (req, res) => { | |
try { | |
const { id } = req.params; | |
let constructionNote = await ConstructionNote.findById(id); | |
request.get(constructionNote.url, function (error, response, body) { | |
if (!error && response.statusCode == 200) { | |
return res.status(200).json({ | |
message: "Construction Note fetched successfully", | |
note: constructionNote, | |
content: body | |
}); | |
} | |
else { | |
return res.status(500).json({ | |
message: "Internal server error" | |
}) | |
} | |
}); | |
} catch (e) { | |
return res.status(500).json({ | |
message: "Internal server error" | |
}) | |
} | |
}, | |
// create a construction note or update the content of a construction note | |
saveConstructionNote: async (req, res) => { | |
try { | |
if (req.body.id == '') { | |
let constructionNote = await new ConstructionNote(); | |
constructionNote.userId = req.user._id; | |
constructionNote.createdBy = req.user.firstName + " " + req.user.lastName; | |
constructionNote.title = req.body.title; | |
constructionNote.type = 'note'; | |
const buffer = new Buffer(req.body.content); | |
// save the note content to the cloudinary cloud server | |
cloudinary.uploader.upload( | |
"data:text/plain;base64," + buffer.toString('base64'), | |
{ | |
resource_type: 'raw' | |
}, | |
async function(err, data) { | |
if (err) { | |
res.status(500).json({ | |
message: "Internal server error" | |
}); | |
} | |
else { | |
constructionNote.public_id = data.public_id; | |
constructionNote.url = data.secure_url; | |
constructionNote.created = Date.now(); | |
constructionNote.updated = Date.now(); | |
await constructionNote.save(); | |
res.status(200).json({ | |
message: "Construction Note created successfully", | |
id: constructionNote._id | |
}); | |
} | |
} | |
); | |
} | |
else { | |
let constructionNote = await ConstructionNote.findById(req.body.id); | |
const buffer = new Buffer(req.body.content); | |
// update the content of the note in the cloudinary cloud server | |
cloudinary.uploader.upload( | |
"data:text/plain;base64," + buffer.toString('base64'), | |
{ | |
resource_type: 'raw', | |
public_id: constructionNote.public_id, | |
overwrite: true | |
}, | |
async function(err, data) { | |
if (err) { | |
res.status(500).json({ | |
message: "Internal server error" | |
}); | |
} | |
else { | |
constructionNote.updated = Date.now(); | |
constructionNote.url = data.secure_url; | |
await constructionNote.save(); | |
res.status(200).json({ | |
message: "Construction Note created successfully", | |
id: constructionNote._id | |
}); | |
} | |
} | |
); | |
} | |
} catch (e) { | |
return res.status(500).json({ | |
message: "Internal server error" | |
}); | |
} | |
}, | |
// convert the html content of the note to pdf file and download it | |
downloadConstructionNote: async (req, res) => { | |
try { | |
const htmlheader = fs.readFileSync('constructionNotes/htmlheader.txt', 'utf8'); | |
const data = htmlheader + req.body.content + '</body></html>'; | |
var options = { | |
width: '597px', | |
height: '794px', | |
border: { | |
"top": "76px", | |
"right": "0", | |
"bottom": "76px", | |
"left": "0" | |
} | |
}; | |
pdf.create(data, options).toBuffer(function(err, buffer) { | |
if (err) { | |
return res.status(500).json({ | |
message: "Internal server error" | |
}); | |
} | |
else { | |
const readStream = new stream.PassThrough(); | |
readStream.end(buffer.toString('base64')); | |
res.set('Content-disposition', `attachment; filename=${req.body.title}.pdf` ); | |
res.set('Content-type', 'octet/stream'); | |
readStream.pipe(res); | |
} | |
}); | |
} catch (e) { | |
return res.status(500).json({ | |
message: "Internal server error" | |
}); | |
} | |
}, | |
// delete construction note info in the database | |
// and the note content in the cloudinary server | |
deleteConstructionNoteById: async (req, res) => { | |
try { | |
const {id} = req.params; | |
let constructionNote = await ConstructionNote.findById(id); | |
cloudinary.uploader.destroy( | |
constructionNote.public_id, | |
{ | |
resource_type: 'raw' | |
}, | |
async function(err, data) { | |
if (err) { | |
return res.status(500).json({ | |
message: "Internal server error" | |
}); | |
} | |
else { | |
await constructionNote.remove(); | |
return res.status(200).json({ | |
message: "Construction Note deleted successfully" | |
}); | |
} | |
} | |
); | |
} catch (e) { | |
return res.status(500).json({ | |
message: "Internal server error" | |
}); | |
} | |
}, | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment