Skip to content

Commit

Permalink
setup cloudinary image upload
Browse files Browse the repository at this point in the history
  • Loading branch information
ebenezerdon committed Mar 7, 2019
1 parent 1292391 commit dcea290
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 10 deletions.
14 changes: 14 additions & 0 deletions 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 package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"body-parser": "^1.18.3",
"chai": "^4.2.0",
"chai-http": "^4.2.0",
"cloudinary": "^1.13.2",
"config": "^1.20.1",
"consign": "^0.1.6",
"cookie-parser": "~1.4.3",
Expand Down
21 changes: 21 additions & 0 deletions server/hepers/uploadToCloudinary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import cloudinary from 'cloudinary';

const uploadToCloudinary = async (imageFilePath) => {
cloudinary.config({
cloud_name: process.env.CLOUDINARY_CLOUD_NAME,
api_key: process.env.CLOUDINARY_API_KEY,
api_secret: process.env.CLOUDINARY_API_SECRET,
});

let response = '';
await cloudinary.v2.uploader
.upload(imageFilePath, (error, result) => {
if (error) response = error;
response = result.url;
})
.catch(error => error);

return response;
};

export default uploadToCloudinary;
8 changes: 5 additions & 3 deletions server/routes/controllers/productsController.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* import moment from 'moment'; */
import pool from '../../models/db';
import uploadToCloudinary from '../../hepers/uploadToCloudinary';

const getAllProducts = (req, res) => {
const text = 'SELECT * FROM products';
Expand Down Expand Up @@ -27,8 +28,9 @@ const getOneProduct = (req, res) => {
});
};

const addProduct = (req, res) => {
const addProduct = async (req, res) => {
const { body } = req;
const productImageUrl = await uploadToCloudinary(body.productimage);
const text = 'SELECT * FROM products WHERE productname = $1';
pool.query(text, [body.productname], (err, data) => {
if (data.rowCount) {
Expand All @@ -44,7 +46,7 @@ const addProduct = (req, res) => {
const values = [
body.productname,
body.description,
body.productimage,
productImageUrl,
body.price,
body.quantity,
body.minallowed,
Expand Down Expand Up @@ -115,4 +117,4 @@ export {
addProduct,
updateProduct,
deleteProduct,
};
};
14 changes: 7 additions & 7 deletions server/routes/controllers/usersController.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import jwt from 'jsonwebtoken';
import dotenv from 'dotenv';
import pool from '../../models/db';
import uploadToCloudinary from '../../hepers/uploadToCloudinary';

dotenv.config();
const secret = process.env.SECRET_KEY;
Expand All @@ -27,10 +28,9 @@ const getOneUser = (req, res) => {
});
};

const addUser = (req, res) => {
const {
body
} = req;
const addUser = async (req, res) => {
const { body } = req;
const userImageUrl = await uploadToCloudinary(body.userimage);
const text = 'SELECT * FROM users WHERE emailaddress = $1';
pool.query(text, [body.emailaddress], (err, data) => {
if (data.rowCount) {
Expand All @@ -47,7 +47,7 @@ const addUser = (req, res) => {
body.fullname,
body.emailaddress,
body.phonenumber,
body.userimage,
userImageUrl,
body.password,
body.role,
];
Expand All @@ -64,7 +64,7 @@ const addUser = (req, res) => {

const updateUser = (req, res) => {
const {
body
body,
} = req;
const text = `UPDATE users
SET fullname=$1, emailaddress=$2, phonenumber=$3, userimage=$4, password=$5, role=$6
Expand Down Expand Up @@ -159,4 +159,4 @@ export {
deleteUser,
loginUser,
getMyProfile,
}
};

0 comments on commit dcea290

Please sign in to comment.