-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from SchweizerischeBundesbahnen/feature/persist…
…ence feat: persistent pastries
- Loading branch information
Showing
18 changed files
with
872 additions
and
539 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,24 @@ | ||
const express = require('express'); | ||
const path = require('path'); | ||
const cookieParser = require('cookie-parser'); | ||
const logger = require('morgan'); | ||
|
||
const appbakersRouter = require('./routes/appbakers'); | ||
const pasteriesRouter = require('./routes/pastries'); | ||
const {morganMiddleware, named} = require('./logger'); | ||
const logger = named('app'); | ||
|
||
const app = express(); | ||
const port = 8080 | ||
|
||
app.use(logger('dev')); | ||
app.use(morganMiddleware); | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(express.urlencoded({extended: false})); | ||
app.use(cookieParser()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
|
||
app.use('/appbakers', appbakersRouter); | ||
app.use('/pastries', pasteriesRouter); | ||
|
||
app.listen(port, () => { | ||
console.log(`BakeTime listening on port ${port}`) | ||
logger.info(`BakeTime listening on port ${port}`) | ||
}) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,43 @@ | ||
const Data = require('../data/data') | ||
const Pastry = require('../models/pastery'); | ||
const Pastry = require('../models/pastry'); | ||
const store = require('../persistence/pastries-persistence'); | ||
|
||
exports.getPastries = (req, res) => { | ||
res.json(Data.pasteries); | ||
exports.getPastries = async (req, res) => { | ||
res.json(store.data); | ||
}; | ||
|
||
exports.getPastry = (req, res) => { | ||
const pastry = Data.pasteries.find(p => p.id === parseInt(req.params.id)); | ||
exports.getPastry = async (req, res) => { | ||
const pastry = store.data.find(p => p.id === parseInt(req.params.id)); | ||
if (!pastry) return res.status(404).send('The pastry with the given ID was not found.'); | ||
res.send(pastry); | ||
}; | ||
|
||
exports.addPastry = (req, res) => { | ||
const pastry = new Pastry(Data.pasteries.length + 1, req.body.name); | ||
Data.pasteries.push(pastry); | ||
exports.addPastry = async (req, res) => { | ||
const pastry = new Pastry(store.data.length + 1, req.body.name); | ||
await store.add(pastry); | ||
|
||
res.status(204).send(); | ||
}; | ||
|
||
exports.updatePastry = (req, res) => { | ||
const pastry = Data.pasteries.find(p => p.id === parseInt(req.params.id)); | ||
exports.updatePastry = async (req, res) => { | ||
const pastry = store.data.find(p => p.id === parseInt(req.params.id)); | ||
if (!pastry) return res.status(404).send('The pastry with the given ID was not found.'); | ||
pastry.name = req.body.name; | ||
pastry.bakingType = req.body.bakingType; | ||
pastry.startTime = req.body.startTime; | ||
pastry.duration = req.body.duration; | ||
pastry.size = req.body.size; | ||
await store.set(store.data); | ||
|
||
res.status(204).send(); | ||
}; | ||
|
||
exports.deletePastry = (req, res) => { | ||
const pastry = Data.pasteries.find(p => p.id === parseInt(req.params.id)); | ||
exports.deletePastry = async (req, res) => { | ||
const pastry = store.data.find(p => p.id === parseInt(req.params.id)); | ||
if (!pastry) return res.status(404).send('The pastry with the given ID was not found.'); | ||
|
||
const index = Data.pasteries.indexOf(pastry); | ||
Data.pasteries.splice(index, 1); | ||
const index = store.data.indexOf(pastry); | ||
store.data.splice(index, 1); | ||
await store.set(store.data); | ||
|
||
res.status(204).send(); | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,40 @@ | ||
const AppBaker = require('../models/appbaker'); | ||
const Pastery = require('../models/pastery'); | ||
|
||
// TODO: Replace static with DB or other data source | ||
|
||
exports.pasteries = [ | ||
new Pastery(1, 'BakeTime') | ||
] | ||
const pastries = require('../persistence/pastries-persistence'); | ||
|
||
exports.appBakers = [ | ||
new AppBaker(1,'François Vessaz', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(2,'Joe Scheidegger', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(3,'Michael Moor', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(4,'Stefan Aebischer', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(5,'Lukas Schlüchter', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(6,'Georgios Antoniadis', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(7,'Marco Ghilardelli', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(8,'Hoang Tran', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(9,'Henrik Karppinen', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(10,'Loris Sorace', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(11,'Jeanne Fleury', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(12,'Thomas Bomatter', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(13,'Dominik Mosberger', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(14,'Alona Korset', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(15,'Ulrich Raab', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(16,'Kirill Ivanov', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(17,'Pauline Windey', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(18,'Simon Meer', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(19,'Leandro Bastos', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(20,'Adriana De São José Martins', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(21,'Nicolas Vidoni', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(22,'Stefan Zeller', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(23,'Yoonjoo Lee', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(24,'Ralf Winkelmann', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(25,'Valerio Oropeza Jose Enrique', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(26,'Vera Fischer', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(27,'Dmytriy Pelts', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(28,'Linus Ackermann', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(29,'David Lienberger', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(30,'Oliver Werlen', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(31,'Damir Ebibi', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(32,'Noé Schär', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(33,'Tristan Fanelli', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(34,'Ladina Kundert', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(35,'Nathanaël Bourgeois', 0.8, ['Node.js'], [exports.pasteries[0]]), | ||
new AppBaker(1,'François Vessaz', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(2,'Joe Scheidegger', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(3,'Michael Moor', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(4,'Stefan Aebischer', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(5,'Lukas Schlüchter', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(6,'Georgios Antoniadis', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(7,'Marco Ghilardelli', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(8,'Hoang Tran', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(9,'Henrik Karppinen', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(10,'Loris Sorace', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(11,'Jeanne Fleury', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(12,'Thomas Bomatter', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(13,'Dominik Mosberger', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(14,'Alona Korset', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(15,'Ulrich Raab', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(16,'Kirill Ivanov', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(17,'Pauline Windey', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(18,'Simon Meer', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(19,'Leandro Bastos', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(20,'Adriana De São José Martins', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(21,'Nicolas Vidoni', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(22,'Stefan Zeller', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(23,'Yoonjoo Lee', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(24,'Ralf Winkelmann', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(25,'Valerio Oropeza Jose Enrique', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(26,'Vera Fischer', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(27,'Dmytriy Pelts', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(28,'Linus Ackermann', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(29,'David Lienberger', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(30,'Oliver Werlen', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(31,'Damir Ebibi', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(32,'Noé Schär', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(33,'Tristan Fanelli', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(34,'Ladina Kundert', 0.8, ['Node.js'], [pastries.data[0]]), | ||
new AppBaker(35,'Nathanaël Bourgeois', 0.8, ['Node.js'], [pastries.data[0]]), | ||
]; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const {createLogger, transports, format} = require('winston'); | ||
const morgan = require('morgan'); | ||
|
||
const httpLogger = createLogger({ | ||
level: 'http', | ||
format: format.combine( | ||
format.timestamp(), | ||
format.json(), | ||
format.colorize({all: true}), | ||
), | ||
transports: [new transports.Console()], | ||
}); | ||
|
||
const defaultLogger = createLogger({ | ||
level: process.env.LOG_LEVEL || 'info', | ||
format: format.combine( | ||
format.timestamp(), | ||
format.json(), | ||
format.colorize({all: true}), | ||
), | ||
transports: [new transports.Console()], | ||
}); | ||
|
||
const named = function (name) { | ||
return defaultLogger.child({name}); | ||
} | ||
|
||
const morganMiddleware = morgan( | ||
':method :url :status :res[content-length] - :response-time ms', | ||
{ | ||
stream: { | ||
// Configure Morgan to use our custom logger with the http severity | ||
write: (message) => httpLogger.http(message.trim()), | ||
}, | ||
} | ||
); | ||
|
||
module.exports = {morganMiddleware, defaultLogger, named}; |
File renamed without changes.
Oops, something went wrong.