wbs_rechner_express.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const express = require('express');
  2. const app = express();
  3. const expressSwagger = require('express-swagger-generator')(app);
  4. app.use(express.json()); //für das Parsing von JSON-Daten
  5. let options = {
  6. swaggerDefinition: {
  7. info: {
  8. description: 'API Dokumentation',
  9. title: 'WBS API',
  10. version: '1.0.0',
  11. },
  12. host: 'localhost:3000',
  13. basePath: '/',
  14. produces: [
  15. "application/json"
  16. ],
  17. schemes: ['http', 'https']
  18. },
  19. basedir: __dirname,
  20. files: ['./**/*.js']
  21. };
  22. expressSwagger(options)
  23. /**
  24. * @typedef Income
  25. * @property {string} type.required
  26. * @property {number} amount.required
  27. * @property {string} frequency.required - monthly / quarterly / yearly
  28. * @property {number} advertisingCosts.required
  29. */
  30. /**
  31. * @typedef Person
  32. * @property {string} name.required
  33. * @property {Array.<Income>} incomes.required
  34. */
  35. /**
  36. * @typedef Frequency
  37. * @property {string} id.required
  38. * @property {string} description.required
  39. */
  40. /**
  41. * @route POST /calculate
  42. * @group Einkommen - Operationen für Einkommen
  43. * @param {Income.model} incomes.body.required - eine neue Person
  44. * @returns {object} 200 - Eine Person hinzufügen
  45. * @returns {Error} default - Unerwarteter Fehler
  46. */
  47. app.post('/calculate', (req, res) => {
  48. let totalIncome = 0;
  49. const persons = req.body;
  50. for (const person of persons) {
  51. for (const income of person.incomes) {
  52. let annualIncome = income.amount * income.frequency;
  53. annualIncome -= income.advertisingCosts;
  54. totalIncome += annualIncome;
  55. }
  56. }
  57. //Beispiel Berechnung nicht entsprechend der WBS Berechnung
  58. let maxIncome = persons.length * 12000;
  59. let wbsGranted = maxIncome <= totalIncome;
  60. res.send({ wbsGranted: wbsGranted });
  61. });
  62. /**
  63. * @route GET /frequencies
  64. * @group Einkommen - Operationen für Einkommen
  65. * @returns {Array.<Frequency>} 200 - Eine Liste der Frequenzen abrufen
  66. * @returns {Error} default - Unerwarteter Fehler
  67. */
  68. app.get('/frequencies', (req, res) => {
  69. const frequencies = [
  70. { value: 365, description: 'täglich' },
  71. { value: 12, description: 'monatlich' },
  72. { value: 4, description: 'vierteljährlich' },
  73. { value: 1, description: 'jährlich' }
  74. //usw...
  75. ];
  76. res.send(frequencies);
  77. });
  78. /**
  79. * @route GET /types
  80. * @group Einkommen - Operationen für Einkommen
  81. * @returns {Array.<IncomeType>} 200 - Eine Liste der Einkommensarten
  82. * @returns {Error} default - Unerwarteter Fehler
  83. */
  84. app.get('/types', (req, res) => {
  85. const types = [
  86. { value: 'work', description: 'als Arbeitnehmer' },
  87. { value: 'pensioner', description: 'als Rentner' },
  88. { value: 'social', description: 'Einnahme aus Sozialeistungen' },
  89. { value: 'entertains', description: 'Unterhalt' }
  90. //usw...
  91. ];
  92. res.send(frequencies);
  93. });
  94. const port = process.env.PORT || 3000;
  95. app.listen(port, () => console.log(`Server läuft auf Port ${port}...`));