Browse Source

Swagger Added, Types Fix

Leonard Schulz (Arbeit) 2 năm trước cách đây
mục cha
commit
a910c81948
1 tập tin đã thay đổi với 96 bổ sung và 1 xóa
  1. 96 1
      backend_nodejs/wbs_rechner_express.js

+ 96 - 1
backend_nodejs/wbs_rechner_express.js

@@ -2,7 +2,61 @@ const express = require('express');
 const app = express();
 app.use(express.json()); //für das Parsing von JSON-Daten
 
+// Swagger Setup
+const swaggerJsdoc = require('swagger-jsdoc');
+const swaggerUi = require('swagger-ui-express');
 
+const options = {
+    definition: {
+        openapi: '3.0.0',
+        info: {
+            title: 'WBS RECHNER',
+            version: '1.0.0',
+            description: 'Eine API für die Berechnung des Gesamteinkommens und die Gewährung von WBS.',
+        },
+    },
+    apis: ['app.js'],
+};
+
+const specs = swaggerJsdoc(options);
+app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(specs));
+
+/**
+ * @swagger
+ * /calculate:
+ *   post:
+ *     summary: Calculates the total income and determines if WBS is granted.
+ *     requestBody:
+ *       required: true
+ *       content:
+ *         application/json:
+ *           schema:
+ *             type: array
+ *             items:
+ *               type: object
+ *               properties:
+ *                 incomes:
+ *                   type: array
+ *                   items:
+ *                     type: object
+ *                     properties:
+ *                       amount:
+ *                         type: number
+ *                       frequency:
+ *                         type: number
+ *                       advertisingCosts:
+ *                         type: number
+ *     responses:
+ *       200:
+ *         description: Success
+ *         content:
+ *           application/json:
+ *             schema:
+ *               type: object
+ *               properties:
+ *                 wbsGranted:
+ *                   type: boolean
+ */
 app.post('/calculate', (req, res) => {
     let totalIncome = 0;
     const persons = req.body;
@@ -21,6 +75,27 @@ app.post('/calculate', (req, res) => {
     res.send({ wbsGranted: wbsGranted });
 });
 
+/**
+ * @swagger
+ * /frequencies:
+ *   get:
+ *     summary: Returns the available income frequencies.
+ *     responses:
+ *       200:
+ *         description: Success
+ *         content:
+ *           application/json:
+ *             schema:
+ *               type: array
+ *               items:
+ *                 type: object
+ *                 properties:
+ *                   value:
+ *                     type: number
+ *                   description:
+ *                     type: string
+ */
+
 app.get('/frequencies', (req, res) => {
     const frequencies = [
         { value: 365, description: 'täglich' },
@@ -32,6 +107,26 @@ app.get('/frequencies', (req, res) => {
     res.send(frequencies);
 });
 
+/**
+ * @swagger
+ * /types:
+ *   get:
+ *     summary: Returns the available income types.
+ *     responses:
+ *       200:
+ *         description: Success
+ *         content:
+ *           application/json:
+ *             schema:
+ *               type: array
+ *               items:
+ *                 type: object
+ *                 properties:
+ *                   value:
+ *                     type: string
+ *                   description:
+ *                     type: string
+ */
 app.get('/types', (req, res) => {
     const types = [
         { value: 'work', description: 'als Arbeitnehmer' },
@@ -40,7 +135,7 @@ app.get('/types', (req, res) => {
         { value: 'entertains', description: 'Unterhalt' }
         //usw...
     ];
-    res.send(frequencies);
+    res.send(types);
 });
 
 const port = process.env.PORT || 3000;