-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1831 from Sawan-Kushwah/backend/stories
Build Backend for Stories Page: Implement CURD operation Routes ✨🚀
- Loading branch information
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
236a1bc
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
word-wise – ./
word-wise-git-main-anshikas-projects-45924011.vercel.app
word-wise-pi.vercel.app
word-wise-anshikas-projects-45924011.vercel.app