-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbid.js
95 lines (90 loc) · 2.14 KB
/
bid.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
const inquirer = require("inquirer");
// connecting db
const mysql = require("mysql2");
const conn = mysql.createConnection(
"mysql://root:fiko1234@localhost:3306/ebay"
);
const pool = conn.promise();
//inquirer
function ask() {
inquirer
.prompt([
{
name: "mehsul",
message: "ne isteyirsen",
type: "list",
choices: ["satmaq", "almaq"],
},
])
.then((choice) => {
if (choice.mehsul == "satmaq") {
addDb();
} else if (choice.mehsul == "almaq") {
delDb();
}
});
}
function run() {
ask();
}
function addDb() {
inquirer
.prompt([
{
name: "mehsul",
message: "mehsulu elave et",
type: "input",
},
{
name: "kateqoriya",
message: "kateqoriya elave et",
type: "input",
},
])
.then((result) => add(result.mehsul, result.kateqoriya, 100));
}
async function add(mehsul, kateqoriya) {
const result = await pool.query(`
insert into auctions(item_name,category, starting_bid) values('${mehsul}','${kateqoriya}','${100}') `);
console.log('elave olundu')
run();
}
async function delDb() {
await list();
inquirer
.prompt([
{
name: "id",
message: "id-ni elave et",
type: "input",
},
{
name: "bid",
message: "bidi elave et",
type: "input",
},
])
.then((result) => del(parseInt(result.id), parseInt(result.bid)));
}
async function del(id, bid) {
const result = await pool.query(`
select * from auctions where id=${id}`);
const product = result[0];
if (product[0]["starting_bid"] >= bid) {
console.log("😞 teklif asagidi:::",bid);
run();
} else {
console.log("🤗 teklif qebul olundu:::", bid);
const ansr = await pool.query(`
update auctions set highest_bid = ${bid} where id = ${id} ;
`);
run();
}
}
async function list() {
const result = await pool.query(`
select * from auctions `);
console.log(result[0]);
return result[0];
}
run();