-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
121 lines (103 loc) · 4.25 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
const express = require('express')
const bodyParser = require('body-parser')
const dburl = process.env.JAWSDB_URL
const mysql = require('mysql')
const db = mysql.createConnection(dburl);
db.connect();
const app = express()
const port = process.env.PORT || 5000;
app.set('view engine', 'ejs')
app.use(bodyParser.json())
app.use(express.static('views'))
app.get('/', async function (req, res) {
res.status(200).render('main')
})
app.get('/account', async function (req, res) {
res.status(200).render('account')
})
app.get('/lost', async function (req, res) {
res.status(200).render('lost')
})
app.get('/found', async function (req, res) {
res.status(200).render('found')
})
app.post('/data/lost', async function (req, res) {
let { id, city, item, category } = req.body
if ((id == '') || (!id)) { id = '%' }
if ((city == '') || (!city)) { city = '%' }
if ((item == '') || (!item)) { item = '%' }
if ((category == '') || (!category)) { category = '%' }
console.log(`got request. city is ${city} item is ${item} category is ${category}`)
db.query(`SELECT * FROM founds WHERE category LIKE '${category}' AND item LIKE '%${item}%' AND city LIKE '${city}' AND status = '1'`, function (error, results, fields) {
if (error) console.log(error);
console.log(results)
if (!results[0]) {
console.log('zero results')
res.status(204)
} else {
res.status(200).json(results)
}
})
})
app.post('/data/found', async function (req, res) {
let { city, item, category } = req.body
let now = new Date(Date.now())
let status = 1
let description = 'no desc'
let created_at = new Date(Date.now())
console.log(`got founded item! city is ${city} name is ${item} category is ${category}`)
db.query(`INSERT INTO founds (item, city, category, found_date, status, description, created_at) VALUES ('${item}', '${city}', '${category}', '${now}', '${status}', '${description}', '${created_at}')`, function (error, results, fields) {
if (error) console.log(error);
console.log(results.affectedRows)
if (results.affectedRows !== 1) {
console.log('not approved')
res.status(401).json({ result: 'not added to DB' })
} else {
res.status(201).json(results)
}
})
})
app.listen(port, () => console.log(`app 🚀 started!! in port ${port}`))
//---------------- DB check ----------------//
// check if the tables existed:
// we have 3 tables - founds, losts and users.
// if table not found we create it now:
db.query(`SHOW TABLES LIKE 'founds'`, function (error, results) {
if (results.length == 0) {
console.log("DB check: founds table not found, create it now...");
db.query(
`CREATE TABLE founds (id INT AUTO_INCREMENT PRIMARY KEY, item VARCHAR(255) NOT NULL,found_date DATE, status TINYINT NOT NULL DEFAULT 1, description TEXT, category VARCHAR(255), city VARCHAR(255) NOT NULL, userid INT, email VARCHAR(255), phone VARCHAR(255), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)`,
function (error, results) {
console.log(results, error);
}
);
} else {
console.log("DB check: find founds table");
}
});
db.query(`SHOW TABLES LIKE 'losts'`, function (error, results) {
if (results.length == 0) {
console.log("DB check: losts table not found, create it now...");
db.query(
`CREATE TABLE losts (id INT AUTO_INCREMENT PRIMARY KEY, item VARCHAR(255) NOT NULL, found_date DATE, status TINYINT NOT NULL DEFAULT 1, description TEXT, category VARCHAR(255), city VARCHAR(255) NOT NULL, userid INT, email VARCHAR(255), phone VARCHAR(255), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)`,
function (error, results) {
console.log(results, error);
}
);
} else {
console.log("DB check: find losts table");
}
});
db.query(`SHOW TABLES LIKE 'users'`, function (error, results) {
if (results.length == 0) {
console.log("DB check: users table not found, create it now...");
db.query(
`CREATE TABLE users (id INT AUTO_INCREMENT PRIMARY KEY, nickname VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, hash VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)`,
function (error, results) {
console.log(results, error);
}
);
} else {
console.log("DB check: find users table");
}
});