-
Notifications
You must be signed in to change notification settings - Fork 4
/
api.js
118 lines (106 loc) · 3.53 KB
/
api.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
"use strict";
var BlogController = require('./controllers/blog');
var BloggerController = require('./controllers/blogger');
exports.serveRoutes = function(router) {
router.get('/v0.1/blogs', function(req, res) {
var includeAuthor = true;
var columns = {};
if (req.query.authors) {
if (req.query.authors === 'false') {
includeAuthor = false;
}
else if (req.query.authors === 'minimal') {
columns = {
userId: 1,
userProvider: 1,
firstName: 1,
lastName: 1,
avatarUrl: 1,
feedUrl: 1,
blogWebsiteUrl: 1,
websiteUrl: 1,
vanityUrl: 1
};
}
}
BloggerController.getValidatedBloggers(function(error, ids, providers) {
if (error) {
sendError(res, 500, error.$err);
}
else {
var options = {
userId: {$in: ids},
userProvider: {$in: providers}
};
BlogController.getPaginatedBlogs(options, includeAuthor, columns, req,
function(blogs, pageNumber, showBack, showNext, error) {
if (error) {
sendError(res, 500, error.$err);
}
else {
if (req.query.original !== 'true') {
BlogController.removeAllHTML(blogs, 400);
}
var page = {
blogs: blogs,
pageNumber: pageNumber,
hasLess: showBack,
hasMore: showNext
};
res.json(page);
}
});
}
});
});
router.get('/v0.1/bloggers', function(req, res) {
BloggerController.getAllProfiles(true, {}, function (profiles, error) {
if (error) {
sendError(res, 500, error.$err);
}
else {
res.json(profiles);
}
});
});
router.get('/v0.1/bloggers/:vanityurl', function(req, res) {
var vanityUrl = req.params.vanityurl;
BloggerController.getProfileByVanityUrl(vanityUrl, req, function (profile, page, error) {
if (error) {
sendError(res, 500, error.$err);
}
else if (profile && page) {
var blogger = {
profile: profile,
page: page
};
res.json(blogger);
}
else {
sendError(res, 404, 'Blogger not found');
}
});
});
// Handle error 404
router.use(function(req, res) {
console.error('ERROR 404. Request: %j', req);
res.status(404);
res.json({
error: '404 Not Found'
});
});
// Handle error 500
router.use(function(error, req, res, next) {
console.error("ERROR 500. Error: %j", error);
res.status(500);
res.json({
error: 'Internal Server Error: ' + error
});
});
function sendError(res, errorCode, errorMessage) {
res.status(errorCode);
res.json({
error: errorMessage
});
}
};