-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
106 lines (87 loc) · 2.21 KB
/
server.js
File metadata and controls
106 lines (87 loc) · 2.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const express = require("express");
const cors = require('cors')
const server = express();
server.listen(3001, () => {
console.log("Server running on port 3001");
});
server.use(cors())
server.use(express.urlencoded({ extended: true }));
server.use(express.json());
let books = [
{
id: "1",
title: "Harry Potter",
author: "J. K. Rowling",
isAvailable: true,
burrowedMemberId: "",
burrowedDate: "",
},
{
id: "2",
title: "Charlie and the Chocolate Factory",
author: "Roald Dahl",
isAvailable: true,
burrowedMemberId: "",
burrowedDate: "",
},
];
server.get("/book", (req, res) => {
res.send(books);
});
server.get("/book/:id", (req, res) => {
const id = req.params.id;
const book = books.find((book) => book.id === id);
res.send(book);
});
server.post("/book", (req, res) => {
const { title, author } = req.body;
const book = {
id: Math.random().toString(16).slice(2),
title,
author,
isAvailable: true,
burrowedMemberId: "",
burrowedDate: "",
};
books.push(book);
res.send(book);
});
server.put("/book/:id/burrow", (req, res) => {
const id = req.params.id;
const { burrowedMemberId, burrowedDate } = req.body;
const bookIndex = books.findIndex((book) => book.id === id);
books[bookIndex] = {
...books[bookIndex],
isAvailable: false,
burrowedMemberId,
burrowedDate,
};
res.send(books[bookIndex]);
});
server.put("/book/:id/return", (req, res) => {
const id = req.params.id;
const bookIndex = books.findIndex((book) => book.id === id);
books[bookIndex] = {
...books[bookIndex],
isAvailable: true,
burrowedMemberId: "",
burrowedDate: "",
};
res.send(books[bookIndex]);
});
server.put("/book/:id", (req, res) => {
const id = req.params.id;
const { title, author } = req.body;
const bookIndex = books.findIndex((book) => book.id === id);
books[bookIndex] = {
...books[bookIndex],
title,
author,
};
res.send(books[bookIndex]);
});
server.delete("/book/:id", (req, res) => {
const id = req.params.id;
books = books.filter((book) => book.id !== id);
res.send(id);
});