forked from mhajah/projekt_weppo2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
database_handling.js
139 lines (121 loc) · 4.45 KB
/
database_handling.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
139
var mssql = require('mssql');
//import * as mssql from 'mssql';
conn = new mssql.ConnectionPool(
'server=34.29.94.142,1433;database=myshop ;user id=sqlserver;password=nokia123;Trusted_Connection=True;TrustServerCertificate=True;');
async function querry(sql){
var conn = new mssql.ConnectionPool(
'server=34.29.94.142,1433;database=myshop;user id=sqlserver;password=nokia123;Trusted_Connection=True;TrustServerCertificate=True;');
try{
await conn.connect();
var request = new mssql.Request(conn);
try{
var result_request = await request.query(sql);
//console.log(result_request);
console.log("Success");
}
catch(err){
if (conn.connected)
conn.close();
console.log(err);
}
await conn.close();
console.log(result_request.recordsets[0]);
return result_request.recordsets[0];
}
catch (err) {
if (conn.connected)
conn.close();
console.log(err);
}
}
async function insert_product(name,description,imgLink,price){
var sql = `INSERT INTO products (name, description, imgLink, price) VALUES ('${name}','${description}','${imgLink}','${price}')`;
querry(sql);
}
async function insert_user(name,password,perm){
var sql = `INSERT INTO users (name, password,perm) VALUES ('${name}','${password}',${perm})`;
querry(sql);
}
async function delete_user_from_datebase(name){
var sql = `DELETE FROM users WHERE name ='${name}'`;
querry(sql)
}
async function delete_product_from_datebase(name){
var sql = `DELETE FROM products WHERE name ='${name}'`;
querry(sql)
}
//zwraca tablicę akutalnych userów z bazy danych
async function pullUsersFromDB(arguments) {
var usersTAB = [];
try {
await conn.connect();
var request = new mssql.Request(conn);
var result_users = await request.query('select * from users');
result_users.recordset.forEach(user => {
usersTAB.push({'name': user.name,'password':user.password,'perm': user.perm});
console.log(">>Pobieram uzytkownikow z bazy danych...")
})
await conn.close();
}
catch (err) {
if (conn.connected)
conn.close();
console.log(err);
}
console.log(usersTAB)
return usersTAB;
}
async function pullProductsFromDB(arguments) {
var productsTAB = [];
try {
await conn.connect();
var request = new mssql.Request(conn);
var result = await request.query('select * from products');
result.recordset.forEach(r => {
productsTAB.push({'name': r.name,'description':r.description,'imgLink': r.imgLink,'price':r.price});
})
await conn.close();
}
catch (err) {
if (conn.connected)
conn.close();
console.log(err);
}
return productsTAB;
}
async function update_product(id,name,newname,newprice,newdescription,newimgLink){
var sql;
if(id === NULL){
sql = `UPDATE products SET name = '${newname}', description = '${newdescription}', imgLink = '${newimgLink}', price = '${newprice}' WHERE name = '${name}'`;
}
else{
sql = `UPDATE products SET name = '${newname}', description = '${newdescription}', imgLink = '${newimgLink}', price = '${newprice}' WHERE ID = '${id}'`;
}
querry(sql);
}
async function update_user(id,name,newname,newpassword,newperm){
var sql;
if(id === NULL){
sql = `UPDATE users SET name = '${newname}', password = '${newpassword}', perm = '${newperm}' WHERE name = '${name}'`;
}
else{
sql = `UPDATE users SET name = '${newname}', password = '${newpassword}', perm = '${newperm}' WHERE ID = '${id}'`;
}
querry(sql);
}
async function insert_order(products,amount,user){
var sql = `INSERT INTO orders (products, amount, userName) VALUES ('${products}', ${amount}, '${user}')`;
querry(sql);
}
async function select_products(){
return querry('select * from products');
}
async function select_users(){
return querry('select * from users');
}
function result_select_users(){
return select_users();
}
//export{update_product,update_user,delete_product_from_datebase,delete_product_from_datebase,insert_product,insert_user}
module.exports = {insert_product,insert_user,update_user,update_product,delete_user_from_datebase,
delete_product_from_datebase,insert_order,select_products,select_users,pullProductsFromDB,pullUsersFromDB};