aboutsummaryrefslogtreecommitdiffstats
path: root/csci4131/hw7/dbio.js
blob: 96faf93f7e36a97ad6e4466697b6f1a376bf6125 (plain) (blame)
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
var mysql = require("mysql");
var bcrypt = require("bcrypt");
var fs = require("fs");
var xml2js = require("xml2js");
var parser = new xml2js.Parser();
var conInfo;
var connection;

fs.readFile(__dirname + '/dbconfig.xml', function (err, data) {
	if (err) throw err; 
	parser.parseString(data, function (err, result) { 
		if (err) throw err; 
		conInfo = result; 
	});
	connection = mysql.createConnection({
		host: conInfo.dbconfig.host[0],
		user: conInfo.dbconfig.user[0],
		password: conInfo.dbconfig.password[0],
		database: conInfo.dbconfig.database[0],
		port: conInfo.dbconfig.port[0]
	});

	connection.connect(function (err) {
		if (err) {
			throw err;
		};
		console.log("Connected to MYSQL database!");
	});
});



function passcheck(user,pass) {
	return new Promise(function(resolve, reject) {
		connection.query('SELECT * FROM tbl_accounts', function(err, rows, fields) {
			let ret = [];
			if (err) {
				return reject(err);
			}
			for (var i = 0; i < rows.length; i++) {
				if (rows[i].acc_login.localeCompare(user) === 0) {
					if (bcrypt.compareSync(pass, rows[i].acc_password)) {
						ret += rows[i];
					}
				}
			}
			resolve(ret);
		});
	});
}

function getContacts() {
	return new Promise (function(resolve, reject) {
		let conTab = []
		connection.query('SELECT * FROM tbl_contacts', function (err, rows, fields) {
			if (err) throw err;
			resolve(rows);
		});
	});
}

function addContacts(contact) {
	let newCon = {
		name: contact.name,
		category: contact.category,
		location: contact.location,
		contact_info: contact.contact,
		email: contact.email,
		website: contact.website,
	}
	return new Promise (function (resolve, reject) {
		connection.query('SELECT * FROM tbl_contacts where name=?', contact.name, function(err, rows, fields) {
			if (err) throw err;
			console.log("Table found")
			if (rows.length > 0) {
				// Duplicate 
				console.log("found Duplicate name!");
				resolve(false);
			} else {
				connection.query('INSERT tbl_contacts SET ?', newCon, function (err, result) {  //Parameterized insert
					if (err) throw err;
					console.log("Values inserted.");
					resolve(true);
				});
			}
		});
	});
}
	


function editContact(contact) {
	let edit = {
		name: contact.name,
		category: contact.category,
		location: contact.location,
		contact_info: contact.contact,
		email: contact.email,
		website: contact.website,
	}
	
	return new Promise(function (resolve, reject) {
		connection.query('SELECT * FROM tbl_contacts where name=?', contact.name, function (err, rows, fields) {
			if (err) throw err;
			if (rows.length == 0) {
				console.log("Name Changed!");
				resolve(false);
			} else {
				connection.query('UPDATE tbl_contacts SET ? WHERE name=?', [edit, edit.name], function(err, result) {
					if (err) throw err;
					console.log("Value edited successfully?")
					resolve(true);
				});
			}
		});
	});
}

function deleteContact(contact) {
	return new Promise(function(resolve, reject) {
		connection.query('DELETE FROM tbl_contacts WHERE name=?', contact, function (err, result) {
			if (err) throw err;
			console.log("Row deleted!")
			resolve();
		})
	})
}



  
exports.addContact = addContacts;
exports.query = passcheck;
exports.getContacts = getContacts;
exports.deleteContact = deleteContact;
exports.editContact = editContact;