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

Cloudinary Setup/IN #51

Merged
merged 20 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 12 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
136 changes: 136 additions & 0 deletions server/controllers/profile.js
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good job on documentation!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you sir!

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");
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, the try/catch here is actually not needed. Now, in other applications maybe but in this one we utilize express-async-handler which actually will catch thrown errors and respond with JSON instead of crashing the application. This actually doesn't give us any context into an error that occurs.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 try/catch for specific errors that are expected then of course we can keep that in!

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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.
Well noted!

});

// @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 }
Copy link
Contributor

Choose a reason for hiding this comment

The 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, {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice destructuring here and utilizing folders for organization!

Copy link
Collaborator

Choose a reason for hiding this comment

The 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 : syntax and rename destructured fields to better fit our conventions (example).

folder: "dogSittersAndOwnersPhotos"
});

profile.addPhoto(secure_url, "profilePic");

res.status(200).json({
msg: "Image uploaded successfully",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would do message instead of msg

});
});
30 changes: 30 additions & 0 deletions server/middleware/multer.js
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;
5 changes: 3 additions & 2 deletions server/models/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ const profileSchema = new mongoose.Schema({
default: "",
},
});
profileSchema.methods.setProflePic = function (imgUrl) {
this.photos.profilePic = imgUrl;

profileSchema.methods.addPhoto = function (imgUrl, name) {
this.photos.name = imgUrl;
this.save();
};

Expand Down
Loading