# All files / Backend/controllers coursepage.js

51.43% Statements 54/105
33.33% Branches 2/6
60.87% Functions 14/23
51.43% Lines 54/105

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
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    1884x
    4x
    4x
    4x
    4x

    4x

    2x
    2x

    2x

    4x

    2x

    2x

    2x

    2x
    2x
    2x

    2x

    2x

    2x
    1x
    1x

    1x
    1x

    1x

    2x
    2x

    4x
    1x
    1x

    1x

    1x
    1x

    4x
    1x
    1x

    1x

    1x
    1x
    1x

    1x

    1x

    4x
    1x
    1x

    1x

    1x
    1x
    1x
    1x
    1x

    1x
    1x
    1x

    4x

     const Course = require('../model/courses');
    const User = require('../model/user');
    const PDFDocument = require('pdfkit');
    const fs = require('fs');
    const path= require('path')

    exports.CoursePage = (req,res,next)=>{
        // const courseName=req.params.courseName;
        const courseId=req.params.courseId;
        Course.findOne({_id:courseId})
        .then(course=>{
            res.status(200).json({course:course})
        })
        .catch(err=>{
            console.log(err)
            next()
        })

    }

    exports.Bookmark = (req,res,next)=>{

        const courseId=req.params.courseId;
        // const courseName=req.params.courseName;
        const userId = req.body._userID;

        User.findById({_id:userId})
        .then(user=>{
            Eif(!user.Bookmark.includes(courseId)){
                user.Bookmark.push(courseId);
                console.log("added to bookmark for user")
            }
            else{
                user.Bookmark.splice(user.Bookmark.indexOf(courseId),1);
                console.log('removed from user bookmark')
            }
            user.save()
            .then(()=>{
                Course.findById({_id:courseId})
                .then(course=>{
                    Eif(!course.bookmark.includes(userId)){
                        course.bookmark.push(userId);
                        console.log("bookmarked --- course")
                    }
                    else{
                        course.bookmark.splice(course.bookmark.indexOf(userId),1);
                    }
                    course.save();
                    console.log(user)
                })
                .catch(err=>{
                    console.log(err);
                })

            })

            console.log(user)
            res.status(202).json({message:"successfully bookmarked/unbookmarked"})
        })
        .catch(err=>{
            // console.log(err)
            throw err;
        })

    }

    exports.ShowBookmark =(req,res,next)=>{
        const userId = req.params.userId;
        console.log(userId)

        User.findById({_id:userId})
        .populate('Bookmark')
        .exec()
        .then(course=>{
            console.log(course)
            res.json({course:course})
        })
        .catch(err=>{
            console.log(err)
            next()
        })
    }

    exports.unbookmark=(req,res,next)=>{
        const userId= req.body.userId;
        const courseId=req.body.id;

        User.findById({_id:userId})
        .then(user=>{
            user.Bookmark.splice(user.Bookmark.indexOf(courseId),1);
            user.save();
            res.status(200).json({message:"successfully unbookmarked"})
        })
        .catch(err=>{
            console.log(err)
        })

        Course.findById({_id:courseId})
        .then(course=>{
            course.bookmark.splice(course.bookmark.indexOf(userId),1);
        })
        .catch(err=>{
            console.log(err)
            next()
        })
    }

    exports.rating=(req,res,next)=>{
        const courseId=req.body.courseId;
        const new_Rating=req.body.rating;

        Course.findById({_id:courseId})
        .then(course=>{
            const total_rating=course.rating.ratingSum+new_Rating;
            const times_updated=  course.rating.timesUpdated+1;
            course.rating.timesUpdated+=1;
            course.rating.ratingSum+=new_Rating;
            course.rating.ratingFinal= (total_rating/times_updated);

            course.save();
            console.log(course)
            res.status(200).json({course:course})
        })
        .catch(err=>{
            console.log(err);
            next();
        })

    }

    exports.pdf=(req,res,next)=>{
        const courseId=req.params.courseId;

        Course.findById({_id:courseId})
        .then(course=>{
            if(!course){
                res.status(400).json({message:"course doesnt exists!"})
            }

        const pdfName="invoice-"+courseId+'.pdf';
        const pdfPath = path.join('Files', pdfName);
        const pdfdoc = new PDFDocument();

        res.setHeader('Content-Type', 'application/pdf');
        res.setHeader(
          'Content-Disposition',
          'inline; filename="' + pdfName + '"'
        );
        pdfdoc.pipe(fs.createWriteStream(pdfPath));

        pdfdoc.pipe(res);
        pdfdoc.fontSize(20).text('HERE IS SOME DESCRIPTION AND TIPS ABOUT THE COURSE , HAVE A GREAT JOURNEY , EXPERIENCE BEST COURSES BY EXPERTIES! THANKYOU ');
        pdfdoc.moveDown();
        pdfdoc.fontSize(18).text('---------------CREATOR------------------');
        pdfdoc.moveDown();
        pdfdoc.text(course.name);
        pdfdoc.moveDown();
        pdfdoc.fontSize(18).text('------------DESCRIPTION-------------');
        pdfdoc.moveDown();
        pdfdoc.text(course.discription);
        pdfdoc.moveDown();
        pdfdoc.text('--------------------------------------------');
        pdfdoc.moveDown();
        pdfdoc.fontSize(18).text('TIPS');
        pdfdoc.text('--------------------------------------------');
        pdfdoc.text('1. Treat an online course like a “real” course.');
        pdfdoc.text('--------------------------------------------');
        pdfdoc.text('2. Hold yourself accountable');
        pdfdoc.text('--------------------------------------------');
        pdfdoc.text(' Practice time management.');
        pdfdoc.text('--------------------------------------------');
        pdfdoc.text('4. Create a regular study space and stay organized.');
        pdfdoc.text('--------------------------------------------');
        pdfdoc.text('5. Eliminate distractions.');
        pdfdoc.text('--------------------------------------------');
        pdfdoc.moveDown();
        pdfdoc.end();
        })
        .catch(err=>{
            console.log(err)
        })

    }