# All files / Backend/controllers teacher.js

9.52% Statements 8/84
0% Branches 0/4
0% Functions 0/24
9.52% Lines 8/84

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
    1694x

    4x

    4x

    4x

    4x

    4x

    4x

    4x

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

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

       // console.log(req.file)
        const imageurl=req.file.path;
        const userId=req.body._id;
        const {title,category,name,willLearn,discription,discriptionLong,requirement,price} = req.body;

        console.log(userId,title)

        const course = new Course({
            title:title,
            category:category,
            imageurl:imageurl,
            name:name,
            willLearn:willLearn,
            discription:discription,
            discriptionLong:discriptionLong,
            requirement:requirement,
            rating:0,
            price:price,
            creator:userId,
        });

        course.save()
        .then(result=>{
            console.log(result);
            res.status(201).json({message:"Course created successfully",newCourse:result})
        })
        .catch(err =>{
            console.log(err);
        })

    }

    exports.uploadVideo = (req,res,next)=>{
        const courseId = req.params.courseID;
        console.log(req.files);
        const videos  = req.files;

        let videoContent = []

        Course.findOne({_id:courseId})
        .then(course=>{

            videos.forEach(video=>{
                let videoContentContainer = {
                    videoUrl:null,
                    usersWatched:[],
                }
                videoContentContainer.videoUrl = video.path;
                videoContent.push(videoContentContainer);
            })
            console.log(videoContent);
            course.videoContent=videoContent;
            course.save()
            .then(result=>{
                res.status(200).json({message:"successfully saved the video"})
            })
        })
        .catch(err=>{
            console.log(err);
        })
    }

    exports.watchedByUsers = (req,res,next)=>{
        const userId=req.body.userId;
        const videoId=req.body.videoId;
        const courseId=req.body.courseId;
        console.log(videoId);
        Course.findById({_id:courseId})
        .then(course=>{
            course.videoContent.every(video=>{
                console.log(video)
                if(video._id == videoId){
                    if(!video.usersWatched.includes(userId)){
                        video.usersWatched.push(userId);
                    }
                    console.log("matched found")
                    return false;
                }
                return true;
                console.log("ran")
            })
            course.save();
            console.log(course.videoContent)
            res.status(200).json({message:"ko"})
        })
        .catch(err=>{
            console.log(err)
        })
    }

    exports.teacherHome =(req,res,next)=>{
        userId = req.body.userId;
        Course.find({creator:userId})
        .then(course=>{
            res.status(200).json({data:course})
        })
        .catch(err=>{
            console.log(err);
        })
    }

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

        Course.findByIdAndRemove({_id:courseId})
        .then(course=>{
            res.status(200).json({message:"course deleted successfully"})
        })
        .catch(err=>{
            console.log(err)
        })
    }

    // editing course
    exports.editCourse = (req,res,next)=>{
        const courseId  = req.body.courseId;

        Course.findOne({_id:courseId})
        .then(course=>{
            res.status(200).json({course})
        })
        .catch(err=>{
            console.log(err)
        })
    }

    exports.updateCourse=(req,res,next)=>{
          console.log(req.file)
         const courseId=req.body.courseId;
         const title=req.body.title;
         const category=req.body.category;
         const imageurl=req.file.path;
         const name=req.body.name;
         const willLearn=req.body.willLearn;
         const discription=req.body.discription;
         const discriptionLong=req.body.discriptionLong;
         const requirement=req.body.requirement;
         //const userId=req.body._id;

         Course.findById({_id:courseId})
         .then(course=>{
             course.title=title;
             course.category=category;
             course.imageurl=imageurl;
             course.name=name;
             course.willLearn=willLearn;
             course.discription=discription;
             course.discriptionLong=discriptionLong;
             course.requirement=requirement;
             course.rating=0;

             course.save();
             res.status(201).json({message:"Course editted successfully"})

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

    }