Skip to content

Commit 7b4af50

Browse files
committed
first commit
0 parents  commit 7b4af50

File tree

6,903 files changed

+567224
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

6,903 files changed

+567224
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/.env

controller/blogController.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
const Blog = require("../models/blogModel");
2+
const cloudinary = require('cloudinary');
3+
4+
exports.addBlog = async (req, res) => {
5+
try {
6+
const { title, img, description, public_id, user_id } = req.body;
7+
const blog = await Blog.create({ title, img, description, public_id, user_id });
8+
res.status(201).json({
9+
success: true,
10+
blog
11+
})
12+
} catch (error) {
13+
console.log(error);
14+
}
15+
}
16+
17+
exports.deleteBlog = async (req, res) => {
18+
try {
19+
const { _id, public_id } = req.body;
20+
cloudinary.uploader.destroy(public_id, ({ result }) => {
21+
console.log(result);
22+
});
23+
const blog = await Blog.findByIdAndDelete(_id);
24+
res.status(200).json({ success: true, blog });
25+
} catch (error) {
26+
console.log(error);
27+
}
28+
}
29+
30+
exports.updateBlog = async (req, res) => {
31+
try {
32+
33+
if (req.body.imgChange) {
34+
cloudinary.uploader.destroy(req.body.del_id, ({ result }) => {
35+
console.log(result);
36+
});
37+
}
38+
39+
const newBlog = await Blog.findByIdAndUpdate(req.body._id, req.body, {
40+
new: true
41+
});
42+
43+
res.status(200).json({
44+
success: true,
45+
newBlog
46+
})
47+
48+
} catch (error) {
49+
console.log(error);
50+
}
51+
}
52+
53+
exports.getBlogs = async (req, res) => {
54+
try {
55+
const blogs = await Blog.find();
56+
res.status(200).json({
57+
success: true,
58+
blogs
59+
})
60+
} catch (error) {
61+
console.log(error);
62+
}
63+
}

controller/userController.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const User = require("../models/userModel");
2+
const bcrypt = require('bcryptjs');
3+
const jwt = require("jsonwebtoken");
4+
5+
6+
exports.register = async (req, res) => {
7+
try {
8+
const { name, email, password } = req.body;
9+
const userfind = await User.findOne({ email });
10+
11+
if (userfind) {
12+
res.status(400).json({ success: false, message: "email is already registered !" });
13+
} else {
14+
const user = await User.create({ name, email, password });
15+
res.status(201).json({ sucess: true, user });
16+
}
17+
18+
} catch (error) {
19+
console.log(error);
20+
}
21+
}
22+
23+
24+
exports.login = async (req, res) => {
25+
try {
26+
const { email, password } = req.body;
27+
const user = await User.findOne({ email: email }).select("+password");
28+
29+
if (user) {
30+
const valid = await bcrypt.compare(password, user.password);
31+
32+
if (valid) {
33+
const token = await user.generateToken();
34+
res.status(200).json({ success: true, token, user });
35+
} else {
36+
res.status(400).json({ success: false, message: "Invalid password" })
37+
}
38+
39+
} else {
40+
res.status(400).json({ success: false, message: "Invalid email" });
41+
}
42+
43+
} catch (error) {
44+
console.log(error);
45+
}
46+
}
47+
48+
49+
exports.getUserData = async (req, res) => {
50+
try {
51+
const { token } = req.body;
52+
const { id } = jwt.verify(token, process.env.SECRET);
53+
const user = await User.findOne({ _id: id });
54+
if(user){
55+
res.status(200).json({ success: true, user });
56+
}else{
57+
res.status(400).json({success:false, message:"user not present"});
58+
}
59+
} catch (error) {
60+
console.log(error);
61+
}
62+
}

database/conn.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
const mongoose = require("mongoose");
2+
3+
mongoose.connect(process.env.DB).then(() => {
4+
console.log("connection established...!");
5+
}).catch((error) => {
6+
console.log(error);
7+
})

models/blogModel.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const mongoose = require("mongoose");
2+
3+
const blogSchema = new mongoose.Schema({
4+
title: {
5+
type: String,
6+
require: true,
7+
},
8+
description: {
9+
type: String,
10+
require: true
11+
},
12+
img: {
13+
type: String,
14+
require: true
15+
},
16+
public_id: {
17+
type: String,
18+
require: true
19+
},
20+
user_id: {
21+
type: String,
22+
}
23+
});
24+
25+
26+
const Blog = new mongoose.model("BLOG", blogSchema);
27+
module.exports = Blog;

models/userModel.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const mongoose = require("mongoose");
2+
const bcrypt = require('bcryptjs');
3+
const jwt = require("jsonwebtoken");
4+
5+
6+
const userSchema = new mongoose.Schema({
7+
name: {
8+
type: String,
9+
required: true
10+
},
11+
email: {
12+
type: String,
13+
required: true,
14+
unique: true
15+
},
16+
password: {
17+
type: String,
18+
required: true,
19+
select: false
20+
}
21+
});
22+
23+
userSchema.pre('save', async function (next) {
24+
if (!this.isModified("password")) {
25+
next();
26+
}
27+
this.password = await bcrypt.hash(this.password, 10);
28+
});
29+
30+
userSchema.methods.generateToken = function () {
31+
return jwt.sign({ id: this._id }, process.env.SECRET, {
32+
expiresIn: "7d",
33+
});
34+
};
35+
36+
37+
const User = new mongoose.model("USER", userSchema);
38+
module.exports = User;

node_modules/.bin/acorn

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/acorn.cmd

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/acorn.ps1

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node_modules/.bin/escodegen

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)