-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: created separate folder for controllers and imported them i…
…n routes
- Loading branch information
1 parent
cb24381
commit bbf51a8
Showing
2 changed files
with
95 additions
and
69 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,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' }); | ||
} | ||
}; |
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 |
---|---|---|
@@ -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; |