-
Notifications
You must be signed in to change notification settings - Fork 2
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
Cloudinary Setup/IN #51
Changes from 12 commits
ff5fd81
50311bf
235ae33
ced0e75
45a80b2
ea75ab5
d19ce2b
9d017e1
cbda0a6
67a1b49
3f8fde5
5e886ba
aabcb91
765edf5
b942c38
d30d2a5
3435cea
2af457b
3082dd4
88b2ddd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So, the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please remove in all applicable cases - now, let me mention. If we're There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @moffatethan My apologies, this was actually a mix up from an old version. You have made the correction in profile controllers and I have made the changes already. |
||
}); | ||
|
||
// @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 } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good work! |
||
); | ||
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, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice destructuring here and utilizing folders for organization! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just want to add here for future cases that we can often leverage the |
||
folder: "dogSittersAndOwnersPhotos" | ||
}); | ||
|
||
profile.addPhoto(secure_url, "profilePic"); | ||
|
||
res.status(200).json({ | ||
msg: "Image uploaded successfully", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would do |
||
}); | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
}; | ||
moffatethan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
module.exports = upload; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good job on documentation!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you sir!