|
|
@@ -0,0 +1,107 @@
|
|
|
+const express = require('express');
|
|
|
+const app = express();
|
|
|
+const expressSwagger = require('express-swagger-generator')(app);
|
|
|
+app.use(express.json()); //für das Parsing von JSON-Daten
|
|
|
+
|
|
|
+let options = {
|
|
|
+ swaggerDefinition: {
|
|
|
+ info: {
|
|
|
+ description: 'API Dokumentation',
|
|
|
+ title: 'WBS API',
|
|
|
+ version: '1.0.0',
|
|
|
+ },
|
|
|
+ host: 'localhost:3000',
|
|
|
+ basePath: '/',
|
|
|
+ produces: [
|
|
|
+ "application/json"
|
|
|
+ ],
|
|
|
+ schemes: ['http', 'https']
|
|
|
+ },
|
|
|
+ basedir: __dirname,
|
|
|
+ files: ['./**/*.js']
|
|
|
+};
|
|
|
+expressSwagger(options)
|
|
|
+
|
|
|
+/**
|
|
|
+ * @typedef Income
|
|
|
+ * @property {string} type.required
|
|
|
+ * @property {number} amount.required
|
|
|
+ * @property {string} frequency.required - monthly / quarterly / yearly
|
|
|
+ * @property {number} advertisingCosts.required
|
|
|
+ */
|
|
|
+
|
|
|
+/**
|
|
|
+ * @typedef Person
|
|
|
+ * @property {string} name.required
|
|
|
+ * @property {Array.<Income>} incomes.required
|
|
|
+ */
|
|
|
+
|
|
|
+/**
|
|
|
+ * @typedef Frequency
|
|
|
+ * @property {string} id.required
|
|
|
+ * @property {string} description.required
|
|
|
+ */
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @route POST /calculate
|
|
|
+ * @group Einkommen - Operationen für Einkommen
|
|
|
+ * @param {Income.model} incomes.body.required - eine neue Person
|
|
|
+ * @returns {object} 200 - Eine Person hinzufügen
|
|
|
+ * @returns {Error} default - Unerwarteter Fehler
|
|
|
+ */
|
|
|
+app.post('/calculate', (req, res) => {
|
|
|
+ let totalIncome = 0;
|
|
|
+ const persons = req.body;
|
|
|
+ for (const person of persons) {
|
|
|
+ for (const income of person.incomes) {
|
|
|
+ let annualIncome = income.amount * income.frequency;
|
|
|
+ annualIncome -= income.advertisingCosts;
|
|
|
+ totalIncome += annualIncome;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //Beispiel Berechnung nicht entsprechend der WBS Berechnung
|
|
|
+ let maxIncome = persons.length * 12000;
|
|
|
+ let wbsGranted = maxIncome <= totalIncome;
|
|
|
+
|
|
|
+ res.send({ wbsGranted: wbsGranted });
|
|
|
+});
|
|
|
+
|
|
|
+/**
|
|
|
+ * @route GET /frequencies
|
|
|
+ * @group Einkommen - Operationen für Einkommen
|
|
|
+ * @returns {Array.<Frequency>} 200 - Eine Liste der Frequenzen abrufen
|
|
|
+ * @returns {Error} default - Unerwarteter Fehler
|
|
|
+ */
|
|
|
+app.get('/frequencies', (req, res) => {
|
|
|
+ const frequencies = [
|
|
|
+ { value: 365, description: 'täglich' },
|
|
|
+ { value: 12, description: 'monatlich' },
|
|
|
+ { value: 4, description: 'vierteljährlich' },
|
|
|
+ { value: 1, description: 'jährlich' }
|
|
|
+ //usw...
|
|
|
+ ];
|
|
|
+ res.send(frequencies);
|
|
|
+});
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * @route GET /types
|
|
|
+ * @group Einkommen - Operationen für Einkommen
|
|
|
+ * @returns {Array.<IncomeType>} 200 - Eine Liste der Einkommensarten
|
|
|
+ * @returns {Error} default - Unerwarteter Fehler
|
|
|
+ */
|
|
|
+app.get('/types', (req, res) => {
|
|
|
+ const types = [
|
|
|
+ { value: 'work', description: 'als Arbeitnehmer' },
|
|
|
+ { value: 'pensioner', description: 'als Rentner' },
|
|
|
+ { value: 'social', description: 'Einnahme aus Sozialeistungen' },
|
|
|
+ { value: 'entertains', description: 'Unterhalt' }
|
|
|
+ //usw...
|
|
|
+ ];
|
|
|
+ res.send(frequencies);
|
|
|
+});
|
|
|
+
|
|
|
+const port = process.env.PORT || 3000;
|
|
|
+app.listen(port, () => console.log(`Server läuft auf Port ${port}...`));
|