-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
187 lines (168 loc) · 4.63 KB
/
database.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import mysql from "mysql2"
import dotenv from 'dotenv'
dotenv.config()
// prepared statement: sending sql and values separately
const pool = mysql.createPool({
host: process.env.MYSQL_HOST,
user: process.env.MYSQL_USER,
password: process.env.MYSQL_PASSWORD,
database: process.env.MYSQL_DATABASE
}).promise()
export async function getAccounts() {
const [rows] = await pool.query("SELECT * FROM account")
return rows
}
export async function getProfiles() {
const [rows] = await pool.query("SELECT * FROM profile")
return rows
}
export async function getCards() {
const [rows] = await pool.query("SELECT * FROM cards")
return rows
}
export async function getAccount(aid) {
const [rows] = await pool.query(`
SELECT
*
FROM
account
WHERE
AccountID = ?
`, [aid])
return rows[0]
}
export async function createAccount(Username, Password, FName, LName, Age, Gender, Email) {
const result = await pool.query(`
INSERT INTO account (Username, Password, FName, LName, Age, Gender, Email)
VALUES (?, ?, ?, ?, ?, ?, ?);
`, [Username, Password, FName, LName, Age, Gender, Email])
// return the AID
return [result[0].insertId];
}
// need to make sure there isn't already a profile named the same under the account
export async function createProfile(ProfileName, aid, Currency = 0, Exp = 0) {
const result = await pool.query(`
INSERT INTO profile (ProfileName, AccountID, Currency, Exp)
VALUES (?, ?, ?, ?);
`, [ProfileName, aid, Currency, Exp]);
// returns the PID
return [result[0].insertId];
}
export async function appendPIdToAccount(AccountID, ProfileID) {
const result = await pool.query(`
UPDATE account
SET ProfileID = ?
WHERE AccountID = ?;
`, [ProfileID, AccountID]);
return result;
}
export async function getProfile(profileID) {
const [rows] = await pool.query(`
SELECT
*
FROM
profile
WHERE
ProfileID = ?
`, [profileID])
return rows[0]
}
export async function getProfileCards(profileID) {
const [rows] = await pool.query(`
SELECT pc.ProfileID, pc.CardID, pc.CardCount, c.Name, c.Description
FROM profile_cards pc
LEFT JOIN cards c ON pc.CardID = c.CardID
WHERE pc.ProfileID = ?
`, [profileID]);
return rows
}
export async function initializeCards(pid, cardsJSON) {
const results = []
for (const cardID in cardsJSON) {
const cardCount = Number(cardsJSON[cardID])
const result = await pool.query(`
INSERT INTO profile_cards (ProfileID, CardID, CardCount)
VALUES (?, ?, ?);
`, [pid, cardID, cardCount]);
results.push(result)
}
return results;
}
export async function addDefaultCards(pid) {
const cardsJSON = {
'17275': '3',
'13208': '3',
'11865': '3',
'18362': '3',
'10247': '3',
'19843': '3',
'25858': '5',
'26836': '5',
'24487': '5',
'28100': '5',
'25734': '5',
'25281': '5',
'25902': '5',
'25991': '5'
}
await initializeCards(pid, cardsJSON)
}
export async function addCard(pid, cardID) {
const existingRow = await pool.query(`
SELECT CardCount
FROM profile_cards
WHERE ProfileID = ? AND CardID = ?
`, [pid, cardID]);
if (existingRow[0].length > 0) {
const CardCount = Number(existingRow[0][0].CardCount)
const updatedCount = CardCount + 1;
const result = await pool.query(`
UPDATE profile_cards
SET CardCount = ?
WHERE ProfileID = ? AND CardID = ?
`, [updatedCount, pid, cardID]);
return result;
}
else {
const result = await pool.query(`
INSERT INTO profile_cards (ProfileID, CardID, CardCount)
VALUES (?, ?, 1);
`, [pid, cardID]);
return result;
}
}
// what if mulitple people have same prof name
export async function getProfileID(aid) {
const [rows] = await pool.query(`
SELECT
ProfileID
FROM
profile
WHERE
AccountID = ?
`, [aid])
return rows[0]
}
// what if multiple people have same account username
export async function getAccountID(username) {
const [rows] = await pool.query(`
SELECT
AccountID
FROM
account
WHERE
Username = ?
`, [username])
return rows[0]
}
export async function getAccountIDWithAuth(username, password) {
const [rows] = await pool.query(`
SELECT
AccountID
FROM
account
WHERE
Username = ? AND Password = ?
`, [username, password])
return rows[0]
}