# All files / Backend/controllers homepage.js

35% Statements 14/40
50% Branches 4/8
29.41% Functions 5/17
35% Lines 14/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    1034x
    4x

    4x
    1x

    1x

    4x
    4x

    4x
    1x

    1x

    3x

    3x

    4x

    4x

     const Course = require('../model/courses');
    const User  = require('../model/user')

    exports.allCourses = (req,res)=>{
        Course.find()
        .then(course=>{
            // console.log(course)
            res.status(200).json({course:course})
        })
        .catch(err=>{
            console.log(err)
        })
    }

    // this fetches courses based on the category

    exports.fetchCourses = (req,res,next)=>{
        const category = req.params.course;
       // console.log(category)
        if(category =="all" || category==""){
            Course.find()
            .then(courses=>{
               // console.log(courses);
                res.status(200).json({course:courses})
            })
            .catch(err=>{
                console.log(err)
                res.status(400).json({message:"error occured"})
            })
        }
        else{
            Course.find({category:category})
            .then(courses=>{
                //console.log(courses);
                res.status(200).json({course:courses})
            })
            .catch(err=>{
                console.log(err)
            })
        }
    }

    exports.preferenceCourses = (req,res,next)=>{
        const category = req.params.course;

        if(category=="preferences"){
            const userId = req.body.userId;
            let courseArray = []
            let no_of_course=0;

            User.findOne({_id:userId})
            .then(user=>{

                // user.preferences = [node, react]

                // console.log(user.preferences);
                user.preferences.forEach(preference=>{

                    Course.find({category:preference})
                    .then(course=>{
                        no_of_course++;
                        courseArray.push(...course);

                        if(no_of_course === user.preferences.length){
                            res.status(200).json({course:courseArray})
                        }

                    })
                    .catch(err=>{
                        console.log(err);
                    })
                })

            })

            .catch(err=>{
                console.log(err)
            })

        }
    }

    // taking preferences from user

    exports.getPreferences = (req,res,next)=>{
        const preferencesArray = req.body.interest;
        const userId = req.body.userId;

        User.findOne({_id:userId})
        .then(user=>{
            user.preferences=preferencesArray;
            user.save();

            return res.status(200).json({message:"Preference added"})
        })
        .catch(err=>{
            console.log(err);
            throw erorr;
        })

    }