-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
76 lines (70 loc) · 2.4 KB
/
index.ts
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
const express = require('express')
const app = express();
import {db} from './MySQLConnect.js'
app.listen(3000);
console.log("Backend Listening on port 3000")
app.get('/', (req, res) =>{
res.redirect('/api/customers')
})
app.get('/api/customers',async (req, res) =>{
try{
const [rows,] = await db.execute(`SELECT * FROM customers;`)
res.json(rows)
res.json(rows)
} catch (err) {
res.status(500).send("error fetching customers")
}
})
app.get('/api/customer/:id',async (req, res)=>{
try{
const id = req.params.id
const [rows,] = await db.execute(`SELECT * FROM customers WHERE id = ?;`,[id])
if (rows.length===0) res.status(404).send(`No Such Customer with id ${id}`)
res.json(rows)
console.table(rows)
} catch (err) {
res.status(500).send(`error fetching customers, error:${err.message}`)
}
})
app.post('/api/customer/add',async (req, res)=>{
const value:string[] = []
console.log(req.query)
for (const key in req.query) {
// console.log(req.params[key])
value.push(req.query[key])
}
console.table(value)
try {
await db.execute(`INSERT INTO customers (first_name,last_name,phone,email,address,city,state) VALUES (?,?,?,?,?,?,?)`,value)
res.send("Customer added")
} catch(err) {
res.status(500).send(`error inserting customers, error: ${err.message}`)
console.error(err)
}
})
app.post('/api/customer/update/:id',async (req, res)=>{
const value:string[] = []
console.log(req.query)
for (const key in req.query) {
// console.log(req.params[key])
value.push(req.query[key])
}
value.push(req.params.id)
console.table(value)
try {
await db.execute(`UPDATE customers SET first_name=?,last_name=?,phone=?,email=?,address=?,city=?,state=? WHERE id=?`,value)
res.send(`Customer with id:${req.params.id} updated`)
} catch(err) {
res.status(500).send(`error updating customers, error: ${err.message}`)
console.error(err)
}
})
app.delete('/api/customer/delete/:id',async (req, res)=>{
try{
const id = req.params.id
const [rows,] = await db.execute(`DELETE FROM customers WHERE id = ?;`,[id])
res.send(`Customer with id: ${req.params.id} deleted`)
} catch (err) {
res.status(500).send(`error delete customer, error:${err.message}`)
}
})