Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file upload using Multer and Github #801

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions backend/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,9 @@ EMAIL_PASS=
ENCRYPTION_SECRET_KEY = # For encrypting data in db (must be 32 bits)
GOOGLE_CLIENT_ID=your_google_client_id
GOOGLE_CLIENT_SECRET=your_google_secret
GITHUB_TOKEN=
GITHUB_REPO_OWNER=
GITHUB_REPO_NAME=
GITHUB_IMAGES_PATH=
ENCRYPTION_SECRET_KEY=

175 changes: 175 additions & 0 deletions backend/package-lock.json

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

2 changes: 2 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"author": "",
"license": "ISC",
"dependencies": {
"axios": "^1.7.7",
"bcryptjs": "^2.4.3",
"cors": "^2.8.5",
"dotenv": "^16.0.3",
Expand All @@ -19,6 +20,7 @@
"jest": "^29.7.0",
"jsonwebtoken": "^9.0.2",
"mongoose": "^6.9.0",
"multer": "^1.4.5-lts.1",
"nodemailer": "^6.9.16",
"nodemon": "^2.0.20",
"passport": "^0.7.0",
Expand Down
32 changes: 32 additions & 0 deletions backend/routes/rent/rentProductRoutes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@

const express = require('express');
const router = express.Router();
const multer = require('multer');
const { uploadImageToGitHub } = require('../../services/rent/githubService');
const RentProduct = require('../../model/rent/rentProduct');
const productController = require('../../controllers/rent/RentProductController');
const upload = multer({ dest: 'uploads/' });


// Create a new product
router.post('/rent-products', productController.createProduct);
Expand All @@ -20,10 +25,37 @@ router.put('/rent-products/:id', productController.updateProduct);
router.delete('/rent-products/:id', productController.deleteProduct);


router.post('/rent-products-img', upload.single('image'), async (req, res) => {
const { name, description, price, rating, category } = req.body;

try {

const imageUrl = await uploadImageToGitHub(req.file);


const newProduct = new RentProduct({
name,
description,
price,
image: imageUrl,
rating,
category: category ? category.split(',') : [],
});

await newProduct.save();
res.status(201).json(newProduct);
} catch (error) {
console.error("Error creating product:", error);
res.status(500).json({ message: 'Error creating product' });
}
});



router.get('/filtered-rent-products', productController.getFilteredProducts );





module.exports = router;
Loading
Loading