Skip to content

Instantly share code, notes, and snippets.

@syamjayaraj

syamjayaraj/posts.js

Created Apr 22, 2021
Embed
What would you like to do?
const express = require("express");
const Post = require("../models/Post");
const router = express.Router();
router.post("/", async (req, res) => {
try {
let post = new Post(req.body);
post = await post.save();
res.status(200).json({
status: 200,
data: post,
});
} catch (err) {
res.status(400).json({
status: 400,
message: err.message,
});
}
});
router.get("/list", async (req, res) => {
try {
let posts = await Post.find();
res.status(200).json({
status: 200,
data: posts,
});
} catch (err) {
res.status(400).json({
status: 400,
message: err.message,
});
}
});
router.get("/:postId", async (req, res) => {
try {
let post = await Post.findOne({
_id: req.params.postId,
});
if (post) {
res.status(200).json({
status: 200,
data: post,
});
}
res.status(400).json({
status: 400,
message: "No post found",
});
} catch (err) {
res.status(400).json({
status: 400,
message: err.message,
});
}
});
router.put("/:postId", async (req, res) => {
try {
let post = await Post.findByIdAndUpdate(req.params.postId, req.body, {
new: true,
});
if (post) {
res.status(200).json({
status: 200,
data: post,
});
}
res.status(400).json({
status: 400,
message: "No post found",
});
} catch (err) {
res.status(400).json({
status: 400,
message: err.message,
});
}
});
router.delete("/:postId", async (req, res) => {
try {
let post = await Post.findByIdAndRemove(req.params.postId);
if (post) {
res.status(200).json({
status: 200,
message: "Post deleted successfully",
});
} else {
res.status(400).json({
status: 400,
message: "No post found",
});
}
} catch (err) {
res.status(400).json({
status: 400,
message: err.message,
});
}
});
module.exports = router;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment