-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
130 lines (130 loc) · 4.73 KB
/
index.js
File metadata and controls
130 lines (130 loc) · 4.73 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
var request = require('request');
var express = require('express');
var jwt = require('jsonwebtoken');
//const passport = require('passport-jwt');
var bodyParser = require('body-parser');
var pgp = require('pg-promise')( /* options */);
var link = 'postgres://Bookish:ZSE$4rfv@localhost:5432/Bookish';
var db = pgp(link);
var secret = 'shhhhh';
var Book = /** @class */ (function () {
function Book(author, copiesAvailable, ISBN, title, numberInLibrary) {
this.author = author;
this.copiesAvailable = copiesAvailable;
this.ISBN = ISBN;
this.title = title;
this.numberInLibrary = numberInLibrary;
}
return Book;
}());
var Loan = /** @class */ (function () {
function Loan(title, returnDate) {
this.title = title;
this.returnDate = returnDate;
}
return Loan;
}());
main();
function main() {
var port = 3000;
var app = express();
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());
app.use(express.static('frontend'));
// handles login
app.get('/login', function (req, res) {
res.sendFile(__dirname + "/frontend/" + "index.html");
});
app.post('/process_get', function (req, res) {
var response = {
username: req.body.username,
password: req.body.password
};
return validate(response.username, response.password).then(function (token) {
// console.log(token);
res.send({
token: token,
redirect: true
});
}, function (error) {
// console.log('error in validate', error);
res.send({
error: error.toString(),
redirect: false
});
});
});
// handles mainsite
app.get('/Bookish', function (req, res) {
if (validateToken(req)) {
res.sendFile(__dirname + "/frontend/" + "Bookish.html");
}
else {
res.redirect('/login');
}
});
// handles Catalogue
app.post('/process_fetchCatalogue', function (req, res) {
db.any('SELECT * FROM public."Books"')
.then(function (catalogue) {
var bookList = listBooksFromCatalogue(catalogue);
res.send(bookList);
}, function (error) { return res.send(error); });
});
// handles adding a new Book
app.post('/process_addBook', function (req, res) {
var book = new Book(req.body.author, req.body.copiesAvailable, req.body.ISBN, req.body.bookName, req.body.copiesInLibrary);
db.any("INSERT INTO public.\"Books\" VALUES ('" + book.author + "', '" + book.copiesAvailable + "', '" + book.ISBN + "', '" + book.title + "', '" + book.numberInLibrary + "');")
.then(function () {
res.send({ success: true });
}, function (error) { return res.send({ success: false, Error: error }); });
});
// handles fetching user's loans
app.post('/process_fetchLoans', function (req, res, next) {
var id = jwt.verify(req.body.token, secret).foo;
getLoanData(id, res)
.then(function (data) {
return res.send(data.map(function (bookObject) { return new Loan(bookObject.Title, bookObject.Return_Date); }))["catch"](next);
});
});
app.listen(port, function () { return console.log("Example app listening on port " + port + "!"); });
}
function validateToken(req) {
var token = req.query.token;
try {
var decoded = jwt.verify(token, secret);
console.log(decoded.foo);
return Number.isInteger(decoded.foo);
}
catch (_a) {
return false;
}
}
function validate(username, password) {
var token;
return db.any("SELECT * FROM public.\"User\" WHERE \"Username\" = '" + username + "';")
.then(function (user) {
if (user.length === 0) {
throw Error("Username not found");
}
if (user[0].Password === password) {
token = jwt.sign({ foo: user[0].User_ID }, secret);
console.log(token);
return token;
}
else {
throw Error("password incorrect");
}
}, function () {
throw Error("Database not reached");
});
}
function listBooksFromCatalogue(catalogue) {
var bookList = catalogue.map(function (book) {
return new Book(book.Author, book.Copies_Available, book.ISBN, book.Title, book.Number_in_Library);
});
return bookList;
}
function getLoanData(id, res) {
return db.any("SELECT * FROM public.\"Loans\"\n INNER JOIN \"Book_Instances\" ON \"Loans\".\"Book_ID\" = \"Book_Instances\".\"Book_ID\"\n INNER JOIN \"Books\" ON \"Book_Instances\".\"ISBN\" = \"Books\".\"ISBN\"\n WHERE \"User_ID\" = '" + id + "';");
}