-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
executable file
·166 lines (140 loc) · 3.58 KB
/
app.js
File metadata and controls
executable file
·166 lines (140 loc) · 3.58 KB
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
156
157
158
159
160
161
162
163
164
165
166
var express = require('express');
var app = express();
var csvjson = require('csvjson');
var traverse = require('traverse');
//var knayi = require("knayi-myscript");
// Retrieve
var MongoClient = require('mongodb').MongoClient;
var ObjectID = require('mongodb').ObjectID;
var dbhost="mongodb://10.240.0.2:27017/elecapi";
var pagesize=20;
var page=1;
var fixDate=function(item){
if(item===null) return item;
if(item.establishment_approval_date!==null) item.establishment_approval_date =new Date(item.establishment_approval_date).getTime();
if(item.registration_application_date!==null) item.registration_application_date =new Date(item.registration_application_date).getTime();
if(item.registration_approval_date!==null) item.registration_approval_date =new Date(item.registration_approval_date).getTime();
if(item.establishment_date!==null) item.establishment_date =new Date(item.establishment_date).getTime();
item.created_at=new Date(item.created_at).getTime();
item.updated_at=new Date(item.updated_at).getTime();
return item;
};
app.use(express.static('app'));
app.get('/',function(req,res){
pagesize=20;
page=1;
if(typeof req.query.per_page!=='undefined'){
pagesize=parseInt(req.query.per_page);
}
if(typeof req.query.page!=='undefined'){
page=parseInt(req.query.page);
}
MongoClient.connect(dbhost, function(err, db) {
var collection = db.collection('party');
collection.count(function(err, count) {
if(err)
res.json({
_meta:{
status:"error"
}
});
else{
collection.find({},{_id:0}).skip(pagesize*(page-1)).limit(pagesize).toArray(function(err, items) {
if(err)
res.json({
_meta:{
status:"error"
}
});
else{
for (var i = 0; i < items.length; i++) {
items[i]=fixDate(items[i]);
}
respond(req,res,items,count);
}
});
}
});
});
});
app.get('/:id',function(req,res){
pagesize=1;
page=1;
var id=parseInt(req.params.id);
MongoClient.connect(dbhost, function(err, db) {
var collection = db.collection('party');
collection.findOne({id:id},{fields:{_id:0}},function(err, item) {
if(err)
res.json({
_meta:{
status:"error"
}
});
else{
item=fixDate(item);
respond(req,res,item,null);
}
});
});
});
var server = app.listen(process.env.PORT || '8080', '0.0.0.0', function () {
var host = server.address().address;
var port = server.address().port;
console.log('Example app listening at http://%s:%s', host, port);
});
function respond(req,res,data,total){
var length=1;
var unicode=true;
var format="unicode";
var total_pages=Math.ceil(total/pagesize);
var links={
next:'?page='+(page+1),
previous:'?page='+(page-1)
};
if(total===null){
links=null;
total_pages=1;
}
else {
if(page===1){
links.previous=null;
}
if(page===total_pages){
links.next=null;
}
}
if(Array.isArray(data)) length=data.length;
var resp={
_meta:{
status:"ok",
count:total,
api_version: 1,
unicode:unicode,
format:format,
per_page:pagesize,
current_page:page,
total_pages:total_pages,
links:links
},
data:data
};
res.header("Access-Control-Allow-Origin", "*");
res.json(resp);
}
function zawuni(obj){
traverse(obj).forEach(function (x) {
if (typeof x !== "object") {
this.update(knayi(x).fontConvertSync('unicode5'));
}
});
return obj;
}
function unizaw(obj){
traverse(obj).forEach(function (x) {
if (typeof x !== "object") {
this.update(knayi(x).fontConvertSync('zawgyi'));
}
});
return obj;
}
module.exports = app;