This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var express = require('express'); | |
var router = express.Router(); | |
var employee = require("../controllers/employee-controller"); | |
// Get all employees | |
router.get('/', employee.list); | |
// Get single employee by id | |
router.get('/show/:id', employee.findById); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var EmployeeController = {}; | |
EmployeeController.list = function(req, res) { | |
res.send('find all'); | |
}; | |
EmployeeController.findById = function(req, res) { | |
res.send('find by id'); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var index = require('./server/routes/index'); // update index route | |
var employees = require('./server/routes/employees'); // update user route | |
app.set('views', path.join(__dirname, 'server/views')); // update views | |
app.use('/employees', employees); // update users to employees |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
var Schema = mongoose.Schema; | |
var employeeSchema = new Schema({ | |
firstName:{ type: String, required: true }, | |
lastName:{ type: String, required: true }, | |
email: { type: String, required: true }, | |
password: String | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost/nodeems', function (err) { | |
if (err) throw err; | |
console.log('Successfully connected to the database'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Employee = require("../models/employee"); | |
var EmployeeController = {}; | |
EmployeeController.list = function(req, res) { | |
Employee.find({}, function (err, employees) { | |
if (err) throw err; | |
res.render("../views/employee/index", {employees: employees}); | |
}); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Employee List</title> | |
<link rel='stylesheet' href='/stylesheets/style.css' /> | |
</head> | |
<body> | |
<div class="container"> | |
<h3><a href="/employees/create">Create Employee</a></h3> | |
<h1>Employee List</h1> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestControllerAdvice | |
public class ApiExceptionHandler { | |
@ExceptionHandler(value = { MethodArgumentTypeMismatchException.class }) | |
public ErrorResponse handleBadRequest(MethodArgumentNotValidException exception) { | |
BindingResult bindingResult = exception.getBindingResult(); | |
List<String> errors = bindingResult.getAllErrors() | |
.stream().map(DefaultMessageSourceResolvable::getDefaultMessage) | |
.collect(Collectors.toList()); | |
return new ErrorResponse(HttpStatus.BAD_REQUEST.value(), exception.getMessage(), errors); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void configure(HttpSecurity http) throws Exception { | |
// disabling csrf since this is REST | |
http.csrf().disable(); | |
// handle unauthorized access | |
http.exceptionHandling().authenticationEntryPoint(new UnauthorizedHandler()); | |
// no need to have a user session | |
http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); |