|
| 1 | +import express from 'express'; |
| 2 | +import swaggerAutogen from 'swagger-autogen'; |
| 3 | +import swaggerUi from 'swagger-ui-express'; |
| 4 | +import swaggerDocument from './swagger-output.json' assert { type: 'json' }; |
| 5 | + |
| 6 | +const swaggerGenerator = swaggerAutogen(); |
| 7 | +const app = express(); |
| 8 | +app.use(express.json()); |
| 9 | +app.use('/swagger-ui', swaggerUi.serve, swaggerUi.setup(swaggerDocument)); |
| 10 | +const port = 3000; |
| 11 | + |
| 12 | +let books = [ |
| 13 | + { isbn: "978-3-7466-3938-5", title: "Die sieben Schwestern", author: "Lucinda Riley", year: 2014 }, |
| 14 | + { isbn: "978-3-498-03467-7", title: "Origin", author: "Dan Brown", year: 2017 }, |
| 15 | + { isbn: "978-3-426-28153-5", title: "Die Känguru-Apokryphen", author: "Marc-Uwe Kling", year: 2018 }, |
| 16 | + { isbn: "978-3-608-96159-1", title: "Die Geschichte der Bienen", author: "Maja Lunde", year: 2017 }, |
| 17 | + { isbn: "978-3-257-07164-6", title: "Tyll", author: "Daniel Kehlmann", year: 2017 }, |
| 18 | + { isbn: "978-3-570-10348-3", title: "Die Tribute von Panem - Flammender Zorn", author: "Suzanne Collins", year: 2010 }, |
| 19 | + { isbn: "978-3-442-71320-6", title: "Das Paket", author: "Sebastian Fitzek", year: 2016 }, |
| 20 | + { isbn: "978-3-10-397271-1", title: "QualityLand", author: "Marc-Uwe Kling", year: 2017 }, |
| 21 | + { isbn: "978-3-442-49058-8", title: "Das Labyrinth der Träumenden Bücher", author: "Walter Moers", year: 2011 } |
| 22 | +]; |
| 23 | + |
| 24 | +let lends = []; |
| 25 | + |
| 26 | +app.get('/books', (request, response) => { |
| 27 | + response.send(books); |
| 28 | +}); |
| 29 | + |
| 30 | +app.get('/books/:isbn', (request, response) => { |
| 31 | + response.send(books.find((book) => book.isbn === request.params.isbn)); |
| 32 | +}); |
| 33 | + |
| 34 | +app.post('/books', (request, response) => { |
| 35 | + books = [...books, request.body]; |
| 36 | + response.send(request.body); |
| 37 | +}); |
| 38 | + |
| 39 | +app.put('/books/:isbn', (request, response) => { |
| 40 | + books = books.map( |
| 41 | + (book) => book.isbn === request.params.isbn ? request.body : book |
| 42 | + ); |
| 43 | + response.send(request.body); |
| 44 | +}) |
| 45 | + |
| 46 | +app.delete('/books/:isbn', (request, response) => { |
| 47 | + books = books.filter((book) => book.isbn !== request.body.isbn); |
| 48 | + response.sendStatus(204); |
| 49 | +}); |
| 50 | + |
| 51 | +app.get('/lends', (request, response) => { |
| 52 | + response.send(lends); |
| 53 | +}); |
| 54 | + |
| 55 | +app.post('/lends', (request, response) => { |
| 56 | + if(request.body.isbn && books.findIndex((book) => book.isbn === request.body.isbn) === -1) { |
| 57 | + return response.sendStatus(404); |
| 58 | + } |
| 59 | + |
| 60 | + const lend = {...request.body, id: crypto.randomUUID(), borrowed_at: new Date().getTime()} |
| 61 | + lends = [...lends, lend]; |
| 62 | + response.send(lend); |
| 63 | +}); |
| 64 | + |
| 65 | +app.delete('/lends/:id', (request, response) => { |
| 66 | + lends.map( |
| 67 | + (lend) => lends.id === request.params.id ? {...lend, returned_at: new Date().getTime()} : lend |
| 68 | + ) |
| 69 | +}); |
| 70 | + |
| 71 | +app.listen(port, () => { |
| 72 | + console.log(`Library app listening on port ${port}`); |
| 73 | +}); |
| 74 | + |
| 75 | +const doc = { |
| 76 | + info: { |
| 77 | + title: 'Library', |
| 78 | + description: 'Meine tolle Bibliothek API' |
| 79 | + }, |
| 80 | + host: 'localhost:3000' |
| 81 | +}; |
| 82 | + |
| 83 | +const outputFile = './aufgabe-6-2/swagger-output.json'; |
| 84 | +const routes = ['./aufgabe-6-2/library.js']; |
| 85 | + |
| 86 | +swaggerGenerator(outputFile, routes, doc); |
0 commit comments