Merge pull request #15 from yashrajverma/yashrajverma

Edit Profile
This commit is contained in:
yashraj verma 2021-05-21 12:30:55 +05:30 committed by GitHub
commit 481d31b1ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 123 additions and 106 deletions

View File

@ -1,38 +1,35 @@
const Student = require('../models/Student') ; const Student = require("../models/Student");
const User = require('../models/User') ; const User = require("../models/User");
//function for trimming strings //function for trimming strings
function trim_arr(arr) function trim_arr(arr) {
{ let newArr = arr.map((a) => {
let newArr = arr.map(a => {
return a.trim(); return a.trim();
}) });
return newArr; return newArr;
} }
module.exports.getProfile = async (req, res, next) => { module.exports.getProfile = async (req, res, next) => {
try try {
{
//we can get the profile of the user including the courses which user has bought //we can get the profile of the user including the courses which user has bought
const userId = req.user._id; const userId = req.user._id;
let user = await User.findById(userId); let user = await User.findById(userId);
let student = await Student.findOne({user : userId}).populate("courses.basicInfo") ; let student = await Student.findOne({ user: userId }).populate(
"courses.basicInfo"
);
res.json({ res.json({
user: user, user: user,
student:student student: student,
}); });
} } catch (err) {
catch(err)
{
res.json({ res.json({
error:err error: err,
}); });
} }
} };
module.exports.postProfile = async (req, res, next) => { module.exports.postProfile = async (req, res, next) => {
try try {
{
//Here we are updating the profile of the user //Here we are updating the profile of the user
const userId = req.user._id; const userId = req.user._id;
let interests = req.body.interests; let interests = req.body.interests;
@ -43,61 +40,79 @@ module.exports.postProfile = async (req , res , next) => {
let skills = req.body.skills; let skills = req.body.skills;
let student = await Student.findOne({ user: userId }); let student = await Student.findOne({ user: userId });
if(interests) if (interests) {
{
student.interests = trim_arr(interests.split(",")); student.interests = trim_arr(interests.split(","));
} }
if(projects) if (projects) {
{
student.projects = trim_arr(projects.split(",")); student.projects = trim_arr(projects.split(","));
} }
if(skills) if (skills) {
{
student.skills = trim_arr(skills.split(",")); student.skills = trim_arr(skills.split(","));
} }
if(yearofgrad) if (yearofgrad) {
{
student.yearofgrad = yearofgrad.trim(); student.yearofgrad = yearofgrad.trim();
} }
if(phoneNumber) if (phoneNumber) {
{
student.phoneNumber = phoneNumber.trim(); student.phoneNumber = phoneNumber.trim();
} }
if(institute) if (institute) {
{
student.institute = institute.trim(); student.institute = institute.trim();
} }
await student.save(); await student.save();
res.json({ res.json({
message:"Updated profile" message: "Updated profile",
}) });
} } catch (err) {
catch(err)
{
console.log(err); console.log(err);
res.json({ res.json({
error:err error: err,
}); });
} }
} };
module.exports.deleteUser = async (req, res, next) => { module.exports.deleteUser = async (req, res, next) => {
//here we are deleting the user //here we are deleting the user
const userId = req.user._id; const userId = req.user._id;
try try {
{
await Student.deleteOne({ user: userId }); await Student.deleteOne({ user: userId });
await User.deleteOne({ _id: userId }); await User.deleteOne({ _id: userId });
res.json({ res.json({
message:"Successfully deleted" message: "Successfully deleted",
}); });
} } catch (err) {
catch(err)
{
console.log(err); console.log(err);
res.json({ res.json({
error:err 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("error", err);
res.status(422).json({ error: err });
return;
};
}
};

View File

@ -3,12 +3,12 @@ const router = express.Router();
const BlogController = require("../controllers/blog"); const BlogController = require("../controllers/blog");
const isAuth = require("../middleware/requirelogin"); 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; module.exports = router;

View File

@ -1,13 +1,15 @@
const express = require('express') ; const express = require("express");
const profileRoute = require('../controllers/profile') ; const profileRoute = require("../controllers/profile");
const authMiddleware = require('../middleware/requirelogin') ; 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);
router.post("/editUser", authMiddleware, profileRoute.editProfile);
module.exports = router; module.exports = router;