Skip to content

Commit

Permalink
Added backend api for story page
Browse files Browse the repository at this point in the history
  • Loading branch information
Sawan-Kushwah committed Nov 10, 2024
1 parent 7a70481 commit 698b458
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 6 deletions.
26 changes: 26 additions & 0 deletions hiring-portal/Backend/controllers/postsController.js
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 };
12 changes: 12 additions & 0 deletions hiring-portal/Backend/models/postModel.js
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;
9 changes: 9 additions & 0 deletions hiring-portal/Backend/routes/storiesRoutes.js
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;
4 changes: 4 additions & 0 deletions hiring-portal/Backend/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ const discussion = require('./routes/discussionRoutes')
const contactRoutes = require('./routes/contactRoutes');
const feedbackRoutes = require('./routes/feedbackRoutes');
const visitorRoutes = require('./routes/visitorRoutes');
const stories = require("./routes/storiesRoutes.js");



const blogRoutes = require('./routes/blogRoutes')
Expand Down Expand Up @@ -60,6 +62,8 @@ app.use("/api", contactRoutes);
app.use("/api", feedbackRoutes);
app.use("/api", visitorRoutes);
app.use("/api/discussion", discussion);
app.use("/api/stories", stories);




Expand Down
44 changes: 38 additions & 6 deletions hiring-portal/src/Components/Stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,50 @@ const Stories = () => {
const [category, setCategory] = useState('');

useEffect(() => {
const savedPosts = JSON.parse(localStorage.getItem('stories')) || [];
setPosts(savedPosts);
// Fetch posts from the backend API
const fetchPosts = async () => {
try {
const response = await fetch('http://localhost:5000/api/stories/getposts');
if (response.ok) {
const data = await response.json();
setPosts(data);
} else {
console.error("Failed to fetch posts");
}
} catch (error) {
console.error("Error fetching posts:", error);
}
};

fetchPosts();
}, []);

const handleSubmit = (e) => {
const handleSubmit = async (e) => {
e.preventDefault();

if (title && content && category) {
const newPost = { title, content, category, date: new Date().toISOString() };
const updatedPosts = [newPost, ...posts];
setPosts(updatedPosts);
localStorage.setItem('stories', JSON.stringify(updatedPosts));

try {
const response = await fetch('http://localhost:5000/api/stories/saveposts', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(newPost),
});

if (response.ok) {
const savedPost = await response.json();
setPosts([savedPost, ...posts]); // Add the new post to state
} else {
console.error("Failed to save the post");
}
} catch (error) {
console.error("Error saving the post:", error);
}

// Clear form fields
setTitle('');
setContent('');
setCategory('');
Expand Down

0 comments on commit 698b458

Please sign in to comment.