-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroutes.js
127 lines (115 loc) · 3.17 KB
/
routes.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
//require schemas from Models folder
var model = require('./models.js')
//export routes to app file
module.exports = function(app, express) {
/* linkClick route */
//GET request for all data
app.get('/linkClickAll', function(req, res) {
//find all urls in database
model.linkClickModel.find({}, function(err, links) {
if(err) {
throw err;
} else {
res.status(200).send(links);
}
});
});
//GET request for a specified url
app.get('/linkClick', function(req, res) {
//pull url from query
var url = req.query.url;
//find url in database
model.linkClickModel.findOne({url: url}, function(err, link) {
if(err) {
throw err;
} else {
res.status(200).send(link);
}
});
});
//POST request
app.post('/linkClick', function(req, res) {
//pull url from request body
var url = req.body.url;
//create new timestamp
var date = Date();
//check if url exists in database
model.linkClickModel.findOne({url: url}, function(err, link) {
//if it exists, update count and add timestamp
if(link) {
link.count++;
link.date.push(date);
link.save();
res.status(200).send("Successfully updated link count")
//if not, create new record, set count to 1 and add timestamp (in array)
} else {
model.linkClickModel.create({
url: url,
count: 1,
date: [date]
}, function(err) {
if(err) {
throw err;
} else {
res.status(200).send("Successfully created new link record");
}
});
}
});
});
/* pageView route */
//GET request for a specified page
app.get('/pageViewAll', function(req, res) {
//find all pages in database
model.pageViewModel.find({}, function(err, pages) {
if(err) {
throw err;
} else {
res.status(200).send(pages);
}
});
});
//GET request for a specified page
app.get('/pageView', function(req, res) {
//pull title from query
var title = req.query.title;
//find title in database
model.pageViewModel.findOne({title: title}, function(err, page) {
if(err) {
throw err;
} else {
res.status(200).send(page);
}
});
});
//POST request
app.post('/pageView', function(req, res) {
//pull title from request body
var title = req.body.title;
//create new timestamp
var date = Date();
//check if title exists in database
model.pageViewModel.findOne({title: title}, function(err, page) {
//if it exists, update count and add timestamp
if(page) {
page.count++;
page.date.push(date);
page.save();
res.status(200).send("Successfully updated page count")
//if not, create new record, set count to 1 and add timestamp (in array)
} else {
model.pageViewModel.create({
title: title,
count: 1,
date: [date]
}, function(err) {
if(err) {
throw err;
} else {
res.status(200).send("Successfully created new page record");
}
});
}
});
});
};