Skip to content

Commit

Permalink
Refactorizando código
Browse files Browse the repository at this point in the history
Refactorizado en el controlador de employees
  • Loading branch information
Jesús Mur Fontanals committed Jan 19, 2015
1 parent 060486d commit 33c1814
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 93 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
# ngUsers: Aplicación AngularJS sobre trabajadores, horas trabajadas y salarios.
## Desarrollo con proposito de aprendizaje.
# Aplicación AngularJS sobre trabajadores, horas trabajadas y salarios.
## Desarrollo con proposito de aprendizaje y práctica.

### Como configurar el entorno.

1. Instalar dependencias mediante el comando: npm install

2. Requerido tener instalado MongoDB para el almacenamiento de los empleados.

3. Introducir empleados mediante post del REST a la siguiente ruta http://localhost:3000/api/employees

### Issues a revisar.

* Resolver un error en el filtro del array .some (Archivo services.js).

### Próximas feautures a desarrollar.

* Metodos PUT y DELETE del controlador de employees.

### Licencia

MIT License (MIT) Copyright (c) 2014 @jesusmurf
MIT License (MIT) Copyright (c) 2015 @jesusmurf
22 changes: 19 additions & 3 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
//Este es un comentario
var express = require('express'),
path = require('path'),
morgan = require('morgan'),
Expand All @@ -16,6 +15,9 @@ app.use(bodyParser.urlencoded({
}));
app.use(bodyParser.json());

// Connect to the database
mongoose.connect('mongodb://localhost:27017/employees');

/* Error handling:
Deveplopment.
*/
Expand All @@ -42,20 +44,34 @@ if (app.get('env') === 'production') {
});
};


// Templating and static files
app.set('view engine', 'html');
app.set('views', __dirname + '/views');
app.use(express.static(path.join(__dirname, 'public')));
app.use('/bower_components', express.static(__dirname + '/bower_components'));

// Routes
var employeesController = require('./controllers/employees');
var router = express.Router();

app.get('/', function(req, res) {
res.render('index');
});

mongoose.connect('mongodb://localhost:27017/employees');
var router = express.Router();

router.route('/employees')
.get(employeesController.getEmployees)
.post(employeesController.postEmployee);

require('./routes/employee.js')(app,router);
router.route('/employees/:employee_id')
.put(employeesController.updateEmployee)
.delete(employeesController.deleteEmployee);

app.use('/api', router);

// Server connection
var port = 3000;
app.listen(port, function(err) {
if (err) {
Expand Down
76 changes: 76 additions & 0 deletions controllers/employees.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
var Employee = require('../models/employees.js');


exports.getEmployees = function(req, res) {
Employee.find(function(err, employees) {
if (err)
res.next(err);
res.status(200);
res.json(employees);
});
};

exports.postEmployee = function(req, res) {
var employee = new Employee({
first: req.body.first,
last: req.body.last,
street: req.body.street,
city: req.body.city,
state: req.body.state,
country: req.body.country,
postal_code: req.body.postal_code,
abilities: req.body.abilities,
data: {
worked_hours: req.body.worked_hours,
worked_hpd: req.body.worked_hpd,
salary: req.body.salary
}
});

employee.save(function(err, employee) {
if (err)
return next(err);
res.status(201);
res.json({
employee: employee,
message: 'Employee created!'
});
});
};

exports.updateEmployee = function(req, res) {
Employee.findById(req.params.employee_id, function(err, employee) {
if (err)
return next(err);

employee.first = req.body.first;
employee.last = req.body.last;
employee.street = req.body.street;
employee.city = req.body.city;
employee.state = req.body.state;
employee.country = req.body.country;
employee.postal_code = req.body.postal_code;
employee.abilities = req.body.abilities;
employee.data.worked_hours = req.body.worked_hours;
employee.data.worked_hpd = req.body.worked_hpd;
employee.data.salary = req.body.salary;

employee.save(function(err, employee) {
if (err)
return next(err);
res.status(200);
res.json({
message: 'Employee updated!'
});
});
});
};

exports.deleteEmployee = function(req, res) {
Employee.remove({_id: req.params.employee_id}, function(err, employee) {
if (err)
return next(err);
res.status(200);
res.json({message: 'Employee deleted!'});
});
};
83 changes: 0 additions & 83 deletions routes/employee.js

This file was deleted.

0 comments on commit 33c1814

Please sign in to comment.