Skip to content

Commit

Permalink
add image upload feature
Browse files Browse the repository at this point in the history
  • Loading branch information
NIKHIL-PALL committed Apr 14, 2024
1 parent 48ab867 commit e02d8a0
Show file tree
Hide file tree
Showing 47 changed files with 463 additions and 171 deletions.
Binary file added Blogs/istockphoto-1147785920-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-1156304531-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-1164102275-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-1168019993-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-1198931639-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-1241341642-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-1241881284-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-1252684502-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-1302647935-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-1345541245-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-1408387701-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-655931794-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-860887528-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Blogs/istockphoto-876014518-612x612.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 24 additions & 3 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
const express = require("express");
const mongoose = require("mongoose");
const dotenv = require("dotenv");
const dotenv = require("dotenv");
const cors = require("cors");
const app = express();
const User = require("./model/User");
const UserRoutes = require("./routes/UserRoutes");
const BlogRoutes = require("./routes/BlogRoutes")

const multer = require("multer");
const path = require("path");
const User = require('./model/User')
const Blog = require('./model/Blogs')
const BlogsController = require('./contollers/BlogsController')
dotenv.config();
app.use(cors());
app.use(express.json())
app.use(express.static('public'));
app.use("/api/user", UserRoutes);

const storage = multer.diskStorage({
destination: (req, file, cb) => {
cb(null, "public/images");
},
filename: (req, file, cb) => {
const uniqueSuffix =
Date.now() +
"-" +
Math.round(Math.random() * 1e9) +
path.extname(file.originalname);
cb(null, file.fieldname + uniqueSuffix);
},
});
const upload = multer({ storage: storage });

app.post("/api/blog/create",upload.single('file'), BlogsController.createBlog);
app.use("/api/blog", BlogRoutes);

mongoose
Expand Down
4 changes: 3 additions & 1 deletion backend/contollers/BlogsController.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const getBlogById = async(req, res) => {
}

const createBlog = async (req, res) => {

const image = req.file.filename;
const { title, content, creator, author, date } = req.body;

try {
Expand All @@ -46,7 +48,7 @@ const createBlog = async (req, res) => {
return res.status(404).json({ message: "User not found" });
}

const newBlog = new Blog({ title, content, creator , author, date});
const newBlog = new Blog({ title, content, creator , author, date, image});
await newBlog.save();

user.blogs.push(newBlog);
Expand Down
2 changes: 1 addition & 1 deletion backend/model/Blogs.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const isoDate = currentDate.toISOString().split('T')[0];
const BlogSchema = new mongoose.Schema({
title : {type : String, required : true},
content : {type : String, required : true},
image : {type : String},
image : {type : String, required : true},
creator : {type : mongoose.Types.ObjectId, required : true, ref : 'User'},
author : {type : String},
date : {type : String}
Expand Down
145 changes: 145 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"dotenv": "^16.4.5",
"express": "^4.19.2",
"mongoose": "^8.3.1",
"multer": "^1.4.5-lts.1",
"uuid": "^9.0.1"
},
"devDependencies": {
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 9 additions & 5 deletions backend/routes/BlogRoutes.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@

const express = require("express");
const BlogsController = require("../contollers/BlogsController");
const router = express.Router();




// app.
router.get("/blogs", BlogsController.getAllBlogs);
router.get("/myblogs/:uid", BlogsController.getBlogsByUserId)
router.post("/create",BlogsController.createBlog);
router.get("/myblogs/:uid", BlogsController.getBlogsByUserId);
// router.post("/create", BlogsController.createBlog);

// router.post("/create", upload.single("file"), BlogsController.createBlog);
router.get("/:bid", BlogsController.getBlogById);
router.patch("/:bid", BlogsController.updateBlog);
router.delete("/:bid", BlogsController.deleteBlog)
router.delete("/:bid", BlogsController.deleteBlog);

module.exports = router;
module.exports = router;
7 changes: 5 additions & 2 deletions frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,24 @@ import EditBlog from "./components/pages/EditBlog";
function App() {
const [isLoggedIn , setIsLoggedIn] = useState(false);
const [userId, setUserId ] = useState(null);
const login = (userId) => {
const [name, setName] = useState("");
const login = (userId, name) => {
setIsLoggedIn(true);
setUserId(userId);
setName(name);
}

const logout = () => {
setIsLoggedIn(false);
setUserId(null);
setName("")
}

return (
<React.Fragment>
<BrowserRouter>
<AuthContext.Provider value={{isLoggedIn : isLoggedIn,login : login, logout : logout , userId : userId, name : name}}>
<Navbar />
<AuthContext.Provider value={{isLoggedIn : isLoggedIn,login : login, logout : logout , userId : userId}}>
<Routes>
<Route path='/' exact Component={Home}/>
<Route path="/blog" exact Component={Auth}/>
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/Contexts/AuthContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {createContext} from "react";
const AuthContext = createContext({
userId : null,
isLoggedIn : false,

name : "",
login : () => {},
logout : () => {}
})
Expand Down
Loading

0 comments on commit e02d8a0

Please sign in to comment.