Skip to content

Commit

Permalink
Update profile.js
Browse files Browse the repository at this point in the history
  • Loading branch information
Gamey001 committed Oct 13, 2021
1 parent a3c85b8 commit 9276088
Showing 1 changed file with 16 additions and 41 deletions.
57 changes: 16 additions & 41 deletions server/controllers/profile.js
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -19,6 +18,7 @@ exports.createProfile = asyncHandler(async (req, res, next) => {
description,
availability,
} = req.body;

const profile = await Profile.create({
firstName,
lastName,
Expand All @@ -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 {
Expand All @@ -60,6 +55,7 @@ exports.updateProfile = asyncHandler(async (req, res, next) => {
description,
availability,
} = req.body;

const profile = await Profile.findOneAndUpdate(
{ user: id },
{
Expand All @@ -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,
});
});

0 comments on commit 9276088

Please sign in to comment.