-
Notifications
You must be signed in to change notification settings - Fork 314
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
9eb78d6
commit 0a78b6a
Showing
7 changed files
with
116 additions
and
97 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
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,25 @@ | ||
// postsController.js | ||
import Post from "../models/postModel.js"; | ||
|
||
// Fetch all posts | ||
export 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 | ||
export 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 }); | ||
} | ||
}; |
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,13 @@ | ||
// postModel.js | ||
import mongoose from "mongoose"; | ||
|
||
const postSchema = new mongoose.Schema({ | ||
title: String, | ||
content: String, | ||
category: String, | ||
date: { type: Date, default: Date.now }, | ||
}); | ||
|
||
const Post = mongoose.model("Post", postSchema); | ||
|
||
export default 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 @@ | ||
import express from "express"; | ||
import { getPosts, savePost } from "../controllers/postsController.js"; | ||
|
||
const router = express.Router(); | ||
|
||
router.get("/getposts", getPosts); | ||
router.post("/saveposts", savePost); | ||
|
||
export default 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
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