-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
109 lines (103 loc) · 3.89 KB
/
scripts.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
var fs = require('fs');
var gui = require('nw.gui');
var mysql = require('mysql');
var users = require('./users').profiles;
var user = require('./user.js');
var homePage = fs.readFileSync("home.html","utf-8");
var loginPage = fs.readFileSync("login.html","utf-8");
var librarianPage = fs.readFileSync("librarian.html","utf-8");
var new_win = gui.Window.get();
var connection = mysql.createConnection(require('./connect').user);
connection.connect();
var library={};
var operations;
var showBooksPage = fs.readFileSync("showBooks.html",'utf-8');
var showAdd = function(){
document.getElementById('add').style.display = 'block';
document.getElementById('remove').style.display = 'none';
document.getElementById('update').style.display = 'none';
document.getElementById('isbn_error').style.display = 'none';
};
var showRemove = function(){
var ISBNs = [];
var getISBN = function (record) {
var ISBN = "<option value='"+record.isbn+"'>"+record.isbn+"</option>";
ISBNs.push(ISBN);
};
connection.query('select isbn from catalog;',function(err, rows, fields){
if (err) throw err;
rows.forEach(getISBN);
document.getElementById('remove').style.display = 'block';
document.getElementById('current_book').style.display = 'none';
document.getElementById('add').style.display = 'none';
document.getElementById('update').style.display = 'none';
document.getElementById('book_removed').style.display = 'none';
document.write(librarianPage.replace('SELECT_ISBN',isbns));
});
};
var showUpdate = function(){
document.getElementById('update').style.display = 'block';
document.getElementById('remove').style.display = 'none';
document.getElementById('add').style.display = 'none';
};
var addBook = function(){
var Book;
Book= document.getElementById('add_isbn').value+',';
Book+= document.getElementById('add_price').value+',\'';
Book+= document.getElementById('add_title').value+'\',\'';
Book+= document.getElementById('add_author').value+'\',\'';
Book+= document.getElementById('add_publisher').value+'\'';
connection.query('insert into catalog() values('+Book+');',add);
connection.query('commit',function(){});
};
var add =function(err, rows, fields) {
if (err)
document.getElementById('isbn_error').style.display = 'block';
};
var showBook = function() {
isbn = document.getElementById('isbnList').value;
connection.query('select * from catalog where isbn like '+isbn,showRecord);
};
var showRecord = function(err, rows, fields) {
if (err) throw err;
console.log(rows);
document.getElementById('bookTitle').value = rows[0].book_title;
document.getElementById('bookAuthor').value = rows[0].author;
document.getElementById('current_book').style.display = 'block';
};
var visitHome = function(profile){
var isUser = profile.id!="admin" &&profile.password!="admin";
if(isUser)
window.document.write(homePage.replace('USERNAME',profile.Name));
else
window.document.write(librarianPage.replace('USERNAME',profile.Name));
};
var removeBook = function(){
var book = document.getElementById("isbnList");
connection.query('delete from catalog where isbn like '+isbn,function(err, rows, fields){
if (err) throw err;
document.getElementById('book_removed').style.display = 'block';
});
};
var visitLogin = function(){
console.log("INcorrect id");
};
var login = function(){
var userID = window.document.getElementsByName('userID')[0].value;
var password =window.document.getElementsByName('password')[0].value;
var profile = users[userID];
var isValid = profile && profile.password === password;
if(isValid) visitHome(profile);
else visitLogin();
};
var home = function(req, res){
console.log('cookie',req.cookie);
var userId = req.headers.cookie &&
req.headers.cookie.split('=')[1];
var profile = userId && users[userId];
if(profile) res.render('home',{notices:notices});
else visitLogin(req,res);
};
var logout = function(){
window.document.write(loginPage);
};