Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Backend/Controller/AdminAdd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const mongoose = require('mongoose')
const User = require ('../Modules/Users')
const Instructor = require('../Modules/Instructor')


const addAdmin = async(req,res) => {
console.log("admin")
const {FirstName, LastName, Gender, Username, Password, Email } = req.body
const users = await User.find({username:Username})
if (users.length){
return res.status(404).json("username already taken")
}const emailll = await User.find({email:Email})
if (emailll.length){
return res.status(404).json("This email is already signed in")
}
await User.create({fName :FirstName, lName: LastName,gender: Gender,username: Username, password: Password, type: "admin", email:Email})
console.log(Username)
return res.status(201).json("Sucess")
}

const addInstructor = async(req,res) => {
console.log("inst")
const {FirstName, LastName, Gender, Username, Password, Email} = req.body
const users = await User.find({username:Username})
if (users.length){
return res.status(404).json("username already taken")
}
const emailll = await User.find({email:Email})
if (emailll.length){
return res.status(404).json("This email is already signed in")
}
const uuu =await User.create({fName :FirstName, lName: LastName,gender: Gender,username: Username, password: Password, type: "instructor", email:Email})
await Instructor.create({user: uuu,rating:5,biography:"",amountOwed:0})
console.log(Username)
return res.status(201).json("Sucess")
}

const addCorporate = async (req,res) => {
console.log("corp")
const {FirstName, LastName, Gender, Username, Password, Email} = req.body
const users = await User.find({username:Username})
if (users.length){
return res.status(404).json("username already taken")
}
const emailll = await User.find({email:Email})
if (emailll.length){
return res.status(404).json("This email is already signed in")
}
await User.create({fName :FirstName, lName: LastName,gender: Gender,username: Username, password: Password, type: "corporate", email:Email})
console.log(Username)
return res.status(201).json("Sucess")
}

module.exports = {
addAdmin,addInstructor,addCorporate
}
82 changes: 82 additions & 0 deletions Backend/Controller/Courses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const mongoose = require('mongoose')
const Course = require ('../Modules/Course')
const countryToCurrency =require('country-to-currency')
const request = require('request')
const _External_URL = 'https://api.exchangerate.host/latest'
const axios = require('axios')

var r

const getCourses = async (req,res) => {
const course = await Course.find()
res.status(200).json(course)


}
const getInstructor = async(id) => {
if(id!==undefined){
const instructor = await Instructor.find({user:mongoose.Types.ObjectId(id)}).populate('user')
return instructor
}
}

const getOneCourse = async (req,res) =>{
const {id} = req.params
if(!mongoose.Types.ObjectId.isValid(id)){
return res.status(404).json({error:'no such course'})
}
const course = await Course.findById(id)
if(!course){
return res.status(404).json({error:'no such course'})
}
const instructor = getInstructor(course.instructor_id)
return res.status(200).json({course, instructor})

}

const addOneCourse = (req,res) => {
const {title , subtitle , price , summary } = req.body

}
// const currencyRate = (req ,res) => {
// apiRequest.callApi(function(response){
// const {rates} = response
// res.json({rates})

// })

// }

// const currencyRate = (callBack, curr) => {
// request(_External_URL ,{json:true}, async (res,err,body)=>{
// if(err) return (err)
// const{rates}=body
// console.log(body)
// for (let element in rates){
// if (element === curr)
// {
// r = rates[element]

// }

// }

// await Course.updateMany( { },
// { $set:{
// price : r
// }}
// )

// res.status(200).send(r)


// });
// }





module.exports = {
getCourses , getOneCourse , addOneCourse
}
31 changes: 31 additions & 0 deletions Backend/Controller/CurrencyRequest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const request = require('request')
_External_URL = 'https://api.exchangerate.host/latest'
const axios = require('axios')
const Courses = require('../Modules/Course')
const countryToCurrency =require('country-to-currency')
const countryCurrency = async (req,res ) => {
const {country , username} = req.body
const curr = countryToCurrency [country]
let cancel
axios.get(_External_UR ,{
cancelToken: new axios.cancelToken(c => cancel=c)
}).then(async function(response) {
const {rates}=response.data
console.log(rates);

for (let element in rates){
if (element === curr)
{
r = rates[element]

}

}

res.status(200).send([r,curr])
});


}

module.exports={countryCurrency}
57 changes: 57 additions & 0 deletions Backend/Controller/Instructor.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
const mongoose = require('mongoose')
const Course = require ('../Modules/Course')


// get instructor course + search instructor
const getInstructorCourses = async (req,res) => {
const {value} = req.query
const {instructor} = req.query //Get id from instructor frontend
if(instructor==undefined){
return res.status(404).json({error:'404'})
}
let course
if(value === undefined){
console.log(instructor)
course = await Course.find({instructor_id: instructor }).sort({enrolledStudents :-1}) //instructor = id
}
else{
course = await Course.find({$and:[{$or:[{title : { $regex: value ,$options: 'i' }},
{subject : { $regex: value,$options: 'i' }}]},
{instructor_id: instructor }]}).sort({enrolledStudents :-1}) //instructor = id
}


if(!course){
return res.status(404).json({error:'no Courses found!'})
}

return res.status(200).json(course)

}

const addOneCourse = async (req,res) => {
const {Title , Subtitle ,Subject , Price , Summary } = req.body
const coursee = await Course.find({title : Title})
console.log(req.body)
//console.log(coursee)
if (coursee.length){
console.log(coursee.length)
return res.status(404).json({error:'Course already exists'})
}
const {id} =req.params
console.log(id)
if(!mongoose.Types.ObjectId.isValid(id)){
return res.status(404).json({error:'no such instructor'})
}
var inst = mongoose.Types.ObjectId(id)
console.log(id)
const name = await User.findById(inst)
console.log(name)
console.log(name._id)
await Course.create({instructor: name.fName+" "+ name.lName, instructor_id: inst ,title: Title, subtitle: Subtitle,subject:Subject , price: Price, summary: Summary})
console.log(Title)
return res.status(201).json("Sucess")
}


module.exports = {getInstructorCourses,addOneCourse}
73 changes: 73 additions & 0 deletions Backend/Controller/Search.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
const mongoose = require('mongoose')
const { create } = require('../Modules/Course')
const Course = require ('../Modules/Course')

const creates = async() =>{
await Course.create({title:"title1",instructor:"inst1",subject:"subj1"})
await Course.create({title:"yiyle1",instructor:"ins1",subject:"subj1"})
}

// values for seaching by typing

const search = async (req , res) => {
const {input } = req.query
try {
if (input==='' ){

return res.status(200).send([])
}

else{
const course = await Course.find({$or:[{title : { $regex: input,$options: 'i' }},
{subject : { $regex: input ,$options: 'i'}},
{instructor: { $regex: input ,$options: 'i'} }]}).sort({rate :-1})
const arr = course.slice(0,10)
return res.status(200).send(arr)
}

}

catch (error){
res.status(404).send(error)
}

}

// values for seaching by clicking button (all searches)

const searchAll = async (req , res) => {
const {input} = req.query

try {
const course = await Course.find({$or:[{title : { $regex: input,$options: 'i' }},
{subject : { $regex: input ,$options: 'i'}},
{instructor: { $regex: input ,$options: 'i'} }]}).sort({rate :-1})
return res.status(200).send(course)
}

catch (error){
res.status(404).send('')
}
}

// get values to filter
const filterValues = async (req,res) =>{
var arr =[]
var subject =[]
var priceMin = Number.MAX_VALUE
var priceMax = Number.MIN_VALUE
const course = await Course.find()
course.forEach(course => {
if(!subject.includes(course.subject)) subject.push(course.subject)
if(course.price < priceMin) priceMin = course.price
if (course.price >priceMax) priceMax = course.price
})
arr.push(subject)
arr.push([priceMin,priceMax])
res.status(200).json(arr)
}


module.exports = {
search , searchAll , filterValues
}
77 changes: 77 additions & 0 deletions Backend/Modules/Course.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
const mongoose = require ('mongoose')
const Schema = mongoose.Schema

const questionsSchema = new Schema ({
number : String ,
question : String ,
choices : [String],
answer : String
}
)
const subtitleSchema = new Schema (
{
title : String,
video : [String],
totalHours : Number,
exercise : [questionsSchema]
})
const courseSchema = new Schema (
{
instructor : {
name :String ,

},
instructor_id :{
type :mongoose.Types.ObjectId , ref : 'Users' , required:true
},
title : {
type : String,
required: true
},
rating : {
type : Number ,
default : 5 ,
},
numberRating : {
type : Number ,
default : 0
},
totalHours :{
type : mongoose.Types.Decimal128,

},
subject :{
type :String ,
required : true
},
price : {
type : String ,
required:true
},
summary : {
type : String,
required:true
},
enrolledStudents :{
type:Number,
default:0
},
preview : {
type : String ,

} ,
subtitles :[subtitleSchema],

promotionInst : {
set : {type:Boolean,default:false},
value: mongoose.Types.Decimal128,
endDate: Date

},
promotionAdmin :{
set: {type:Boolean,default:false},
value:mongoose.Types.Decimal128
}
},{timestamps : true}
)
module.exports = mongoose.model('Course' ,courseSchema )
Loading