Skip to content

Commit

Permalink
refactor: created separate folder for controllers and imported them i…
Browse files Browse the repository at this point in the history
…n routes
  • Loading branch information
hemant14050 authored and krishnaacharyaa committed Nov 16, 2023
1 parent cb24381 commit bbf51a8
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 69 deletions.
77 changes: 77 additions & 0 deletions backend/controllers/posts-controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import Post from '../models/post.js';

export const createPostHandler = async (req, res) => {
try {
const post = new Post(req.body);
const savedPost = await post.save();
res.status(200).json(savedPost);
} catch (err) {
res.status(400).json({ message: err.message });
}
};

export const getAllPostsHandler = async (req, res) => {
try {
const posts = await Post.find();
res.status(200).status(200).json(posts);
} catch (err) {
res.status(500).json({ message: err.message });
}
};

export const getFeaturedPostsHandler = async (req, res) => {
try {
const featuredPosts = await Post.find({ isFeaturedPost: true });
res.status(200).json(featuredPosts);
} catch (err) {
res.status(500).json({ message: err.message });
}
};

export const getPostByCategoryHandler = async (req, res) => {
const category = req.params.category;
try {
const categoryPosts = await Post.find({ categories: category });
res.status(200).json(categoryPosts);
} catch (err) {
res.status(500).json({ message: err.message });
}
};

export const getLatestPostsHandler = async (req, res) => {
try {
const latestPosts = await Post.find().sort({ timeOfPost: -1 });
res.status(200).json(latestPosts);
} catch (err) {
res.status(500).json({ message: err.message });
}
};

export const getPostByIdHandler = async (req, res) => {
try {
const post = await Post.findById(req.params.id);
res.status(200).json(post);
} catch (err) {
res.status(404).json({ message: 'Post not found' });
}
};

export const updatePostHandler = async (req, res) => {
try {
const updatedPost = await Post.findByIdAndUpdate(req.params.id, req.body, {
new: true,
});
res.status(200).json(updatedPost);
} catch (err) {
res.status(404).json({ message: 'Post not found' });
}
};

export const deletePostByIdHandler = async (req, res) => {
try {
await Post.findByIdAndRemove(req.params.id);
res.status(200).json({ message: 'Post deleted' });
} catch (err) {
res.status(404).json({ message: 'Post not found' });
}
};
87 changes: 18 additions & 69 deletions backend/routes/posts.js
Original file line number Diff line number Diff line change
@@ -1,88 +1,37 @@
import { Router } from 'express';
const router = Router();
import Post from '../models/post.js';
import {
createPostHandler,
getAllPostsHandler,
getFeaturedPostsHandler,
getPostByCategoryHandler,
getLatestPostsHandler,
getPostByIdHandler,
updatePostHandler,
deletePostByIdHandler,
} from '../controllers/posts-controller.js';

// Create a new post
router.post('/', async (req, res) => {
try {
const post = new Post(req.body);
const savedPost = await post.save();
res.json(savedPost);
} catch (err) {
res.status(400).json({ message: err.message });
}
});
router.post('/', createPostHandler);

// Get all posts
router.get('/', async (req, res) => {
try {
const posts = await Post.find();
res.json(posts);
} catch (err) {
res.status(500).json({ message: err.message });
}
});
router.get('/', getAllPostsHandler);

// Route to get featured posts
router.get('/featured', async (req, res) => {
try {
const featuredPosts = await Post.find({ isFeaturedPost: true });
res.json(featuredPosts);
} catch (err) {
res.status(500).json({ message: err.message });
}
});
router.get('/featured', getFeaturedPostsHandler);

// Route to get posts by category
router.get('/categories/:category', async (req, res) => {
const category = req.params.category;
try {
const categoryPosts = await Post.find({ categories: category });
res.json(categoryPosts);
} catch (err) {
res.status(500).json({ message: err.message });
}
});
router.get('/categories/:category', getPostByCategoryHandler);

// Route for fetching the latest posts
router.get('/latest', async (req, res) => {
try {
const latestPosts = await Post.find().sort({ timeOfPost: -1 });
res.json(latestPosts);
} catch (err) {
res.status(500).json({ message: err.message });
}
});
router.get('/latest', getLatestPostsHandler);
// Get a specific post by ID
router.get('/:id', async (req, res) => {
try {
const post = await Post.findById(req.params.id);
res.json(post);
} catch (err) {
res.status(404).json({ message: 'Post not found' });
}
});
router.get('/:id', getPostByIdHandler);

// Update a post by ID
router.patch('/:id', async (req, res) => {
try {
const updatedPost = await Post.findByIdAndUpdate(req.params.id, req.body, {
new: true,
});
res.json(updatedPost);
} catch (err) {
res.status(404).json({ message: 'Post not found' });
}
});
router.patch('/:id', updatePostHandler);

// Delete a post by ID
router.delete('/:id', async (req, res) => {
try {
await Post.findByIdAndRemove(req.params.id);
res.json({ message: 'Post deleted' });
} catch (err) {
res.status(404).json({ message: 'Post not found' });
}
});
router.delete('/:id', deletePostByIdHandler);

export default router;

0 comments on commit bbf51a8

Please sign in to comment.