-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
94 lines (70 loc) · 2.01 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
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/feeling-good');
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
});
let ipSchema = mongoose.Schema({
date: {
type: Date,
default: Date.now,
expires: 5000
},
ipAddres: String
});
let ip = mongoose.model('IpAddres', ipSchema);
let BadclicksSchema = mongoose.Schema({
clicks: Number
});
let badUserClicks = mongoose.model('badClicks', BadclicksSchema);
let GoodclicksSchema = mongoose.Schema({
clicks: Number
});
let goodUserClicks = mongoose.model('goodClicks', GoodclicksSchema);
//handling ip Addreses
let save = (addres) => {
ip.count({ipAddres: addres}, function (err, count){
if(count>0){
console.log('user exists');
} else {
const newRepository = new ip({ipAddres: addres});
newRepository.save().then(() => console.log('saved user'));
}
});
};
let getCollection = (add, callback) => {
ip.findOne({ipAddres: add}, function(err, res) {
if (err) {
console.log('we got error!');
} else {
callback(res);
}
});
}
//handling Clicks
let getBadClicks = (callback) => {
badUserClicks.count({},function(err, count) {
callback(count);
});
}
let getGoodClicks = (callback) => {
goodUserClicks.count({}, function(err, count) {
callback(count);
});
}
let badClicks = (click) => {
const newClicks = new badUserClicks({clicks: click});
newClicks.save().then(() => console.log('saved a BAD click'));
}
let goodClicks = (click) => {
const newClicks = new goodUserClicks({clicks: click});
newClicks.save().then(() => console.log('saved a GOOD click'));
}
module.exports.ip = ip;
module.exports.badUserClicks = badUserClicks;
module.exports.save = save;
module.exports.getCollection = getCollection;
module.exports.getBadClicks = getBadClicks;
module.exports.getGoodClicks = getGoodClicks;
module.exports.badClicks = badClicks;
module.exports.goodClicks = goodClicks;