-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemployee.js
More file actions
executable file
·79 lines (63 loc) · 2.1 KB
/
employee.js
File metadata and controls
executable file
·79 lines (63 loc) · 2.1 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
var fileSystem = require("fs");
var util = require("./util.js");
var employeeArrCache = {};
var primaryKey = 1;
function addEmployee(body) {
var newEmployee = JSON.parse(body);
increasePrimaryKey();
employeeArrCache[primaryKey] = newEmployee;
var convertToString = JSON.stringify(employeeArrCache);
fileSystem.writeFileSync(__dirname + "/data/data.txt", convertToString);
}
function getEmployee(param) {
var result = lookupEmployeeById(param);
// var startTime = new Date().getTime();
// while (new Date().getTime() < startTime + 15000);
return JSON.stringify(result);
}
function updatetEmployee(body) {
var toBeUpdateEmployee = JSON.parse(body);
// console.log("Waiting");
// var startTime = new Date().getTime();
// while (new Date().getTime() < startTime + 15000);
// console.log("finish Waiting");
if (toBeUpdateEmployee.id) {
employeeArrCache[toBeUpdateEmployee.id] = toBeUpdateEmployee;
var convertToString = JSON.stringify(employeeArrCache);
//fileSystem.writeFileSync(__dirname + "/data/data.txt", convertToString);
fileSystem.writeFile(__dirname + "/data/data.txt", convertToString);
}
}
function lookupEmployeeById(param) {
if (util.isEmpty(employeeArrCache)) {
readFromFileToCache();
}
if (!util.isEmpty(param)) {
var result = employeeArrCache[param.id];
if (result) {
return result;
} else {
return {};
}
} else {
var empList = [];
for ( var p in employeeArrCache) {
empList.push(employeeArrCache[p]);
}
return empList;
}
return {};
}
function readFromFileToCache() {
var data = fileSystem.readFileSync(__dirname + "/data/data.txt", "utf8");
console.log(data);
employeeArrCache = JSON.parse(data);
}
function increasePrimaryKey() {
primaryKey = Object.keys(employeeArrCache).length + 1;
return primaryKey;
}
exports.addEmployee = addEmployee;
exports.getEmployee = getEmployee;
exports.updatetEmployee = updatetEmployee;
exports.readFromFileToCache = readFromFileToCache;