summaryrefslogtreecommitdiffstats
path: root/usecaseui-holmes/src/main/webapp/public/framework/appserver/api.js
blob: e4a237b73aa279fe85f59dbdf61334c9e245c8b3 (plain)
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*

    Copyright 2016-2017, Huawei Technologies Co., Ltd.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

            http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.

*/

var express = require('express');
var router = express.Router();
/*var js2xmlparser = require("js2xmlparser");
var xml2js = require('xml2js');*/
var fs = require('fs');
var http = require('http');
var bodyParser = require('body-parser');
var path=require('path');

var MongoClient = require('mongodb').MongoClient;
var ObjectId = require('mongodb').ObjectId;

var url = 'mongodb://localhost/rest_test';

/*var exists = require('file-exists');*/

// ############################################  Sign In code #################################################
// Routes
router.post('/signin', function (req, res) {
	console.log("signin");
	var obj = {
		"name": req.body.name,
		"pswd": req.body.pswd
	};

	var ObjLst =[] ;
	var nameExist =0;
	console.log(obj);
	ObjLst = JSON.parse(fs.readFileSync('input.json', 'utf8'));
	for(var index = 0; index < ObjLst.length; index++) {
		if (ObjLst[index].name === req.body.name && ObjLst[index].pswd === req.body.pswd) {
			console.log("success");
			nameExist++;
			res.statusCode=200;
			res.statusMessage="Sucessfully loged in";
			break;
		}
	}
	if (nameExist != 1) {
		console.log("failed");
		res.statusCode=404;
		res.statusMessage="failed to login";
	}
	res.send();
});

// ############################################  Sign Up Code #################################################

router.post('/signup', function (req, res) {

	console.log("in api.js");

	var ObjLst =[] ;
	var nameNotExist =0;
	var obj = {
		"name": req.body.name,
		"pswd": req.body.pswd,
		"email": req.body.email
	};
	if(fs.existsSync("input.json")) {
		console.log("File Exist");
		ObjLst = JSON.parse(fs.readFileSync('input.json', 'utf8'));
	}

	for(var index = 0; index < ObjLst.length; index++) {
		if (ObjLst[index].name === req.body.name && ObjLst[index].email === req.body.email) {
			console.log("success");
			nameNotExist++;
			break;
		}
	}
	if (nameNotExist == 0) {
		ObjLst.push(obj);
		console.log(ObjLst);
		fs.writeFile('input.json', JSON.stringify(ObjLst),  function(err) {
			if (err) {
				return console.error(err);
			}
			console.log("Data written successfully!");
			console.log("Let's read newly written data");
			res.statusCode=200;
			res.statusMessage="Sucessfully signed up";
		});
	}
	res.send();
});

function getDataFrmProvince() {
    var provinceData =[] ;
    if(fs.existsSync("provinceData.json")) {
        console.log("File Exist");
        provinceData = JSON.parse(fs.readFileSync('provinceData.json', 'utf8'));
    }
    return provinceData;
}

function saveDataToProvince(provinceData) {
    fs.writeFile('provinceData.json', JSON.stringify(provinceData),  function(err) {
        var statueCode = 0;
        if (err) {
            console.error(err);
            return statueCode = 404;
        }
        console.log("Data written successfully!");
        console.log("Let's read newly written data");
        return statueCode = 200;
    });
}

function getIndexOfIdProvince(id) {
    var provinceData = getDataFrmProvince();
    var returnIndx = -1;
    for (var index = 0; index < provinceData.length; index++) {
        if(provinceData[index].id == id) {
            returnIndx = index;
            break;
        }
    }
    return returnIndx;
}

function deleteIdFromProvince(idList) {
    var provinceData = getDataFrmProvince();
    /*for (var index = 0; index < provinceData.length; index++) {
        if(provinceData[index].id == id) {
            console.log("Deleting id : " + index);
            provinceData.splice(index, 1);
            break;
        }
    }*/
    for(var i = 0; i < idList.length; i++) {
        for (var index = 0; index < provinceData.length; index++) {
            if(provinceData[index].id == idList[i]) {
                console.log("Deleting id : " + index);
                provinceData.splice(index, 1);
                break;
            }
        }
    }
    return provinceData;
}

router.get('/getAllProvinceData', function (req, res) {
    var provinceData = getDataFrmProvince();
    if(provinceData) {
        output = '{"provinceData" : ' + JSON.stringify(provinceData) + '}';

        res.setHeader("Content-Type", "application/json");
        console.log("output : " + JSON.stringify(output));
        res.end(output);
    }
    /*MongoClient.connect(url, function(err, db) {
        console.log("Connected... :-)");
        var cursor = db.collection('ProvinceData').find({},function(err, cursor) {
            cursor.toArray(function (err, items) {
                output = '{"provinceData" : ' + JSON.stringify(items) + '}';

                res.setHeader("Content-Type", "application/json");
                //console.log("output : " + JSON.stringify(output));
                res.end(output);
            });
        });
        db.close(function(){
            console.log("Connection Closed... :-)");
        });
    });*/
});


router.post('/addProvinceData', function(req, res) {
    var provinceData = getDataFrmProvince();
    var gen_id = Math.floor(Math.random() * (9999 - 1000 + 1)) + 1000;
    provinceData.push({
        "id": gen_id,
        "province_name": req.body.province_name,
        "ip": req.body.ip,
        "port": req.body.port
    });
    var statusCode = saveDataToProvince(provinceData);
    res.statusCode=statusCode;

    if(statusCode == 200) {
        res.statusMessage="Sucessfully signed up";
    }
    else {
        res.statusMessage="Error";
    }

    /*MongoClient.connect(url, function(err, db) {
        db.collection('ProvinceData').insertOne({
            province_name: req.body.province_name,
            ip: req.body.ip,
            port: req.body.port
        });
        db.close();
    });*/
    res.statusCode=200;
    res.statusMessage="Sucessfully signed up";
    res.send();
});

router.post('/deleteProvinceData', function (req, res) {
    //var provinceData = getDataFrmProvince();
    console.log("IdList: "+ req.body.idList);
    /*for(var i = req.body.idList.length - 1; i >= 0; i--) {
        var index = getIndexOfIdProvince(req.body.idList[i]);
        console.log("Deleting id : " + index);
        if(index != -1) {
            provinceData.splice(index, 1);
        }
        //deleteIdFromProvince(req.body.idList[i]);
    };*/

    var provinceData = deleteIdFromProvince(req.body.idList);

    saveDataToProvince(provinceData);

    /*MongoClient.connect(url, function(err, db) {

        console.log("Deleting Province Data... " + req.body.idList);
        for(var i = 0; i < req.body.idList.length; i++) {
            db.collection('ProvinceData').deleteOne({ "_id": ObjectId(req.body.idList[i])});
        }
        /!*db.collection('ProvinceData').deleteOne({ "_id": ObjectId(req.body.idList)});*!/

        db.close(function(){
            console.log("Connection Closed... :-)");
        });
        res.send();
    });*/
    res.statusCode=200;
    res.statusMessage="Sucessfully signed up";
    res.send();
});

router.post('/editProvinceData', function (req, res) {
    var provinceData = getDataFrmProvince();
    var index = getIndexOfIdProvince(req.body.id);
    console.log("Editing id : " + index);
    if(index != -1) {
        provinceData[index].province_name = req.body.province_name;
        provinceData[index].ip = req.body.ip;
        provinceData[index].port = req.body.port;
    }
    saveDataToProvince(provinceData);

    res.statusCode=200;
    res.statusMessage="Sucessfully signed up";
    res.send();
    /*MongoClient.connect(url, function(err, db) {

        console.log("Editing Province Data... " + req.body._id);
        db.collection('ProvinceData').updateOne(
            { "_id": ObjectId(req.body._id)},
            {
                $set: {'province_name': req.body.province_name, 'ip': req.body.ip, 'port': req.body.port}
            }
        );

        db.close(function(){
            console.log("Connection Closed... :-)");
        });
        res.send();
    });*/
});

// Return router
module.exports = router;