forked from moondemon68/POSBar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
138 lines (123 loc) · 3.16 KB
/
app.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const fs = require("fs");
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
// VARIABLES
var accessLevel = 0;
var currentUser = "";
var users = {
username: [],
password: [],
level: []
};
var reports = {
pelapor: [],
laporan: []
};
// END OF VARIABLES
// READ FILES
fs.readFile('database/users.json','utf8',function (err,data) {
if (err) {
console.log(err);
} else {
users = JSON.parse(data);
}
});
fs.readFile('database/reports.json','utf8',function (err,data) {
if (err) {
console.log(err);
} else {
reports = JSON.parse(data);
}
});
// END OF READ FILES
// HOME PAGE
app.get("/",function(req,res) {
var jsonUsers = JSON.stringify(users);
fs.writeFile('database/users.json',jsonUsers,'utf8',function(req,res) {
console.log(jsonUsers);
});
var jsonReports = JSON.stringify(reports);
fs.writeFile('database/reports.json',jsonReports,'utf8',function(req,res) {
console.log(jsonReports);
});
if (accessLevel === 0) res.render("login");
else if (accessLevel === 1) res.render("cashier");
else if (accessLevel === 2) res.render("supervisor");
else if (accessLevel === 3) res.render("admin");
else res.render("404");
});
// END OF HOME PAGE
// LOGIN
app.post("/login",function(req,res) {
for (var i=0;i<users.username.length;i++) {
if (users.username[i] === req.body.username && users.password[i] === req.body.password) {
currentUser = req.body.username;
accessLevel = users.level[i];
break;
}
}
console.log(currentUser,accessLevel);
res.redirect("/");
});
// END OF LOGIN
// LOGOUT
app.get("/logout",function(req,res) {
currentUser="";
accessLevel=0;
res.redirect("/");
});
// END OF LOGOUT
// REGISTER NEW USERS
app.get("/register",function(req,res) {
res.render("register");
});
app.post("/register",function(req,res) {
users.username.push(req.body.username);
users.password.push(req.body.password);
users.level.push(Number(req.body.level));
res.redirect("/");
});
// END OF REGISTER
// LAPORAN
app.get("/lapor",function(req,res) {
res.render("lapor");
});
app.post("/lapor",function(req,res) {
reports.pelapor.push(req.body.pelapor);
reports.laporan.push(req.body.laporan);
res.redirect("/");
});
app.get("/lihat-laporan",function(req,res) {
console.log(accessLevel);
if (accessLevel >= 0) res.render("lihat-laporan",{reports:reports});
else res.render("404");
});
// END OF LAPORAN
<<<<<<< HEAD
// REPORT
app.post("/lapor",function(req,res) {
res.render("lapor");
});
// WIPE DATABASE
app.get("/wipe",function(req,res) {
=======
// WIPERS
app.get("/wipe-users",function(req,res) {
>>>>>>> 127c8a1889e91e59ae5cd757680e8d06d4926836
users = {
username: [],
password: [],
level: []
};
res.redirect("/");
});
// END OF WIPERS
app.listen(3000, function() {
console.log("Server started on port 3000");
});