Skip to content

Commit

Permalink
Merge pull request #51 from hatchways/upload-profile-picture
Browse files Browse the repository at this point in the history
Cloudinary Setup/IN
  • Loading branch information
Gamey001 authored Oct 19, 2021
2 parents 9276088 + 88b2ddd commit 9337cd4
Show file tree
Hide file tree
Showing 5 changed files with 159 additions and 79 deletions.
131 changes: 62 additions & 69 deletions server/controllers/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,59 @@ const asyncHandler = require("express-async-handler");
// @desc Create new profile
// @access Public
exports.createProfile = asyncHandler(async (req, res, next) => {
const {
firstName,
lastName,
gender,
birthday,
email,
phoneNumber,
location,
profilePic,
description,
availability,
} = req.body;

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,
});
const profile = await Profile.create({
firstName,
lastName,
gender,
birthday,
email,
phoneNumber,
location,
profilePic,
description,
availability,
});

res.status(201).json({
profile,
});
res.status(201).json({
profile,
});
});

// @route PUT /profile
// @desc updates a profile with given ID
// @access Private
exports.updateProfile = asyncHandler(async (req, res, next) => {
const id = req.user._id;
const id = req.user._id;

const {
firstName,
lastName,
gender,
birthday,
email,
phoneNumber,
location,
profilePic,
description,
availability,
} = req.body;

const {
const profile = await Profile.findOneAndUpdate(
{ user: id },
{
firstName,
lastName,
gender,
Expand All @@ -54,59 +68,38 @@ exports.updateProfile = asyncHandler(async (req, res, next) => {
profilePic,
description,
availability,
} = req.body;
},
{ new: true }
);

const profile = await Profile.findOneAndUpdate(
{ user: id },
{
firstName,
lastName,
gender,
birthday,
email,
phoneNumber,
location,
profilePic,
description,
availability,
},
{ new: true }
);

res.status(200).json({
profile,
});
res.status(200).json({
profile,
});
});

// @route GET /profile
// @desc gets a profile with the given ID
// @access Private
exports.getProfile = asyncHandler(async (req, res, next) => {
const id = req.user._id;
const profile = await Profile.findOne({ user: id });
const id = req.user._id;
const profile = await Profile.findOne({ user: id });

if (!profile) {
res.status(404);
throw new Error("The profile does not exist");
}
if (!profile.length) return res.json({ profile });

res.status(200).json({
profile,
});
res.status(200).json({
profile,
});
});

// @route GET /profiles
// @desc gets all profiles
// @access Private
exports.getAllProfiles = asyncHandler(async (req, res, next) => {
const profiles = await Profile.find();
const profiles = await Profile.find();

if (!profiles) {
res.status(500);
throw new Error("0 results");
}
if (!profiles.length) return res.json({ profile });

res.status(200).json({
profiles,
});
});
res.status(200).json({
profiles,
});
});
2 changes: 1 addition & 1 deletion server/models/Profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@ profileSchema.methods.setProflePic = function (imgUrl) {
this.save();
};

module.exports = Profile = mongoose.model("profile", profileSchema);
module.exports = Profile = mongoose.model("profile", profileSchema);
2 changes: 2 additions & 0 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
},
"dependencies": {
"bcryptjs": "^2.4.3",
"cloudinary": "^1.27.0",
"colors": "^1.4.0",
"cookie-parser": "^1.4.5",
"dotenv": "^8.2.0",
Expand All @@ -21,6 +22,7 @@
"jsonwebtoken": "^8.5.1",
"mongoose": "^5.12.8",
"morgan": "^1.10.0",
"multer": "^1.4.3",
"nodemon": "^2.0.6",
"socket.io": "^4.1.0"
},
Expand Down
23 changes: 21 additions & 2 deletions server/routes/profile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,26 @@
const express = require("express");
const protect = require("../middleware/auth");
const upload = require("../middleware/multer");
const { validateMongoId, validateProfileDetails } = require("../validate");
const router = express.Router();
const { createProfile } = require("../controllers/profile");
const {
updateProfile,
getProfile,
getAllProfiles,
uploadProfilePic,
createProfile
} = require("../controllers/profile");

router.route("/create").post(createProfile);
router.route("/create").post(protect, validateProfileDetails, createProfile);

router
.route("/update")
.put(validateMongoId, validateProfileDetails, updateProfile);

router.route("/").get(protect, getProfile);

router.route("/upload").post(upload.single("profilePic"), uploadProfilePic);

router.route("/profiles").get(protect, getAllProfiles);

module.exports = router;
80 changes: 73 additions & 7 deletions server/validate.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
const { check, validationResult } = require("express-validator");

const msgPrefix = "Please enter a valid";
const genderOptions = ["MALE", "FEMALE", "OTHER"];
const checkFalsyVal = { checkFalsy: true };

exports.validateRegister = [
check("username", "Please enter a username").not().isEmpty(),
check("email", "Please enter a valid email address").isEmail(),
check("username", `${msgPrefix} username`).notEmpty(),
check("email", `${msgPrefix} email address`).isEmail(),
check(
"password",
"Please enter a password with 6 or more characters"
).isLength({
min: 6
min: 6,
}),
(req, res, next) => {
const errors = validationResult(req);
Expand All @@ -16,17 +20,79 @@ exports.validateRegister = [
if (!errors.isEmpty())
return res.status(400).json({ errors: errors.array() });
next();
}
},
];

exports.validateLogin = [
check("email", "Please enter a valid email address").isEmail(),
check("password", "Password is required").not().isEmpty(),
check("email", `${msgPrefix} email address`).isEmail(),
check("password", "Password is required").notEmpty(),
(req, res, next) => {
const errors = validationResult(req);

if (!errors.isEmpty())
return res.status(400).json({ errors: errors.array() });
next();
},
];

exports.validateProfileDetails = [
check("firstName", ` ${msgPrefix} first name`)
.exists(checkFalsyVal)
.trim()
.escape()
.isLength({ max: 20 }),
check("lastName", ` ${msgPrefix} last name`)
.exists(checkFalsyVal)
.trim()
.escape()
.isLength({ max: 20 }),
check("gender", ` ${msgPrefix} gender`)
.exists(checkFalsyVal)
.isIn(genderOptions),
check("birthday", ` ${msgPrefix} birthday format`)
.exists(checkFalsyVal)
.toDate(),
check("email", ` ${msgPrefix} email address`)
.exists(checkFalsyVal)
.trim()
.escape()
.isEmail(),
check("phoneNumber", ` ${msgPrefix} phone number`)
.exists(checkFalsyVal)
.trim()
.isMobilePhone(),
check("location", ` ${msgPrefix} location`)
.exists(checkFalsyVal)
.trim()
.escape()
.isLength({ max: 10 }),
check("profilePic", ` ${msgPrefix} image url`)
.exists(checkFalsyVal)
.escape()
.isURL(),
check("description", ` ${msgPrefix} description format`)
.exists(checkFalsyVal)
.escape()
.isLength({ max: 100 }),
check("availability", ` ${msgPrefix} availability format`)
.exists(checkFalsyVal)
.isArray(),
(req, res, next) => {
const errors = validationResult(req);

if (!errors.isEmpty())
return res.status(400).json({ errors: errors.array() });
next();
},
];

exports.validateMongoId = [
check("_id", "Invalid profile ID").exists(checkFalsyVal).isMongoId(),
(req, res, next) => {
const errors = validationResult(req);

if (!errors.isEmpty())
return res.status(400).json({ errors: errors.array() });
next();
}
},
];

0 comments on commit 9337cd4

Please sign in to comment.