From c17b4df22d780c9557f7676979f851c1c09bd93f Mon Sep 17 00:00:00 2001 From: Gamey001 Date: Fri, 8 Oct 2021 10:25:47 +0100 Subject: [PATCH 1/5] added cloudinary config --- server/controllers/profile.js | 136 ++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 server/controllers/profile.js diff --git a/server/controllers/profile.js b/server/controllers/profile.js new file mode 100644 index 0000000..67766a0 --- /dev/null +++ b/server/controllers/profile.js @@ -0,0 +1,136 @@ +const Profile = require("../models/Profile"); +const { cloudinary } = require("../utils/cloudinary"); +const asyncHandler = require("express-async-handler"); + +// @route POST /profile/create +// @desc Create new profile +// @access Public +exports.createProfile = asyncHandler(async (req, res, next) => { + try { + const { + firstName, + lastName, + gender, + birthday, + email, + phoneNumber, + location, + profilePic, + description, + availability, + } = req.body; + const profile = await Profile.create({ + firstName, + lastName, + gender, + birthday, + email, + phoneNumber, + location, + profilePic, + description, + availability, + }); + + res.status(201).json({ + profile, + }); + } catch (err) { + res.status(500); + throw new Error("Something went wrong, please try again"); + } +}); + +// @route PUT /profile +// @desc update a profile with given ID +// @access Private +exports.updateProfile = asyncHandler(async (req, res, next) => { + try { + const id = req.user._id; + + const { + firstName, + lastName, + gender, + birthday, + email, + phoneNumber, + location, + profilePic, + description, + availability, + } = req.body; + const profile = await Profile.findOneAndUpdate( + { user: id }, + { + firstName, + lastName, + gender, + birthday, + email, + phoneNumber, + location, + profilePic, + description, + availability, + }, + { new: true } + ); + res.status(200).json({ + profile, + }); + } catch (err) { + res.status(500); + throw new Error("Something went wrong, please try again"); + } +}); + +// @route GET /profile +// @desc gets a profile with the given ID +// @access Private +exports.getProfile = asyncHandler(async (req, res, next) => { + try { + const id = req.user._id; + const profile = await Profile.findOne({ user: id }); + + if (!profile) { + res.status(404); + throw new Error("This profile does not exist"); + } + res.status(200).json({ + profile, + }); + } catch (err) { + res.status(500); + throw new Error("Something went wrong, please try again"); + } +}); + +// @route GET /profile +// @desc get all profiles +// @access Private +exports.getAllProfiles = asyncHandler(async (req, res, next) => { + const profiles = await Profile.find(); + res.status(200).json({ + profiles, + }); +}); + +exports.uploadProfilePic = asyncHandler(async (req, res, next) => { + const { _id, file } = req.user; + const profile = await Profile.find({ user: _id }); + + if (!file) { + res.status(400); + throw new Error("Failed to upload photo, ensure that you have selected a valid file format") + } + const { secure_url } = await cloudinary.uploader.upload(file.path, { + folder: "dogSittersAndOwnersPhotos" + }); + + profile.addPhoto(secure_url, "profilePic"); + + res.status(200).json({ + msg: "Image uploaded successfully", + }); +}); From 82c4179826ebe3dd9294fb277b2451c3de4737fb Mon Sep 17 00:00:00 2001 From: Gamey001 Date: Fri, 8 Oct 2021 10:26:00 +0100 Subject: [PATCH 2/5] Create multer.js --- server/middleware/multer.js | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 server/middleware/multer.js diff --git a/server/middleware/multer.js b/server/middleware/multer.js new file mode 100644 index 0000000..9c846e8 --- /dev/null +++ b/server/middleware/multer.js @@ -0,0 +1,30 @@ +const multer = require("multer"); +const path = require("path"); + +const storage = multer.diskStorage({}); + +const upload = multer({ + storage, + limit: { fileSize: 1000000 }, + fileFilter: (req, file, cb) => { + checkFiletype(file, cb); + }, +}); + +const checkFiletype = (file, cb) => { + const { originalname, mimetype } = file; + const validExtName = /jpeg|jpg|png|gif/; + + const fileTypeCheck = validExtName.test( + path.extname(originalname).toLocaleLowerCase() + ); + const mimeTypeCheck = validExtName.test(mimetype); + + if (fileTypeCheck && mimeTypeCheck) { + return cb(null, true); + } else { + return cb(null, false); + } +}; + +module.exports = upload; From d60fbeeeb6239fbe1d4f6623a48398cd537a6e86 Mon Sep 17 00:00:00 2001 From: Gamey001 Date: Fri, 8 Oct 2021 10:26:11 +0100 Subject: [PATCH 3/5] Create cloudinary.js --- server/utils/cloudinary.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 server/utils/cloudinary.js diff --git a/server/utils/cloudinary.js b/server/utils/cloudinary.js new file mode 100644 index 0000000..bb561a1 --- /dev/null +++ b/server/utils/cloudinary.js @@ -0,0 +1,13 @@ +const cloudinary = require("cloudinary").v2; +require('dotenv').config(); + +const { CLOUDINARY_API_NAME, CLOUDINARY_API_KEY, CLOUDINARY_API_SECRET } = process.env; + +cloudinary.config({ + cloud_name: CLOUDINARY_API_NAME, + api_key: CLOUDINARY_API_KEY, + api_secret: CLOUDINARY_API_SECRET, + secure:true +}); + +module.exports = { cloudinary }; \ No newline at end of file From a3c85b8be68503fc6b15e43dd44cedbda7c3400d Mon Sep 17 00:00:00 2001 From: Gamey001 Date: Thu, 14 Oct 2021 00:38:38 +0100 Subject: [PATCH 4/5] Update profile.js --- server/controllers/profile.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/server/controllers/profile.js b/server/controllers/profile.js index 67766a0..6cb441c 100644 --- a/server/controllers/profile.js +++ b/server/controllers/profile.js @@ -117,7 +117,8 @@ exports.getAllProfiles = asyncHandler(async (req, res, next) => { }); exports.uploadProfilePic = asyncHandler(async (req, res, next) => { - const { _id, file } = req.user; + const { file } = req.body; + const { _id } = req.user; const profile = await Profile.find({ user: _id }); if (!file) { From 92760885148b676f8fea143a10f192e5bdd92eb5 Mon Sep 17 00:00:00 2001 From: Gamey001 Date: Thu, 14 Oct 2021 00:42:36 +0100 Subject: [PATCH 5/5] Update profile.js --- server/controllers/profile.js | 57 ++++++++++------------------------- 1 file changed, 16 insertions(+), 41 deletions(-) diff --git a/server/controllers/profile.js b/server/controllers/profile.js index 6cb441c..b6b029c 100644 --- a/server/controllers/profile.js +++ b/server/controllers/profile.js @@ -1,12 +1,11 @@ const Profile = require("../models/Profile"); -const { cloudinary } = require("../utils/cloudinary"); const asyncHandler = require("express-async-handler"); // @route POST /profile/create // @desc Create new profile // @access Public exports.createProfile = asyncHandler(async (req, res, next) => { - try { + const { firstName, lastName, @@ -19,6 +18,7 @@ exports.createProfile = asyncHandler(async (req, res, next) => { description, availability, } = req.body; + const profile = await Profile.create({ firstName, lastName, @@ -35,17 +35,12 @@ exports.createProfile = asyncHandler(async (req, res, next) => { res.status(201).json({ profile, }); - } catch (err) { - res.status(500); - throw new Error("Something went wrong, please try again"); - } }); // @route PUT /profile -// @desc update a profile with given ID +// @desc updates a profile with given ID // @access Private exports.updateProfile = asyncHandler(async (req, res, next) => { - try { const id = req.user._id; const { @@ -60,6 +55,7 @@ exports.updateProfile = asyncHandler(async (req, res, next) => { description, availability, } = req.body; + const profile = await Profile.findOneAndUpdate( { user: id }, { @@ -76,62 +72,41 @@ exports.updateProfile = asyncHandler(async (req, res, next) => { }, { new: true } ); + res.status(200).json({ profile, }); - } catch (err) { - res.status(500); - throw new Error("Something went wrong, please try again"); - } }); // @route GET /profile // @desc gets a profile with the given ID // @access Private exports.getProfile = asyncHandler(async (req, res, next) => { - try { const id = req.user._id; const profile = await Profile.findOne({ user: id }); if (!profile) { res.status(404); - throw new Error("This profile does not exist"); + throw new Error("The profile does not exist"); } + res.status(200).json({ profile, }); - } catch (err) { - res.status(500); - throw new Error("Something went wrong, please try again"); - } }); -// @route GET /profile -// @desc get all profiles +// @route GET /profiles +// @desc gets all profiles // @access Private exports.getAllProfiles = asyncHandler(async (req, res, next) => { const profiles = await Profile.find(); - res.status(200).json({ - profiles, - }); -}); -exports.uploadProfilePic = asyncHandler(async (req, res, next) => { - const { file } = req.body; - const { _id } = req.user; - const profile = await Profile.find({ user: _id }); - - if (!file) { - res.status(400); - throw new Error("Failed to upload photo, ensure that you have selected a valid file format") + if (!profiles) { + res.status(500); + throw new Error("0 results"); } - const { secure_url } = await cloudinary.uploader.upload(file.path, { - folder: "dogSittersAndOwnersPhotos" - }); - profile.addPhoto(secure_url, "profilePic"); - - res.status(200).json({ - msg: "Image uploaded successfully", - }); -}); + res.status(200).json({ + profiles, + }); +}); \ No newline at end of file