2021-04-01 06:38:27 -07:00
|
|
|
const Student = require('../models/Student') ;
|
|
|
|
const User = require('../models/User') ;
|
|
|
|
|
2021-04-10 00:47:40 -07:00
|
|
|
//function for trimming strings
|
2021-04-01 06:38:27 -07:00
|
|
|
function trim_arr(arr)
|
|
|
|
{
|
|
|
|
let newArr = arr.map(a => {
|
|
|
|
return a.trim() ;
|
|
|
|
})
|
|
|
|
return newArr ;
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.getProfile =async (req , res , next) => {
|
|
|
|
try
|
|
|
|
{
|
2021-04-10 00:47:40 -07:00
|
|
|
//we can get the profile of the user including the courses which user has bought
|
2021-04-01 06:38:27 -07:00
|
|
|
const userId = req.user._id ;
|
|
|
|
let user = await User.findById(userId) ;
|
2021-04-10 00:47:40 -07:00
|
|
|
let student = await Student.findOne({user : userId}).populate("courses.basicInfo") ;
|
2021-04-01 06:38:27 -07:00
|
|
|
res.json({
|
|
|
|
user:user ,
|
|
|
|
student:student
|
|
|
|
}) ;
|
|
|
|
}
|
|
|
|
catch(err)
|
|
|
|
{
|
|
|
|
res.json({
|
|
|
|
error:err
|
|
|
|
}) ;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.postProfile = async (req , res , next) => {
|
|
|
|
try
|
|
|
|
{
|
2021-04-10 00:47:40 -07:00
|
|
|
//Here we are updating the profile of the user
|
2021-04-01 06:38:27 -07:00
|
|
|
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() ;
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
message:"Updated profile"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
catch(err)
|
|
|
|
{
|
|
|
|
console.log(err);
|
|
|
|
res.json({
|
|
|
|
error:err
|
|
|
|
}) ;
|
|
|
|
}
|
2021-04-02 01:36:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports.deleteUser = async (req , res , next) => {
|
2021-04-10 00:47:40 -07:00
|
|
|
//here we are deleting the user
|
2021-04-02 01:36:16 -07:00
|
|
|
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
|
|
|
|
}) ;
|
|
|
|
}
|
2021-04-01 06:38:27 -07:00
|
|
|
}
|