-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Bansalayush247/Bansalayush247-patch-1
Blog App Created
- Loading branch information
Showing
12 changed files
with
1,707 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
const mongoose = require("mongoose"); | ||
require("dotenv").config(); | ||
const dbConnect=()=>{ | ||
mongoose.connect(process.env.DATABASE_URL,{ | ||
useNewUrlParser:true, | ||
useUnifiedTopology:true, | ||
}) | ||
.then(()=>console.log("DB ka connection is SUccessfull")) | ||
.catch((error)=>{ | ||
console.log("Issue in DB connection"); | ||
console.error(error.message); | ||
process.exit(1); | ||
}); | ||
} | ||
|
||
module.exports=dbConnect; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
const BlogPost= require("../models/BlogPost"); | ||
const BlogLike= require("../models/BlogLike"); | ||
const BlogComment= require("../models/BlogComment"); | ||
|
||
//define route handler | ||
|
||
exports.createPost =async(req,res)=>{ | ||
try{ | ||
//extract title and description from request body | ||
const {title,description}=req.body; | ||
//Create a new Todo obj and insert in DB | ||
const response= await BlogPost.create({title,description}); | ||
|
||
// Send a json response with a success flag | ||
res.status(200).json({ | ||
success:true, | ||
data:response, | ||
message:"Entry Created Successfully" | ||
}) | ||
} | ||
catch(err){ | ||
console.error(err); | ||
console.log(err); | ||
res.status(500) | ||
.json({ | ||
success:false, | ||
data:"internal server error", | ||
message:err.message, | ||
}) | ||
} | ||
} | ||
|
||
exports.createLike =async(req,res)=>{ | ||
try{ | ||
//extract title and description from request body | ||
const {like}=req.body; | ||
//Create a new Todo obj and insert in DB | ||
const response= await BlogLike.create({like}); | ||
|
||
// Send a json response with a success flag | ||
res.status(200).json({ | ||
success:true, | ||
data:response, | ||
message:"Liked Successfully" | ||
}) | ||
} | ||
catch(err){ | ||
console.error(err); | ||
console.log(err); | ||
res.status(500) | ||
.json({ | ||
success:false, | ||
data:"internal server error", | ||
message:err.message, | ||
}) | ||
} | ||
} | ||
exports.createComment =async(req,res)=>{ | ||
try{ | ||
//fetch data from req body; | ||
|
||
const{post,user,body}=req.body; | ||
|
||
//create a comment object | ||
|
||
const comment = new BlogComment({ | ||
post,user,body | ||
}) | ||
|
||
//save the new comment into database | ||
|
||
const savedComment = await comment.save(); | ||
|
||
const updatedPost= await BlogPost.findByIdAndUpdate(post,{$push:{comments:savedComment._id}},{new:true}) | ||
.populate("comments") | ||
.exec(); | ||
// Send a json response with a success flag | ||
res.status(200).json({ | ||
success:true, | ||
post: updatedPost, | ||
message:"Entry Created Successfully" | ||
}) | ||
} | ||
catch(err){ | ||
console.error(err); | ||
console.log(err); | ||
res.status(500) | ||
.json({ | ||
success:false, | ||
data:"internal server error", | ||
message:err.message, | ||
}) | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
const BlogPost= require("../models/BlogPost"); | ||
const BlogLike= require("../models/BlogLike"); | ||
const BlogComment= require("../models/BlogComment"); | ||
|
||
exports.deletePost=async(req,res)=>{ | ||
try{ | ||
const{id}=req.params; | ||
const{title,description}=req.body; | ||
await BlogPost.findByIdAndDelete(id); | ||
|
||
res.json({ | ||
success:true, | ||
message:"Post Deleted", | ||
}) | ||
} | ||
catch(err) | ||
{ | ||
console.error(err); | ||
console.log(err); | ||
res.status(500) | ||
.json({ | ||
success:false, | ||
data:"internal server error", | ||
message:err.message, | ||
}) | ||
} | ||
|
||
} | ||
|
||
exports.deleteLike=async(req,res)=>{ | ||
try{ | ||
const{id}=req.params; | ||
const{like}=req.body; | ||
await BlogLike.findByIdAndDelete(id); | ||
|
||
res.json({ | ||
success:true, | ||
message:"Like Deleted", | ||
}) | ||
} | ||
catch(err) | ||
{ | ||
console.error(err); | ||
console.log(err); | ||
res.status(500) | ||
.json({ | ||
success:false, | ||
data:"internal server error", | ||
message:err.message, | ||
}) | ||
} | ||
|
||
} | ||
|
||
exports.deleteComment=async(req,res)=>{ | ||
try{ | ||
const{id}=req.params; | ||
const{comment}=req.body; | ||
await BlogComment.findByIdAndDelete(id); | ||
|
||
res.json({ | ||
success:true, | ||
message:"Comment Deleted", | ||
}) | ||
} | ||
catch(err) | ||
{ | ||
console.error(err); | ||
console.log(err); | ||
res.status(500) | ||
.json({ | ||
success:false, | ||
data:"internal server error", | ||
message:err.message, | ||
}) | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const BlogPost = require("../models/BlogPost"); | ||
//define route handle | ||
exports.getPost = async (req, res) => { | ||
try { | ||
//fetch all data from data base | ||
const blogs = await BlogPost.find({}); | ||
//response | ||
res.status(200).json({ | ||
success: true, | ||
data: blogs, | ||
message: "Entire blog Is fetched", | ||
}); | ||
|
||
} | ||
catch (err) { | ||
console.error(err); | ||
console.log(err); | ||
|
||
res.status(500) | ||
.json({ | ||
success: false, | ||
error: err.message, | ||
message: "Server Error", | ||
}); | ||
} | ||
} | ||
|
||
exports.getPostById = async (req, res) => { | ||
try { | ||
//extract blog items basis on id | ||
const id = req.params.id; | ||
const blog = await BlogPost.findById({ _id: id }); | ||
|
||
//data for fiven id not found | ||
if (!blog) { | ||
return res.status(404).json({ | ||
success: false, | ||
message: "No Data Found with given id", | ||
}) | ||
} | ||
|
||
//data found | ||
res.status(200).json({ | ||
success: true, | ||
data: blog, | ||
message: `blog ${id} successfully fetched`, | ||
}) | ||
} | ||
catch (err) { | ||
console.error(err); | ||
console.log(err); | ||
|
||
res.status(500) | ||
.json({ | ||
success: false, | ||
error: err.message, | ||
message: "Server Error", | ||
}); | ||
|
||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
const BlogPost= require("../models/BlogPost"); | ||
const BlogLike= require("../models/BlogLike"); | ||
const BlogComment= require("../models/BlogComment"); | ||
|
||
exports.updatePost=async(req,res)=>{ | ||
try{ | ||
const{id}=req.params; | ||
const{title,description}=req.body; | ||
const blog = await BlogPost.findByIdAndUpdate( | ||
{_id:id}, | ||
{title,description, updatedAt: Date.now()},) | ||
|
||
res.status(200).json({ | ||
success:true, | ||
data:blog, | ||
message:"post updated successfully", | ||
}) | ||
} | ||
catch(err) | ||
{ | ||
console.error(err); | ||
console.log(err); | ||
res.status(500) | ||
.json({ | ||
success:false, | ||
data:"internal server error", | ||
message:err.message, | ||
}) | ||
} | ||
|
||
} | ||
|
||
exports.updateLike=async(req,res)=>{ | ||
try{ | ||
const{id}=req.params; | ||
const{like}=req.body; | ||
const blogLike = await BlogLike.findByIdAndUpdate( | ||
{_id:id}, | ||
{like, updatedAt: Date.now()},) | ||
|
||
res.status(200).json({ | ||
success:true, | ||
data:blogLike, | ||
message:"like updated successfully", | ||
}) | ||
} | ||
catch(err) | ||
{ | ||
console.error(err); | ||
console.log(err); | ||
res.status(500) | ||
.json({ | ||
success:false, | ||
data:"internal server error", | ||
message:err.message, | ||
}) | ||
} | ||
|
||
} | ||
|
||
exports.updateComment=async(req,res)=>{ | ||
try{ | ||
const{id}=req.params; | ||
const{comment}=req.body; | ||
const blogComment = await BlogComment.findByIdAndUpdate( | ||
{_id:id}, | ||
{comment, updatedAt: Date.now()},) | ||
|
||
res.status(200).json({ | ||
success:true, | ||
data:blogComment, | ||
message:"comment updated successfully", | ||
}) | ||
} | ||
catch(err) | ||
{ | ||
console.error(err); | ||
console.log(err); | ||
res.status(500) | ||
.json({ | ||
success:false, | ||
data:"internal server error", | ||
message:err.message, | ||
}) | ||
} | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
const express=require("express"); | ||
const app=express(); | ||
|
||
|
||
require("dotenv").config(); | ||
const PORT = process.env.PORT||4000; | ||
|
||
app.use(express.json()); | ||
|
||
const blogRoutes=require("./routes/blogs"); | ||
|
||
app.use("/api/v1",blogRoutes); | ||
|
||
app.listen(PORT,()=>{ | ||
console.log("Port started successfully"); | ||
}) | ||
|
||
const dbConnect = require("./config/database"); | ||
dbConnect(); | ||
|
||
app.get("/",(req,res)=>{ | ||
res.send("<h1>HOMEPAGE</h1>"); | ||
}) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
const mongoose= require("mongoose"); | ||
const commentSchema = new mongoose.Schema( | ||
|
||
{ | ||
post:{ | ||
type:mongoose.Schema.Types.ObjectId, | ||
ref:"BlogPost", | ||
}, | ||
user:{ | ||
type:String, | ||
required:true, | ||
}, | ||
body:{ | ||
type:String, | ||
required:true, | ||
}, | ||
} | ||
); | ||
|
||
module.exports=mongoose.model("BlogComment",commentSchema); |
Oops, something went wrong.