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
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
backend/node_modules
backend/public
frontend/node_modules

cert.pem
key.pem
keytmp.pem
18 changes: 18 additions & 0 deletions backend/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
APP_NAME="backend_coding_assignment"
SECRET_KEY="Cm4kiCPKcO"
API_V1="/api"
PORT=3300
MONGO_CONNECTION_URL="mongodb+srv://root:root@cluster0.mkuko.mongodb.net/video-assignment?retryWrites=true&w=majority"

# documents name
CANDIDATE_COLLECTION="candidates"
QUESTION_COLLECTION="video_questions"
VIDEO_COLLECTION="videos"

# auth0
BASE_URL="https://localhost:3300/"
CLIENT_ID="DIhnDi09YRVFKjkmpJ66aWVrkco9zdNr"
ISSUER_URL="https://dev-1amdk4ya.us.auth0.com"

# FRONT END
FRONT_URL="http://localhost:3000/"
65 changes: 65 additions & 0 deletions backend/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
var createError = require('http-errors');
var express = require('express');
var path = require('path');
var cookieParser = require('cookie-parser');
var logger = require('morgan');
var cors = require('cors');

// auth0
const { auth } = require('express-openid-connect');
var configAuth = require('./config/auth');
// config env
var dotenv = require('dotenv').config();
// setup the connection
var { connect } = require('./config/connection');

var indexRouter = require('./routes/index');
var candidateRouter = require('./routes/candidate');
var questionRouter = require('./routes/question');
var videoRouter = require('./routes/video');

var app = express();

// set-auth0
app.use(auth(configAuth));
// connection-to-db
connect(process.env.MONGO_CONNECTION_URL);

// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');

app.use(cors());
app.use(logger('dev'));
app.use(express.json());
app.use(express.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));

const api = process.env.API_V1;
app.use('/', indexRouter);
app.use(`${api}/candidate`, candidateRouter);
app.use(`${api}/question`, questionRouter);
app.use(`${api}/video`, videoRouter);

app.get('/', (req, res) => {
res.send(req.oidc.isAuthenticated() ? 'Logged in' : 'Logged out');
});

// catch 404 and forward to error handler
app.use(function(req, res, next) {
next(createError(404));
});

// error handler
app.use(function(err, req, res, next) {
// set locals, only providing error in development
res.locals.message = err.message;
res.locals.error = req.app.get('env') === 'development' ? err : {};

// render the error page
res.status(err.status || 500);
res.render('error');
});

module.exports = app;
95 changes: 95 additions & 0 deletions backend/bin/www
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env node

/**
* Module dependencies.
*/

var app = require('../app');
var debug = require('debug')('backend:server');
var https = require('https');
var http = require('http');

const fs = require('fs');
// const key = fs.readFileSync('../key.pem');
// const cert = fs.readFileSync('../cert.pem');

/**
* Get port from environment and store in Express.
*/

var port = normalizePort(process.env.PORT || '3000');
app.set('port', port);

/**
* Create HTTP server.
*/
var server = http.createServer(app);
// var server = https.createServer({key, cert}, app);

/**
* Listen on provided port, on all network interfaces.
*/

server.listen(port);

server.on('error', onError);
server.on('listening', onListening);
/**
* Normalize a port into a number, string, or false.
*/

function normalizePort(val) {
var port = parseInt(val, 10);

if (isNaN(port)) {
// named pipe
return val;
}

if (port >= 0) {
// port number
return port;
}

return false;
}

/**
* Event listener for HTTP server "error" event.
*/

function onError(error) {
if (error.syscall !== 'listen') {
throw error;
}

var bind = typeof port === 'string'
? 'Pipe ' + port
: 'Port ' + port;

// handle specific listen errors with friendly messages
switch (error.code) {
case 'EACCES':
console.error(bind + ' requires elevated privileges');
process.exit(1);
break;
case 'EADDRINUSE':
console.error(bind + ' is already in use');
process.exit(1);
break;
default:
throw error;
}
}

/**
* Event listener for HTTP server "listening" event.
*/

function onListening() {
var addr = server.address();
var bind = typeof addr === 'string'
? 'pipe ' + addr
: 'port ' + addr.port;
debug('Listening on ' + bind);
}
14 changes: 14 additions & 0 deletions backend/config/auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

require('dotenv').config();

const config = {
authRequired: false,
auth0Logout: true,
secret: process.env.SECRET_KEY,
baseURL: process.env.BASE_URL,
clientID: process.env.CLIENT_ID,
issuerBaseURL: process.env.ISSUER_URL
};

module.exports = config;
15 changes: 15 additions & 0 deletions backend/config/connection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict';

module.exports.connect = async(url) => {
const mongoose = require('mongoose');
const urlConnection = url;

// connect it
await mongoose.connect(urlConnection, {useNewUrlParser: true, useUnifiedTopology: true});
// grab the connection
const db = await mongoose.connection;

// check the error connection
db.on('error', console.error.bind(console, 'Error connect to the database'));
console.log(`Connected to database ${db.name}`);
}
13 changes: 13 additions & 0 deletions backend/controllers/candidate.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const CandidateSchema = require('../models/candidate');

const getAll = async(req, res, next) => {
const candidates = await CandidateSchema.find().exec().catch(() => console.log);
//
res.status(200).json({data: candidates});
}

module.exports = {
getAllCandidate: getAll,
}
13 changes: 13 additions & 0 deletions backend/controllers/question.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
'use strict';

const QuestionSchema = require('../models/question');

const getAll = async(req, res, next) => {
const questions = await QuestionSchema.find().exec();
//
res.status(200).json({data: questions});
}

module.exports = {
getAllQuestion: getAll,
}
38 changes: 38 additions & 0 deletions backend/controllers/videos.controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
'use strict';

const { ObjectId } = require('bson');
const VideoSchema = require('../models/video');

const getAll = async(req, res, next) => {
const videos = await VideoSchema.find().exec();
//
res.status(200).json({data: videos});
}

const getByUserId = async(req, res, next) => {
const id = req.params.id;
const video = await VideoSchema.find({user_id: ObjectId(id)}).exec();

res.status(200).json({data: video});
}

const postVideo = async(req, res, next) => {
const userId = req.body.user_id;
const questionId = req.body.question_id;
const videoPath = req.body.video_path;

const data = {
user_id: ObjectId(userId),
question_id: ObjectId(questionId),
video_path: videoPath
};

const video = await VideoSchema.create(data);
res.status(200).json(video);
}

module.exports = {
getAllVideos: getAll,
getVideoByCandidate: getByUserId,
postVideo
}
20 changes: 20 additions & 0 deletions backend/models/candidate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

require('dotenv').config();
const mongoose = require('mongoose');

const CandidateSchema = new mongoose.Schema({
username: {
type: String,
required: true
},
password: {
type: String,
required: true
},
}, {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
collection: process.env.CANDIDATE_COLLECTION
});

const candidate = module.exports = mongoose.model(process.env.CANDIDATE_COLLECTION, CandidateSchema);
20 changes: 20 additions & 0 deletions backend/models/question.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
'use strict';

require('dotenv').config();
const mongoose = require('mongoose');

const QuestionSchema = new mongoose.Schema({
question: {
type: String,
required: true
},
max_time: {
type: Number,
default: 1
}
}, {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
collection: process.env.QUESTION_COLLECTION
});

module.exports = mongoose.model(process.env.QUESTION_COLLECTION, QuestionSchema);
26 changes: 26 additions & 0 deletions backend/models/video.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
'use strict';

require('dotenv').config();
const mongoose = require('mongoose');

const VideoSchema = new mongoose.Schema({
video_path: {
type: String,
required: true
},
user_id: {
type: mongoose.Schema.Types.ObjectId,
ref: process.env.CANDIDATE_COLLECTION,
required: true
},
question_id: {
type: mongoose.Schema.Types.ObjectId,
ref: process.env.QUESTION_COLLECTION,
required: true
},
}, {
timestamps: { createdAt: 'created_at', updatedAt: 'updated_at' },
collection: process.env.VIDEO_COLLECTION
});

module.exports = mongoose.model(process.env.VIDEO_COLLECTION, VideoSchema);
Loading