Last active
December 20, 2015 13:22
-
-
Save bookercodes/e286f870b6cfa40303ce to your computer and use it in GitHub Desktop.
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 app = express(); | |
var bodyParser = require('body-parser'); | |
var models = require("./models"); | |
var cors = require('cors'); | |
var seed = require("./seed"); | |
var Hall = models.Hall; | |
var Item = models.Item; | |
var Action = models.Action; | |
app.use(cors()); | |
app.use(bodyParser.urlencoded({extended: true})); | |
app.use(bodyParser.json()); | |
var port = 8080; | |
var router = express.Router(); | |
router.get('/halls', function (req, res) { | |
Hall.findAll({include: [{ | |
model: Item, | |
as: 'items', | |
include: [ | |
{ | |
model: Action, | |
as: 'actions' | |
} | |
] | |
}]}).then(function(halls){ | |
res.json(halls); | |
}); | |
}); | |
app.use('/api', router); | |
connection.sync().then(function () { | |
Hall.findAll() | |
.then(function (halls) { | |
if (halls.length == 0) { | |
seed(); | |
console.log('Seeding database'); | |
} else { | |
console.log('No seeding needed'); | |
} | |
app.listen(port); | |
console.log("Server listening at port " + port); | |
}); | |
}); | |
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 Sequelize = require('sequelize'); | |
var connection = new Sequelize('factory', 'factoryuser', 'factorypassword') | |
var Hall = connection.define('hall', { | |
name: { | |
type: Sequelize.STRING, | |
primaryKey: true | |
}, | |
area: Sequelize.STRING, | |
x: Sequelize.INTEGER, | |
y: Sequelize.INTEGER, | |
width: Sequelize.INTEGER, | |
height: Sequelize.INTEGER | |
}); | |
var Item = connection.define('item', { | |
name: Sequelize.STRING, | |
productnumber: Sequelize.STRING, | |
description: Sequelize.TEXT, | |
category: Sequelize.STRING, | |
x: Sequelize.INTEGER, | |
y: Sequelize.INTEGER, | |
width: Sequelize.INTEGER, | |
height: Sequelize.INTEGER | |
}); | |
Hall.hasMany(Item, {foreignKey: "hallName"}); | |
Item.belongsTo(Hall, {foreignKey: "hallName"}); | |
var Action = connection.define('action', { | |
date: Sequelize.STRING, | |
type: Sequelize.STRING, | |
description: Sequelize.TEXT | |
}) | |
Item.hasMany(Action, {foreignKey: "itemId"}); | |
Action.belongsTo(Item, {foreignKey: "itemId"}); | |
module.exports = { | |
Action: Action, | |
Hall: Hall, | |
Item: Item | |
}; |
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
module.exports = function seed(models) { | |
var Hall = models.Hall; | |
var Item = models.Item; | |
var Action = models.Action; | |
Hall.create({ | |
"name": "Fabricagehal", | |
"area": 50, | |
"x": 0, | |
"y": 0, | |
"width": 5, | |
"height": 10 | |
}).then(function (createdHall) { | |
const hallName = createdHall.dataValues.name; | |
Item.create({ | |
"name": "Machine", | |
"productnumber": "ABC-123", | |
"description": "Een machine die machinedingen doet", | |
"category": "machine", | |
"x": 1, | |
"y": 1, | |
"width": 1, | |
"height": 1, | |
"hallName": hallName | |
}).then(function (createdItem) { | |
const itemId = createdItem.dataValues.id; | |
Action.create({ | |
"date": "2015-12-16T18:25:43.511Z", | |
"type": "VERVANGING", | |
"description": "De machine moet vervangen worden", | |
"itemId": itemId | |
}); | |
}); | |
Item.create({ | |
"name": "Robot", | |
"productnumber": "EFG-456", | |
"description": "Een robot die robotdingen doet", | |
"category": "machine", | |
"x": 2, | |
"y": 1, | |
"width": 3, | |
"height": 1, | |
"hallName": hallName | |
}).then(function (createdItem) { | |
const itemId = createdItem.dataValues.id; | |
Action.create({ | |
"date": "2015-12-16T17:25:43.511Z", | |
"type": "NAZICHT", | |
"description": "De robot moet nagekeken worden", | |
"itemId": itemId | |
}); | |
}); | |
Item.create({ | |
"name": "Printer", | |
"productnumber": "PPP-999", | |
"description": "Een printer die print", | |
"category": "machine", | |
"x": 0, | |
"y": 3, | |
"width": 1, | |
"height": 1, | |
"hallName": hallName | |
}).then(function (createdItem) { | |
const itemId = createdItem.dataValues.id; | |
Action.create({ | |
"date": "2015-12-16T18:26:43.511Z", | |
"type": "VERVANGING", | |
"description": "De printer moet vervangen worden", | |
"itemId": itemId | |
}); | |
}); | |
}); | |
Hall.create({ | |
"name": "R&D-hal", | |
"area": 100, | |
"x": 0, | |
"y": 10, | |
"width": 10, | |
"height": 10 | |
}).then(function (createdHall) { | |
const hallName = createdHall.dataValues.name; | |
Item.create({ | |
"name": "Microscoop", | |
"productnumber": "LLL-123", | |
"description": "Een microscoop heeft eigenlijk geen omschrijving nodig, hé?", | |
"category": "machine", | |
"x": 0, | |
"y": 0, | |
"width": 1, | |
"height": 1, | |
"hallName": hallName | |
}); | |
Item.create({ | |
"name": "Stoel", | |
"productnumber": "ST-OEL", | |
"description": "Een comfi stoel om op te zitten", | |
"category": "machine", | |
"x": 5, | |
"y": 5, | |
"width": 1, | |
"height": 1, | |
"hallName": hallName | |
}); | |
Item.create({ | |
"name": "Printer", | |
"productnumber": "PPP-999", | |
"description": "Een printer die print", | |
"category": "machine", | |
"x": 9, | |
"y": 9, | |
"width": 1, | |
"height": 1, | |
"hallName": hallName | |
}); | |
}); | |
Hall.create({ | |
"name": "Verpakkinghal", | |
"area": 27, | |
"x": 5, | |
"y": 0, | |
"width": 9, | |
"height": 10 | |
}).then(function (createdHall) { | |
const hallName = createdHall.dataValues.name; | |
Item.create({ | |
"name": "Lopendeband", | |
"productnumber": "LOL-123", | |
"description": "Een lopende band die dingen transporteert", | |
"category": "band", | |
"x": 0, | |
"y": 0, | |
"width": 8, | |
"height": 1, | |
"hallName": hallName | |
}); | |
Item.create({ | |
"name": "StapelDozen", | |
"productnumber": "STA-PEL", | |
"description": "Een stapel dozen", | |
"category": "lamp", | |
"x": 8, | |
"y": 0, | |
"width": 1, | |
"height": 1, | |
"hallName": hallName | |
}); | |
Item.create({ | |
"name": "Printer", | |
"productnumber": "PPP-999", | |
"description": "Een printer die print", | |
"category": "machine", | |
"x": 0, | |
"y": 2, | |
"width": 1, | |
"height": 1, | |
"hallName": hallName | |
}); | |
}); | |
Hall.create({ | |
"name": "WC", | |
"area": 16, | |
"x": 10, | |
"y": 10, | |
"width": 4, | |
"height": 4 | |
}).then(function (createdHall) { | |
const hallName = createdHall.dataValues.name; | |
Item.create({ | |
"name": "Pot", | |
"productnumber": "POT-POT", | |
"description": "Een doodgewone WC-pot.", | |
"category": "machine", | |
"x": 0, | |
"y": 0, | |
"width": 1, | |
"height": 1, | |
"hallName": hallName | |
}); | |
}); | |
Hall.create({ | |
"name": "Inkomhal", | |
"area": 24, | |
"x": 10, | |
"y": 14, | |
"width": 4, | |
"height": 6 | |
}).then(function (createdHall) { | |
const hallName = createdHall.dataValues.name; | |
Item.create({ | |
"name": "Deur", | |
"productnumber": "DE-UR-123", | |
"description": "Een doodgewone deur.", | |
"category": "band", | |
"x": 0, | |
"y": 0, | |
"width": 1, | |
"height": 1, | |
"hallName": hallName | |
}).then(function (createdItem) { | |
const itemId = createdItem.dataValues.id; | |
Action.create({ | |
"date": "2015-12-16T18:25:43.511Z", | |
"type": "VERVANGING", | |
"description": "De deur moet vervangen worden", | |
"itemId": itemId | |
}); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment