Skip to content

Commit

Permalink
created the entire blog controller
Browse files Browse the repository at this point in the history
  • Loading branch information
pramit-marattha committed Jan 15, 2021
1 parent dbef84c commit 180689c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 3 deletions.
58 changes: 56 additions & 2 deletions server/controllers/blog.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,57 @@
// for creating forms
const formidable = require("formidable");
// to create slugs
const slugify = require("slugify");
// stripping the html from the body to create excerpt
const stripHtml = require("string-strip-html");
// lodash for updating the blogs
const _ = require("lodash");
// models
const Blog = require("../models/blogSchema");
const Category = require("../models/categorySchema");
const Tag = require("../models/tagSchema");
// Handling errors (sending mongoose error to the client)
const {errorHandler} = require("../helpers/databaseErrorHandler");
// node js file system
const fs = require("fs");


exports.create = (req,res)=>{
res.json({time: Date().toString()})
}
let form = new formidable.IncomingForm()
form.keepExtensions = true
form.parse(req,(err,fields,files)=>{
if(err){
return res.status(400).json({
error: "Image Upload failed"
})
}
const {title,body,categories,taglists} = fields
let blog = new Blog()
blog.title = fields.title
blog.body = fields.body
blog.slug = slugify(title).toLowerCase()
blog.mtitle = `${title} | ${process.env.BLOG_NAME}`
blog.mdesc = stripHtml(body.substring(0,150))
blog.postedBy = req.user._id

// blog.categories = fields.categories
// blog.taglists = fields.taglists
if(files.photo){
if(files.photo.size > 10000000){
return res.status(400).json({
error: "Image size tooo large"
});
}
blog.photo.data = fs.readFileSync(files.photo.path)
blog.photo.contentType = files.photo.type
}
blog.save((err,result)=>{
if (err){
return res.status(400).json({
error: errorHandler(err)
});
}
res.json(result);
})
})
}
2 changes: 1 addition & 1 deletion server/controllers/category.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const Category = require("../models/categorySchema");
const slugify = require("slugify");
const {errorHandler} = require("../helpers/databaseErrorHandler")
const {errorHandler} = require("../helpers/databaseErrorHandler");

exports.create = (req,res)=>{
const {name} = req.body
Expand Down

0 comments on commit 180689c

Please # to comment.