diff --git a/controllers/profile.js b/controllers/profile.js index 6017015..662b62c 100644 --- a/controllers/profile.js +++ b/controllers/profile.js @@ -1,103 +1,118 @@ -const Student = require('../models/Student') ; -const User = require('../models/User') ; +const Student = require("../models/Student"); +const User = require("../models/User"); //function for trimming strings -function trim_arr(arr) -{ - let newArr = arr.map(a => { - return a.trim() ; - }) - return newArr ; +function trim_arr(arr) { + let newArr = arr.map((a) => { + return a.trim(); + }); + return newArr; } -module.exports.getProfile =async (req , res , next) => { - try - { - //we can get the profile of the user including the courses which user has bought - const userId = req.user._id ; - let user = await User.findById(userId) ; - let student = await Student.findOne({user : userId}).populate("courses.basicInfo") ; - res.json({ - user:user , - student:student - }) ; - } - catch(err) - { - res.json({ - error:err - }) ; - } -} +module.exports.getProfile = async (req, res, next) => { + try { + //we can get the profile of the user including the courses which user has bought + const userId = req.user._id; + let user = await User.findById(userId); + let student = await Student.findOne({ user: userId }).populate( + "courses.basicInfo" + ); + res.json({ + user: user, + student: student, + }); + } catch (err) { + res.json({ + error: err, + }); + } +}; -module.exports.postProfile = async (req , res , next) => { - try - { - //Here we are updating the profile of the user - const userId = req.user._id ; - let interests = req.body.interests ; - let projects = req.body.projects ; - let yearofgrad = req.body.yearofgrad ; - let phoneNumber = req.body.phoneNumber ; - let institute = req.body.institute ; - let skills = req.body.skills ; +module.exports.postProfile = async (req, res, next) => { + try { + //Here we are updating the profile of the user + const userId = req.user._id; + let interests = req.body.interests; + let projects = req.body.projects; + let yearofgrad = req.body.yearofgrad; + let phoneNumber = req.body.phoneNumber; + let institute = req.body.institute; + let skills = req.body.skills; - let student = await Student.findOne({user : userId}) ; - if(interests) - { - student.interests = trim_arr(interests.split(",")) ; - } - if(projects) - { - student.projects = trim_arr(projects.split(",")) ; - } - if(skills) - { - student.skills = trim_arr(skills.split(",")) ; - } - if(yearofgrad) - { - student.yearofgrad = yearofgrad.trim() ; - } - if(phoneNumber) - { - student.phoneNumber = phoneNumber.trim() ; - } - if(institute) - { - student.institute = institute.trim() ; - } - await student.save() ; + let student = await Student.findOne({ user: userId }); + if (interests) { + student.interests = trim_arr(interests.split(",")); + } + if (projects) { + student.projects = trim_arr(projects.split(",")); + } + if (skills) { + student.skills = trim_arr(skills.split(",")); + } + if (yearofgrad) { + student.yearofgrad = yearofgrad.trim(); + } + if (phoneNumber) { + student.phoneNumber = phoneNumber.trim(); + } + if (institute) { + student.institute = institute.trim(); + } + await student.save(); - res.json({ - message:"Updated profile" - }) - } - catch(err) - { - console.log(err); - res.json({ - error:err - }) ; - } -} + res.json({ + message: "Updated profile", + }); + } catch (err) { + console.log(err); + res.json({ + error: err, + }); + } +}; -module.exports.deleteUser = async (req , res , next) => { - //here we are deleting the user - const userId = req.user._id ; - try - { - await Student.deleteOne({user:userId}) ; - await User.deleteOne({_id : userId}) ; - res.json({ - message:"Successfully deleted" - }) ; +module.exports.deleteUser = async (req, res, next) => { + //here we are deleting the user + const userId = req.user._id; + try { + await Student.deleteOne({ user: userId }); + await User.deleteOne({ _id: userId }); + res.json({ + message: "Successfully deleted", + }); + } catch (err) { + console.log(err); + res.json({ + error: err, + }); + } +}; + +module.exports.editProfile = async (req, res, next) => { + const { _id, newFirstName, newLastName, email } = req.body; + try { + if (_id) { + let user = await User.findById({ _id }); + console.log("User", user); + if (user) { + user.firstName = newFirstName; + user.lastName = newLastName; + user.email = email; + user = await user.save(); + res.json({ message: "Profile Updated!" }); + } else { + res.status(422).json({ error: "User Not Found with that Id" }); + return; + } + } else { + res.status(422).json({ error: "User Id is Must" }); + return; } - catch(err) - { - console.log(err); - res.json({ - error:err - }) ; - } -} \ No newline at end of file + } catch { + (err) => { + console.log("error", err); + res.status(422).json({ error: err }); + return; + }; + } +}; diff --git a/routes/blog.js b/routes/blog.js index 9336f60..03e0a99 100644 --- a/routes/blog.js +++ b/routes/blog.js @@ -3,12 +3,12 @@ const router = express.Router(); const BlogController = require("../controllers/blog"); const isAuth = require("../middleware/requirelogin"); -router.post("/add-blog", BlogController.addBlog); +router.post("/addBlog", BlogController.addBlog); -router.get("/get-all-blogs", BlogController.getAllBlogs); +router.get("/getAllBlogs", BlogController.getAllBlogs); -router.post("/delete-blog/", BlogController.deleteBlog); +router.post("/deleteBlog/", BlogController.deleteBlog); -router.post("/edit-blog/", BlogController.editBlog); +router.post("/editBlog/", BlogController.editBlog); module.exports = router; diff --git a/routes/profile.js b/routes/profile.js index d28c137..adc3a2c 100644 --- a/routes/profile.js +++ b/routes/profile.js @@ -1,13 +1,15 @@ -const express = require('express') ; -const profileRoute = require('../controllers/profile') ; -const authMiddleware = require('../middleware/requirelogin') ; +const express = require("express"); +const profileRoute = require("../controllers/profile"); +const authMiddleware = require("../middleware/requirelogin"); -const router = express.Router() ; +const router = express.Router(); -router.get('/getProfile' , authMiddleware , profileRoute.getProfile) ; +router.get("/getProfile", authMiddleware, profileRoute.getProfile); -router.post('/postProfile' , authMiddleware , profileRoute.postProfile) ; +router.post("/postProfile", authMiddleware, profileRoute.postProfile); -router.post('/deleteUser' , authMiddleware , profileRoute.deleteUser) ; +router.post("/deleteUser", authMiddleware, profileRoute.deleteUser); -module.exports = router ; \ No newline at end of file +router.post("/editUser", authMiddleware, profileRoute.editProfile); + +module.exports = router;