Commit 10a06bf0 authored by D’jalmar Gutierrez Titirico's avatar D’jalmar Gutierrez Titirico 🚲

Añadiendo relaciones entre los modelos

parent d7c2e625
'use strict';
var express = require('express');
var controller = require('./tagPostulante.controller');
var router = express.Router();
router.get('/', controller.index);
router.get('/:id', controller.show);
router.post('/', controller.create);
router.put('/:id', controller.update);
router.patch('/:id', controller.update);
router.delete('/:id', controller.destroy);
module.exports = router;
'use strict';
var proxyquire = require('proxyquire').noPreserveCache();
var tagPostulanteCtrlStub = {
index: 'tagPostulanteCtrl.index',
show: 'tagPostulanteCtrl.show',
create: 'tagPostulanteCtrl.create',
update: 'tagPostulanteCtrl.update',
destroy: 'tagPostulanteCtrl.destroy'
};
var routerStub = {
get: sinon.spy(),
put: sinon.spy(),
patch: sinon.spy(),
post: sinon.spy(),
delete: sinon.spy()
};
// require the index with our stubbed out modules
var tagPostulanteIndex = proxyquire('./index.js', {
'express': {
Router: function() {
return routerStub;
}
},
'./tagPostulante.controller': tagPostulanteCtrlStub
});
describe('TagPostulante API Router:', function() {
it('should return an express router instance', function() {
expect(tagPostulanteIndex).to.equal(routerStub);
});
describe('GET /api/tagsPostulantes', function() {
it('should route to tagPostulante.controller.index', function() {
expect(routerStub.get
.withArgs('/', 'tagPostulanteCtrl.index')
).to.have.been.calledOnce;
});
});
describe('GET /api/tagsPostulantes/:id', function() {
it('should route to tagPostulante.controller.show', function() {
expect(routerStub.get
.withArgs('/:id', 'tagPostulanteCtrl.show')
).to.have.been.calledOnce;
});
});
describe('POST /api/tagsPostulantes', function() {
it('should route to tagPostulante.controller.create', function() {
expect(routerStub.post
.withArgs('/', 'tagPostulanteCtrl.create')
).to.have.been.calledOnce;
});
});
describe('PUT /api/tagsPostulantes/:id', function() {
it('should route to tagPostulante.controller.update', function() {
expect(routerStub.put
.withArgs('/:id', 'tagPostulanteCtrl.update')
).to.have.been.calledOnce;
});
});
describe('PATCH /api/tagsPostulantes/:id', function() {
it('should route to tagPostulante.controller.update', function() {
expect(routerStub.patch
.withArgs('/:id', 'tagPostulanteCtrl.update')
).to.have.been.calledOnce;
});
});
describe('DELETE /api/tagsPostulantes/:id', function() {
it('should route to tagPostulante.controller.destroy', function() {
expect(routerStub.delete
.withArgs('/:id', 'tagPostulanteCtrl.destroy')
).to.have.been.calledOnce;
});
});
});
/**
* Using Rails-like standard naming convention for endpoints.
* GET /api/tagsPostulantes -> index
* POST /api/tagsPostulantes -> create
* GET /api/tagsPostulantes/:id -> show
* PUT /api/tagsPostulantes/:id -> update
* DELETE /api/tagsPostulantes/:id -> destroy
*/
'use strict';
import _ from 'lodash';
import {TagPostulante} from '../../sqldb';
function respondWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function(entity) {
if (entity) {
res.status(statusCode).json(entity);
}
};
}
function saveUpdates(updates) {
return function(entity) {
return entity.updateAttributes(updates)
.then(updated => {
return updated;
});
};
}
function removeEntity(res) {
return function(entity) {
if (entity) {
return entity.destroy()
.then(() => {
res.status(204).end();
});
}
};
}
function handleEntityNotFound(res) {
return function(entity) {
if (!entity) {
res.status(404).end();
return null;
}
return entity;
};
}
function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
}
// Gets a list of TagPostulantes
export function index(req, res) {
TagPostulante.findAll()
.then(respondWithResult(res))
.catch(handleError(res));
}
// Gets a single TagPostulante from the DB
export function show(req, res) {
TagPostulante.find({
where: {
_id: req.params.id
}
})
.then(handleEntityNotFound(res))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Creates a new TagPostulante in the DB
export function create(req, res) {
TagPostulante.create(req.body)
.then(respondWithResult(res, 201))
.catch(handleError(res));
}
// Updates an existing TagPostulante in the DB
export function update(req, res) {
if (req.body._id) {
delete req.body._id;
}
TagPostulante.find({
where: {
_id: req.params.id
}
})
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(respondWithResult(res))
.catch(handleError(res));
}
// Deletes a TagPostulante from the DB
export function destroy(req, res) {
TagPostulante.find({
where: {
_id: req.params.id
}
})
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
}
/**
* TagPostulante model events
*/
'use strict';
import {EventEmitter} from 'events';
var TagPostulante = require('../../sqldb').TagPostulante;
var TagPostulanteEvents = new EventEmitter();
// Set max event listeners (0 == unlimited)
TagPostulanteEvents.setMaxListeners(0);
// Model events
var events = {
'afterCreate': 'save',
'afterUpdate': 'save',
'afterDestroy': 'remove'
};
// Register the event emitter to the model events
for (var e in events) {
var event = events[e];
TagPostulante.hook(e, emitEvent(event));
}
function emitEvent(event) {
return function(doc, options, done) {
TagPostulanteEvents.emit(event + ':' + doc._id, doc);
TagPostulanteEvents.emit(event, doc);
done(null);
}
}
export default TagPostulanteEvents;
'use strict';
var app = require('../..');
import request from 'supertest';
var newTagPostulante;
describe('TagPostulante API:', function() {
describe('GET /api/tagsPostulantes', function() {
var tagPostulantes;
beforeEach(function(done) {
request(app)
.get('/api/tagsPostulantes')
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) {
return done(err);
}
tagPostulantes = res.body;
done();
});
});
it('should respond with JSON array', function() {
expect(tagPostulantes).to.be.instanceOf(Array);
});
});
describe('POST /api/tagsPostulantes', function() {
beforeEach(function(done) {
request(app)
.post('/api/tagsPostulantes')
.send({
name: 'New TagPostulante',
info: 'This is the brand new tagPostulante!!!'
})
.expect(201)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) {
return done(err);
}
newTagPostulante = res.body;
done();
});
});
it('should respond with the newly created tagPostulante', function() {
expect(newTagPostulante.name).to.equal('New TagPostulante');
expect(newTagPostulante.info).to.equal('This is the brand new tagPostulante!!!');
});
});
describe('GET /api/tagsPostulantes/:id', function() {
var tagPostulante;
beforeEach(function(done) {
request(app)
.get('/api/tagsPostulantes/' + newTagPostulante._id)
.expect(200)
.expect('Content-Type', /json/)
.end((err, res) => {
if (err) {
return done(err);
}
tagPostulante = res.body;
done();
});
});
afterEach(function() {
tagPostulante = {};
});
it('should respond with the requested tagPostulante', function() {
expect(tagPostulante.name).to.equal('New TagPostulante');
expect(tagPostulante.info).to.equal('This is the brand new tagPostulante!!!');
});
});
describe('PUT /api/tagsPostulantes/:id', function() {
var updatedTagPostulante;
beforeEach(function(done) {
request(app)
.put('/api/tagsPostulantes/' + newTagPostulante._id)
.send({
name: 'Updated TagPostulante',
info: 'This is the updated tagPostulante!!!'
})
.expect(200)
.expect('Content-Type', /json/)
.end(function(err, res) {
if (err) {
return done(err);
}
updatedTagPostulante = res.body;
done();
});
});
afterEach(function() {
updatedTagPostulante = {};
});
it('should respond with the updated tagPostulante', function() {
expect(updatedTagPostulante.name).to.equal('Updated TagPostulante');
expect(updatedTagPostulante.info).to.equal('This is the updated tagPostulante!!!');
});
});
describe('DELETE /api/tagsPostulantes/:id', function() {
it('should respond with 204 on successful removal', function(done) {
request(app)
.delete('/api/tagsPostulantes/' + newTagPostulante._id)
.expect(204)
.end((err, res) => {
if (err) {
return done(err);
}
done();
});
});
it('should respond with 404 when tagPostulante does not exist', function(done) {
request(app)
.delete('/api/tagsPostulantes/' + newTagPostulante._id)
.expect(404)
.end((err, res) => {
if (err) {
return done(err);
}
done();
});
});
});
});
'use strict';
export default function(sequelize, DataTypes) {
return sequelize.define('TagPostulante', {
_id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
}
}, {
schema: 'public',
tableName: 'tag_postulante',
updatedAt: 'fecha_modificacion',
createdAt: 'fecha_creacion'
});
}
...@@ -16,6 +16,8 @@ export default function(app) { ...@@ -16,6 +16,8 @@ export default function(app) {
app.use('/api/tags', require('./api/tag')); app.use('/api/tags', require('./api/tag'));
app.use('/api/comentarios', require('./api/comentario')); app.use('/api/comentarios', require('./api/comentario'));
app.use('/api/cargos', require('./api/cargo')); app.use('/api/cargos', require('./api/cargo'));
app.use('/api/tagsPostulantes',require('./api/tagPostulante'));
// Todas las rutas de activos o api no definidas deben volver a 404 // Todas las rutas de activos o api no definidas deben volver a 404
app.route('/:url(api|auth|components|app|bower_components|assets)/*') app.route('/:url(api|auth|components|app|bower_components|assets)/*')
.get(errors[404]); .get(errors[404]);
......
...@@ -20,12 +20,12 @@ db.Postulacion = db.sequelize.import('../api/postulacion/postulacion.model'); ...@@ -20,12 +20,12 @@ db.Postulacion = db.sequelize.import('../api/postulacion/postulacion.model');
db.Tag = db.sequelize.import('../api/tag/tag.model'); db.Tag = db.sequelize.import('../api/tag/tag.model');
db.Comentario = db.sequelize.import('../api/comentario/comentario.model'); db.Comentario = db.sequelize.import('../api/comentario/comentario.model');
db.Cargo = db.sequelize.import('../api/cargo/cargo.model'); db.Cargo = db.sequelize.import('../api/cargo/cargo.model');
db.TagPostulante = db.sequelize.import('../api/tagPostulante/tagPostulante.model')
//Insertando las relaciones entre las tablas //Insertando las relaciones entre las tablas
db.Postulante.belongsToMany(db.TipoDato,{ db.Postulante.belongsToMany(db.TipoDato,{
foreignKey: 'fk_postulante', foreignKey: 'fk_postulante',
constraints: false,
as: 'Datos', as: 'Datos',
through: { through: {
model: db.DatoPostulante, unique:false model: db.DatoPostulante, unique:false
...@@ -33,11 +33,45 @@ db.Postulante.belongsToMany(db.TipoDato,{ ...@@ -33,11 +33,45 @@ db.Postulante.belongsToMany(db.TipoDato,{
}); });
db.TipoDato.belongsToMany(db.Postulante,{ db.TipoDato.belongsToMany(db.Postulante,{
foreignKey: 'fk_tipo_dato', foreignKey: 'fk_tipo_dato',
constraints: false,
as: 'Postulantes', as: 'Postulantes',
through:{ through:{
model:db.DatoPostulante, unique:false model:db.DatoPostulante, unique:false
} }
}); });
// relacion n-m entre Postulante y Cargo
db.Postulante.belongsToMany(db.Cargo, {
foreignKey: 'fk_postulante',
as: 'Postulaciones',
through: {model: db.Postulacion, unique: false}
});
db.Cargo.belongsToMany(db.Postulante, {
foreignKey: 'fk_cargo',
as: 'Postulantes',
through: {model: db.Postulacion, unique: false}
});
//relacion n-1 entre Comentario y Postulante
db.Postulante.hasMany(db.Comentario, {
foreignKey: 'fk_postulante',
as: 'Comentarios'
});
db.Comentario.belongsTo(db.Postulante, {
foreignKey: 'fk_postulante',
as: 'Postulante'
});
// relacion n-m entre Postulante y Tag
db.Postulante.belongsToMany(db.Tag, {
foreignKey: 'fk_postulante',
as: 'Tags',
through: {model: db.TagPostulante, unique: false}
});
db.Tag.belongsToMany(db.Postulante, {
foreignKey: 'fk_tag',
as: 'Postulantes',
through: {model: db.TagPostulante, unique: false}
});
export default db; export default db;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment