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

update profile #42

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
60 changes: 59 additions & 1 deletion controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,62 @@
const UserProfile = require('../models/UserProfile');
import User from "../models/User.js";

// Update user profile route handler
export async function updateUserProfile(req, res) {
try {
const userId = req.params.id;
const updates = req.body;

const updateFields = {};

if (updates.skills) {
updateFields.skills = updates.skills;
}

if (updates.bio) {
updateFields.bio = updates.bio;
}

if (updates.profilePicture) {
updateFields.profilePicture = updates.profilePicture;
}

if (updates.portfolio) {
updateFields.portfolio = updates.portfolio;
}

// Handle adding/removing skills separately
if (updates.addSkill || updates.removeSkill) {
const user = await User.findById(userId);

if (!user) {
return res.status(404).json({ message: "User not found" });
}

if (updates.addSkill) {
if (!user.skills.includes(updates.addSkill)) {
updateFields.skills = [...user.skills, updates.addSkill];
}
}

if (updates.removeSkill) {
updateFields.skills = user.skills.filter(skill => skill !== updates.removeSkill);
}
}

const updatedUser = await User.findByIdAndUpdate(userId, updateFields, { new: true });

if (!updatedUser) {
return res.status(404).json({ message: 'User not found' });
}

// Send the updated user as the response
return res.status(200).json(updatedUser);

} catch (error) {
console.error('Error updating user profile:', error);
return res.status(500).json({ message: 'Internal Server Error', error: error.message });
}

export async function handleCreateuser(req,res){
try {
const user = req.body;
Expand Down
4 changes: 2 additions & 2 deletions routes/userRoutes.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

import express from 'express';
const router = express.Router();
import {handleCreateuser} from '../controllers/userController.js'
router.post('/profile',handleCreateuser);

export default router;

export default router;
3 changes: 2 additions & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import dotenv from "dotenv";
import connectDb from "./config/db.js";
import User from "./models/User.js";
import sessionRoutes from "./routes/sessionRoutes.js";
import userRoutes from "./routes/userRoutes.js";

const app = express();

Expand Down Expand Up @@ -36,8 +37,8 @@ app.post("/register", async (req, res) => {
});
}
});

app.use("/session", sessionRoutes);
app.use("/api/users", userRoutes);

app.get("/", (req, res) => {
res.status(200).send("Hello World!");
Expand Down