Skip to content

Commit

Permalink
Merge pull request #8 from SchweizerischeBundesbahnen/feature/persist…
Browse files Browse the repository at this point in the history
…ence

feat: persistent pastries
  • Loading branch information
kirill1722 authored Jul 24, 2024
2 parents 3093e76 + b412832 commit 6d13f99
Show file tree
Hide file tree
Showing 18 changed files with 872 additions and 539 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,4 +127,10 @@ dist
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*
.pnp.*

# idea config
.idea/

# persistence
api/.node-persist
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ cd api
npm install
node app.js
```
To enable log levels below info use this command:
````
LOG_LEVEL=debug node app.js
````

### Frontend
To get started locally, add a .env file at the root of the `frontend` dir with the `VITE_BACKEND_URL` var pointing to the webserver hosted locally above.
Expand Down
9 changes: 5 additions & 4 deletions api/app.js
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}`)
})
32 changes: 17 additions & 15 deletions api/controllers/pastries-controller.js
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();
};
78 changes: 36 additions & 42 deletions api/data/data.js
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]]),
];
38 changes: 38 additions & 0 deletions api/logger.js
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.
Loading

0 comments on commit 6d13f99

Please sign in to comment.