-
Notifications
You must be signed in to change notification settings - Fork 76
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7a70481
commit 698b458
Showing
5 changed files
with
89 additions
and
6 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,26 @@ | ||
const Post = require("../models/postModel.js"); | ||
|
||
// Fetch all posts | ||
const getPosts = async (req, res) => { | ||
try { | ||
const posts = await Post.find(); | ||
res.status(200).json(posts); | ||
} catch (error) { | ||
res.status(500).json({ message: "Error fetching posts", error }); | ||
} | ||
}; | ||
|
||
// Save a new post | ||
const savePost = async (req, res) => { | ||
const { title, content, category, date } = req.body; | ||
|
||
try { | ||
const newPost = new Post({ title, content, category, date }); | ||
const savedPost = await newPost.save(); | ||
res.status(201).json(savedPost); | ||
} catch (error) { | ||
res.status(500).json({ message: "Error saving post", error }); | ||
} | ||
}; | ||
|
||
module.exports = { getPosts, savePost }; |
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,12 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const postSchema = new mongoose.Schema({ | ||
title: String, | ||
content: String, | ||
category: String, | ||
date: { type: Date, default: Date.now }, | ||
}); | ||
|
||
const Post = mongoose.model("Post", postSchema); | ||
|
||
module.exports = Post; |
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,9 @@ | ||
const express = require("express"); | ||
const { getPosts, savePost } = require("../controllers/postsController.js"); | ||
|
||
const router = express.Router(); | ||
|
||
router.get("/getposts", getPosts); | ||
router.post("/saveposts", savePost); | ||
|
||
module.exports = router; |
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
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