-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
131 lines (113 loc) · 3.55 KB
/
app.js
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
const express = require('express');
const sqlite3 = require('sqlite3');
const {PrismaClient} = require('@prisma/client');
const bodyParser = require('body-parser');
const app = express();
const prisma = new PrismaClient();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded());
// get all books
app.get('/', async (req, res)=> {
const books = await prisma.BookStore.findMany();
res.json(books);
})
// get a specific book
app.get('/:id', async (req, res)=>{
// validation
const id = req.params.id;
if(id.length < 1 || id.length > 10 || Number(id) == NaN || Number(id) < 1){
res.status(501).send("Error! Invalid ID")
}
else{
const dbBookId = await prisma.BookStore.findUnique({
where: {id: Number(id)},
select: {id: true}
});
if(dbBookId == null){
res.status(502).send("Failed!\nid not found")
}
else{
const mybook = await prisma.BookStore.findUnique({
where: {id: Number(id)},
select: {Book: true, Status: true}
});
res.status(200).json(mybook);
}
}
});
app.post('/newbook/:book/:status', async (req, res) => {
const {book, status} = req.params;
if(book.length > 50 || book.length < 2){
res.status(501).send('Error!\nYour book looks suspicious, check again then retry');
}
else if(status.length > 15 || status.length < 2){
res.status(501).send('Error!\nThe reading status is unsurpported');
}
else{
const newbook = await prisma.BookStore.create({
data: {
Book: book,
Status: status
},
});
res.status(200).send('Book Successfully Added to Shelf');
}
})
app.put('/update/:id/:status', async (req, res) => {
let {status, id} = req.params;
// validation
if(status.length > 15 || status.length < 2){
res.status(501).send('Error!\nThe reading status is unsurpported');
}
else if(id.length < 1 || id.length > 10 || Number(id) == NaN || Number(id) < 1){
res.status(501).send("Error! Invalid ID")
}
else{
const dbBookId = await prisma.BookStore.findUnique({
where: {id: Number(id)},
select: {id: true}
});
if(dbBookId == null){
res.status(501).send("Failed!\nid not found")
}
else{
const updateStatus = await prisma.BookStore.update({
where: {
id: Number(id),
},
data: {
Status: status
}
});
res.status(200).send('Success\nreading status has been updated')
};
};
});
app.delete('/delete/:id', async (req, res) =>{
// validation
const id = req.params.id;
if(id.length < 1 || id.length > 10 || Number(id) == NaN || Number(id) < 1){
res.status(501).send("Error! Invalid ID")
}
else{
const dbBookId = await prisma.BookStore.findUnique({
where: {id: Number(id)},
select: {id: true}
})
if(dbBookId == null){
res.status(501).send("Failed!\nid not found")
}
else{
const deletebook = await prisma.BookStore.delete({
where: {
id: Number(id)
},
});
res.status(200).send('The Book has been deleted');
}
}
})
// const PORT = 5000;
// app.listen(PORT, ()=>{
// console.log(`server active on http://localhost:${PORT}`);
// });