Skip to content

Commit 988acb2

Browse files
committed
chore: add tasks day 2
1 parent d6bc258 commit 988acb2

10 files changed

Lines changed: 1309 additions & 0 deletions

File tree

aufgabe-3-1/hello.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import express from 'express';
2+
3+
// var, const, let
4+
5+
const app = express();
6+
const port = 3000;
7+
8+
app.get('/', (request, response) => {
9+
response.send('Hello World!');
10+
});
11+
12+
app.listen(port, () => {
13+
console.log(`Example app listening on port ${port}`);
14+
});

aufgabe-3-3/endpoints.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import express from 'express';
2+
3+
const app = express();
4+
const port = 3000;
5+
6+
app.get('/now', (request, response) => {
7+
response.send(new Date().toLocaleString("de-CH"));
8+
});
9+
10+
const names = [
11+
"Hans",
12+
"Franz",
13+
"Jack",
14+
"Daniel",
15+
"Susanne",
16+
"Ceren",
17+
"Sara",
18+
"Josef",
19+
"Kunigunde"
20+
]
21+
22+
app.get('/name', (request, response) => {
23+
const index = Math.floor(Math.random() * names.length);
24+
response.send(names[index]);
25+
})
26+
27+
app.listen(port, () => {
28+
console.log(`Endpoints app listening on port ${port}`);
29+
});

aufgabe-4-2/endpoints.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import express from 'express';
2+
3+
const app = express();
4+
app.use(express.json());
5+
app.use(express.urlencoded());
6+
const port = 3000;
7+
8+
let names = [
9+
"Hans",
10+
"Franz",
11+
"Jack",
12+
"Daniel",
13+
"Susanne",
14+
"Ceren",
15+
"Sara",
16+
"Josef",
17+
"Kunigunde"
18+
]
19+
20+
app.get('/now', (request, response) => {
21+
response.send(new Date().toLocaleString("de-CH", {timeZone: request.query.tz}));
22+
});
23+
24+
app.get('/names', (_, response) => {
25+
response.send(names);
26+
});
27+
28+
app.delete('/names', (request, response) => {
29+
const nameToDelete = request.query.name;
30+
names = names.filter((name) => name !== nameToDelete);
31+
response.sendStatus(204);
32+
});
33+
34+
app.post('/test', (req, res) => {
35+
console.log(req.body);
36+
res.sendStatus(204);
37+
})
38+
39+
app.listen(port, () => {
40+
console.log(`Endpoints app listening on port ${port}`);
41+
});

aufgabe-5-1/library.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import express from 'express';
2+
3+
const app = express();
4+
app.use(express.json());
5+
const port = 3000;
6+
7+
let books = [
8+
{ isbn: "978-3-7466-3938-5", title: "Die sieben Schwestern", author: "Lucinda Riley", year: 2014 },
9+
{ isbn: "978-3-498-03467-7", title: "Origin", author: "Dan Brown", year: 2017 },
10+
{ isbn: "978-3-426-28153-5", title: "Die Känguru-Apokryphen", author: "Marc-Uwe Kling", year: 2018 },
11+
{ isbn: "978-3-608-96159-1", title: "Die Geschichte der Bienen", author: "Maja Lunde", year: 2017 },
12+
{ isbn: "978-3-257-07164-6", title: "Tyll", author: "Daniel Kehlmann", year: 2017 },
13+
{ isbn: "978-3-570-10348-3", title: "Die Tribute von Panem - Flammender Zorn", author: "Suzanne Collins", year: 2010 },
14+
{ isbn: "978-3-442-71320-6", title: "Das Paket", author: "Sebastian Fitzek", year: 2016 },
15+
{ isbn: "978-3-10-397271-1", title: "QualityLand", author: "Marc-Uwe Kling", year: 2017 },
16+
{ isbn: "978-3-442-49058-8", title: "Das Labyrinth der Träumenden Bücher", author: "Walter Moers", year: 2011 }
17+
];
18+
19+
let lends = [];
20+
21+
app.get('/books', (request, response) => {
22+
response.send(books);
23+
});
24+
25+
app.get('/books/:isbn', (request, response) => {
26+
response.send(books.find((book) => book.isbn === request.params.isbn));
27+
});
28+
29+
app.post('/books', (request, response) => {
30+
books = [...books, request.body];
31+
response.send(request.body);
32+
});
33+
34+
app.put('/books/:isbn', (request, response) => {
35+
books = books.map(
36+
(book) => book.isbn === request.params.isbn ? request.body : book
37+
);
38+
response.send(request.body);
39+
})
40+
41+
app.delete('/books/:isbn', (request, response) => {
42+
books = books.filter((book) => book.isbn !== request.body.isbn);
43+
response.sendStatus(204);
44+
})
45+
46+
app.listen(port, () => {
47+
console.log(`Library app listening on port ${port}`);
48+
});

aufgabe-5-2/library.js

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import express from 'express';
2+
3+
const app = express();
4+
app.use(express.json());
5+
const port = 3000;
6+
7+
let books = [
8+
{ isbn: "978-3-7466-3938-5", title: "Die sieben Schwestern", author: "Lucinda Riley", year: 2014 },
9+
{ isbn: "978-3-498-03467-7", title: "Origin", author: "Dan Brown", year: 2017 },
10+
{ isbn: "978-3-426-28153-5", title: "Die Känguru-Apokryphen", author: "Marc-Uwe Kling", year: 2018 },
11+
{ isbn: "978-3-608-96159-1", title: "Die Geschichte der Bienen", author: "Maja Lunde", year: 2017 },
12+
{ isbn: "978-3-257-07164-6", title: "Tyll", author: "Daniel Kehlmann", year: 2017 },
13+
{ isbn: "978-3-570-10348-3", title: "Die Tribute von Panem - Flammender Zorn", author: "Suzanne Collins", year: 2010 },
14+
{ isbn: "978-3-442-71320-6", title: "Das Paket", author: "Sebastian Fitzek", year: 2016 },
15+
{ isbn: "978-3-10-397271-1", title: "QualityLand", author: "Marc-Uwe Kling", year: 2017 },
16+
{ isbn: "978-3-442-49058-8", title: "Das Labyrinth der Träumenden Bücher", author: "Walter Moers", year: 2011 }
17+
];
18+
19+
let lends = [];
20+
21+
app.get('/books', (request, response) => {
22+
response.send(books);
23+
});
24+
25+
app.get('/books/:isbn', (request, response) => {
26+
response.send(books.find((book) => book.isbn === request.params.isbn));
27+
});
28+
29+
app.post('/books', (request, response) => {
30+
books = [...books, request.body];
31+
response.send(request.body);
32+
});
33+
34+
app.put('/books/:isbn', (request, response) => {
35+
books = books.map(
36+
(book) => book.isbn === request.params.isbn ? request.body : book
37+
);
38+
response.send(request.body);
39+
})
40+
41+
app.delete('/books/:isbn', (request, response) => {
42+
books = books.filter((book) => book.isbn !== request.body.isbn);
43+
response.sendStatus(204);
44+
});
45+
46+
app.get('/lends', (request, response) => {
47+
response.send(lends);
48+
});
49+
50+
app.post('/lends', (request, response) => {
51+
if(request.body.isbn && books.findIndex((book) => book.isbn === request.body.isbn) === -1) {
52+
return response.sendStatus(404);
53+
}
54+
55+
const lend = {...request.body, id: crypto.randomUUID(), borrowed_at: new Date().getTime()}
56+
lends = [...lends, lend];
57+
response.send(lend);
58+
});
59+
60+
app.delete('/lends/:id', (request, response) => {
61+
lends.map(
62+
(lend) => lends.id === request.params.id ? {...lend, returned_at: new Date().getTime()} : lend
63+
)
64+
});
65+
66+
app.listen(port, () => {
67+
console.log(`Library app listening on port ${port}`);
68+
});

aufgabe-6-2/library.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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

Comments
 (0)