This repository has been archived by the owner on Aug 2, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
148 lines (131 loc) · 4.49 KB
/
server.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
const express = require('express');
const sqlite3 = require('sqlite3').verbose();
const app = express();
require('dotenv').config();
//import connect-db.js
app.use(express.static(__dirname + '/public'));
app.use(express.json())
app.get('/', (req, res) => {
res.sendFile(__dirname + '/index.html');
});
app.get('/cards', (req, res) => {
// open db connection
const db = new sqlite3.Database('./db/cards-jwasham.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
return console.log(err.message);
}
console.log('Database opened successfully');
});
// select all cards where category is null and limit to 1
db.all('SELECT * FROM cards WHERE category IS NULL LIMIT 1', (err, rows) => {
if (err) {
return console.log(err.message);
}
console.log('All rows fetched successfully');
// close db connection
db.close();
// return card
res.json({id: rows[0].id, front: rows[0].front, back: rows[0].back, category: rows[0].category});
});
});
app.delete('/cards/:id', (req, res) => {
// open db connection
const db = new sqlite3.Database('./db/cards-jwasham.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
return console.log(err.message);
}
console.log('Database opened successfully');
});
// delete card with id
db.run('DELETE FROM cards WHERE id = ?', [req.params.id], (err) => {
if (err) {
return console.log(err.message);
}
console.log('Card deleted successfully');
// close db connection
db.close();
// return card deleted
res.json({message: 'Card deleted successfully'});
});
});
// be able to change front and back of card with a certain id
app.put('/cards-text/:id', (req, res) => {
// open db connection
const db = new sqlite3.Database('./db/cards-jwasham.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
return console.log(err.message);
}
console.log('Database opened successfully');
});
// change card with id
db.run('UPDATE cards SET front = ?, back = ? WHERE id = ?', [req.body.front, req.body.back, req.params.id], (err) => {
if (err) {
return console.log(err.message);
}
console.log('Card text updated successfully');
// close db connection
db.close();
// return card updated
res.json({message: 'Card updated successfully'});
});
});
app.post('/update-card-category', (req, res) => {
const db = new sqlite3.Database('./db/cards-jwasham.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
return console.log(err.message);
}
console.log('Database opened successfully');
});
// update card category
db.run('UPDATE cards SET category = ? WHERE id = ?', [req.body.category, req.body.id], (err) => {
if (err) {
return console.log(err.message);
}
console.log('Card category updated successfully');
// close db connection
db.close();
// return card
res.json({status: 'success'});
});
});
// insert a new category into the db
app.post('/insert-category', (req, res) => {
const db = new sqlite3.Database('./db/cards-jwasham.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
return console.log(err.message);
}
console.log('Database opened successfully');
});
// insert category
db.run('INSERT INTO categories (category_name) VALUES (?)', [req.body.category_name], (err) => {
if (err) {
return console.log(err.message);
}
console.log('Category inserted successfully');
// close db connection
db.close();
// return card
res.json({status: 'success'});
});
});
app.get('/category', (req, res) => {
// open db connection
const db = new sqlite3.Database('./db/cards-jwasham.db', sqlite3.OPEN_READWRITE, (err) => {
if (err) {
return console.log(err.message);
}
});
// select all categories from the categories table
db.all('SELECT * FROM categories', (err, rows) => {
if (err) {
return console.log(err.message);
}
console.log('All rows fetched successfully');
res.json(rows);
// close db connection
db.close();
});
});
app.listen(process.env.PORT, process.env.URL, () => {
console.log(`Server running on port ${process.env.PORT}`);
});