Skip to content

Commit 4b730fe

Browse files
Merge pull request #11 from vishesh1525/vishesh
CHORE:Added the Backend for the post management and Scheduling
2 parents 73fef03 + 0cb041e commit 4b730fe

18 files changed

+2614
-18
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ dist-ssr/
1616

1717
# Local environment files
1818
*.local
19-
19+
.env
2020
# Editor directories and files
2121
.vscode/*
2222
!.vscode/extensions.json

Backend/.env_sample

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
MONGODB_URI=
2+
PORT=8080
3+
CLOUD_NAME
4+
CLOUD_API_KEY=
5+
CLOUD_API_KEY_SECRET=

Backend/app.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import express from "express";
2+
import cors from "cors";
3+
import cookieParser from "cookie-parser";
4+
import "./utils/cron.js";
5+
6+
const app = express();
7+
8+
app.use(
9+
cors({
10+
origin: '*', // Allows requests from any origin
11+
})
12+
);
13+
14+
app.use(express.json({ limit: "1000kb" }));
15+
app.use(express.urlencoded({ extended: true, limit: "1000kb" }));
16+
17+
app.use(express.static("public"));
18+
19+
app.use(cookieParser());
20+
21+
import loginroutes from "./routes/user.route.js";
22+
import postroutes from "./routes/post.route.js";
23+
24+
app.use('/api/users', loginroutes);
25+
app.use('/api/posts', postroutes);
26+
27+
export { app };
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import express from 'express';
2+
import Post from '../models/post.model.js';
3+
import User from '../models/user.model.js';
4+
5+
const createPost = async (req, res) => {
6+
try {
7+
const { userEmail, caption, platform, scheduledTime } = req.body;
8+
const photoUrl = req.file.path;
9+
10+
const user = await User.findOne({ email: userEmail }).select('_id');
11+
12+
if (!user) {
13+
return res.status(400).json({ message: "User is not registered" });
14+
}
15+
16+
const newPost = new Post({
17+
user: user._id,
18+
photoUrl,
19+
caption,
20+
platform,
21+
scheduledTime: new Date(scheduledTime)
22+
});
23+
24+
await newPost.save();
25+
26+
res.status(201).json({
27+
message: 'Post created successfully with file upload',
28+
post: newPost
29+
});
30+
31+
} catch (error) {
32+
res.status(500).json({
33+
message: 'Error creating post',
34+
error: error.message
35+
});
36+
}
37+
};
38+
39+
export default createPost;
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import bcrypt from 'bcryptjs';
2+
import User from '../models/user.model.js';
3+
4+
5+
export const registerUser = async (req, res) => {
6+
try {
7+
const { email, password } = req.body;
8+
9+
const existingUser = await User.findOne({ email });
10+
if (existingUser) {
11+
return res.status(400).json({ message: 'Email already in use' });
12+
}
13+
14+
const hashedPassword = await bcrypt.hash(password, 12);
15+
const newUser = new User({
16+
email,
17+
password: hashedPassword
18+
});
19+
20+
await newUser.save();
21+
22+
res.status(201).json({ message: 'User registered successfully' });
23+
} catch (error) {
24+
res.status(500).json({ message: 'Error registering user', error: error.message });
25+
}
26+
};
27+
28+
29+
export const loginUser = async (req, res) => {
30+
try {
31+
const { email, password } = req.body;
32+
33+
const user = await User.findOne({ email });
34+
if (!user) {
35+
return res.status(400).json({ message: 'User not found' });
36+
}
37+
38+
const isPasswordCorrect = await bcrypt.compare(password, user.password);
39+
if (!isPasswordCorrect) {
40+
return res.status(400).json({ message: 'Invalid credentials' });
41+
}
42+
43+
res.status(200).json({ message: 'Login successful' });
44+
} catch (error) {
45+
res.status(500).json({ message: 'Error logging in', error: error.message });
46+
}
47+
};

Backend/db/index.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import mongoose from "mongoose"
2+
3+
const connectionDB=async()=>{
4+
try {
5+
const connectioninstance=await mongoose.connect(`${process.env.MONGODB_URI}/blog}`)
6+
console.log(`\n Mongodb connected ${connectioninstance.connection.host}`)
7+
} catch (error) {
8+
console.log("MongoDB Connection Error",error)
9+
process.exit(1)
10+
11+
}
12+
}
13+
14+
export default connectionDB

Backend/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import {app} from "./app.js";
2+
import dotenv from "dotenv"
3+
import connectionDB from "./db/index.js";
4+
dotenv.config({
5+
path:"./.env"
6+
})
7+
connectionDB()
8+
.then(() => {
9+
console.log("Database connected");
10+
app.listen(process.env.PORT, () => {
11+
console.log(`Server is running on port ${process.env.PORT}`)
12+
}
13+
)})

Backend/models/post.model.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import mongoose from 'mongoose'
2+
3+
const postSchema = new mongoose.Schema({
4+
user: {
5+
type: mongoose.Schema.Types.ObjectId,
6+
ref: 'User',
7+
required: true
8+
},
9+
photoUrl: {
10+
type: String,
11+
required: true
12+
},
13+
caption: {
14+
type: String,
15+
required: true
16+
},
17+
platform: {
18+
type: String,
19+
required: true,
20+
enum: ['Facebook', 'Instagram', 'Twitter','Linkedln','Threads','Google Business','Tiktok','Youtube']
21+
},
22+
scheduledTime: {
23+
type: Date,
24+
required: true
25+
},
26+
published: { type: Boolean, default: false },
27+
createdAt: {
28+
type: Date,
29+
default: Date.now
30+
}
31+
});
32+
33+
const Post = mongoose.model('Post', postSchema);
34+
35+
export default Post;

Backend/models/user.model.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import mongoose, { Schema } from "mongoose";
2+
3+
const userSchema = new Schema({
4+
email: {
5+
type: String,
6+
required: true,
7+
unique: true,
8+
lowercase: true,
9+
trim: true,
10+
},
11+
12+
password: {
13+
type: String,
14+
required: [true, "password is required"], // message to frontend
15+
},
16+
17+
}, { timestamps: true });
18+
19+
const User = mongoose.model("User", userSchema); // create the document by this name
20+
21+
export default User

0 commit comments

Comments
 (0)