-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
155 lines (128 loc) · 3.34 KB
/
index.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
148
149
150
151
152
153
154
155
const express = require('express');
const app = express()
const bodyparser = require('body-parser')
const Sequelize = require('sequelize')
const port = 4500
app.use(bodyparser.json())
app.use(bodyparser.urlencoded({extended: false}))
const sequelize = new Sequelize('bitthaldb','Bitthal','Bitthal@123', {
dialect: "mysql",
});
// schema for new user
const reg_frm = sequelize.define('req_table', {
name: {
type: Sequelize.STRING,
allowNull: false,
},
contact: {
type: Sequelize.INTEGER,
unique: true,
},
email: Sequelize.STRING,
password: {
type: Sequelize.STRING,
allowNull: false,
},
},{tableName: "reg_frm"});
reg_frm.sync();
// schema for spam users
const spam_frm = sequelize.define('spam_table', {
contct: {
type: Sequelize.INTEGER,
allowNull: false,
unique: true,
}
},{tableName: "spam_frm"});
spam_frm.sync();
sequelize.authenticate().then(()=>{
console.log('connection successful')
}).catch((err) =>{
console.log(err,'this has error');
})
/*app.get("/" , (req,res) =>{
res.send("Working Good");
})*/
app.post('/' ,async(req,res) =>{
const name = req.body.name;
const contact = req.body.contact;
const email = req.body.email;
const password = req.body.password;
const saveData = reg_frm.build({
name,
contact,
email,
password,
});
await saveData.save();
res.send('data send successfully');
});
// to get all users with registered number and password
app.get('/' , async(req,res) =>{
const allData = await reg_frm.findAll();
res.json(allData)
})
// To get all users with the required phone no.
app.get("/contact/:contact" , async(req,res) =>{
const reqcon = req.params.contact;
const data = await reg_frm.findOne({
where:{
contact: reqcon
}
}).then(function(data){
if(!data){
res.send('not find');
}
res.json(data);
})
})
// to get the names , phoneno , isSpam or not
app.get("/name/:name" , async(req,res) =>{
const reqname = req.params.name;
const reqdata = await reg_frm.findOne({
where:{
name: reqname
}
}).then(function(reqdata){
if(!reqdata){
res.send('not find the existing name');
}
const reqcontct = reqdata.contact;
const temp_obj = "No";
const findcontct = spam_frm.findAll({
where:{
contct: reqcontct
}
}).then(function(findcontct){
if(!findcontct){
temp_obj ="No"
}
temp_obj="yes";
})
const obj ={
reqobj: reqdata,
spamcon: temp_obj,
}
res.status(200).json(obj);
})
})
// Spam table to save data in database(post) and to get data from spam table(get)
app.post('/spam', async(req,res) =>{
const contct = req.body.contct;
const saveSpam = spam_frm.build({
contct,
});
await saveSpam.save();
res.send('send to spam successfully');
})
app.get('/spam', async(req,res) =>{
const allData = await spam_frm.findAll();
res.json(allData)
})
/*app.put('/:id', (req,res) =>{
const data = req.body.data
reg_frm.update({
})
})*/
app.listen(port, () =>{
console.log(`server starts at http://localhost:${port}`);
})