Repository navigation
Expand file tree
/
Copy pathLocationDb.js
More file actions
116 lines (103 loc) · 3.45 KB
/
Copy pathLocationDb.js
File metadata and controls
116 lines (103 loc) · 3.45 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
import { cxnPool } from '@/Database';
const tableName = "locations";
// const createLocationsTable = () => {
// const createTableQuery = `
// CREATE TABLE IF NOT EXISTS ${tableName} (
// locationId INT AUTO_INCREMENT PRIMARY KEY,
// locationName VARCHAR(255) NOT NULL,
// locationAddress VARCHAR(255) NOT NULL,
// locationLat DECIMAL(10, 6) NOT NULL,
// locationLong DECIMAL(10, 6) NOT NULL,
// acctId INT NOT NULL, -- New column to store the account ID
// FOREIGN KEY (acctId) REFERENCES accounts(acctId) -- Assuming 'accounts' table has 'acctId' as the primary key
// )
// `;
// cxnPool.query(createTableQuery, (err) => {
// if (err) {
// console.error('Error creating locations table:', err);
// }
// });
// };
// // Call the function to create the table
// createLocationsTable();
// Function to save a location (create) using MySQL
const saveLocation = (locationData) => {
const { locationName, locationAddress, locationLat, locationLong } = locationData;
const query = `INSERT INTO ${tableName} (locationName, locationAddress, locationLat, locationLong) VALUES (?, ?, ?, ?)`;
return new Promise((resolve, reject) => {
cxnPool.query(query, [locationName, locationAddress, locationLat, locationLong], (err, result) => {
if (err) {
console.error('Error saving location:', err);
reject(err);
} else {
resolve(result.insertId);
}
});
});
};
const deleteLocation = (locationId) => {
const query = `DELETE FROM ${tableName} WHERE locationId = ?`;
return new Promise((resolve, reject) => {
cxnPool.query(query, [locationId], (err, result) => {
if (err) {
console.error('Error deleting location:', err);
reject(err);
} else {
resolve(result.affectedRows > 0);
}
});
});
};
const updateLocation = (locationId, updatedData) => {
const { locationName, locationAddress, locationLat, locationLong } = updatedData;
const query = `UPDATE ${tableName} SET locationName = ?, locationAddress = ?, locationLat = ?, locationLong = ? WHERE locationId = ?`;
return new Promise((resolve, reject) => {
cxnPool.query(query, [locationName, locationAddress, locationLat, locationLong, locationId], (err, result) => {
if (err) {
console.error('Error updating location:', err);
reject(err);
} else {
resolve(result.affectedRows > 0);
}
});
});
};
const getLocations = () => {
const query = `SELECT * FROM ${tableName}`;
return new Promise((resolve, reject) => {
cxnPool.query(query, (err, rows) => {
if (err) {
console.error('Error fetching locations:', err);
reject(err);
} else {
resolve(rows);
}
});
});
};
const getLocationById = (locationId) => {
const query = `SELECT * FROM ${tableName} WHERE locationId = ?`;
return new Promise((resolve, reject) => {
cxnPool.query(query, [locationId], (err, row) => {
if (err) {
console.error('Error fetching location by ID:', err);
reject(err);
} else {
resolve(row[0]);
}
});
});
};
const getLocationsByAccountId = (req, res) => {
const { acctId } = req.params;
const query = `SELECT * FROM locations WHERE acctId = ?`;
locationPool.query(query, [acctId], (err, results) => {
if (err) {
console.error('Error fetching locations:', err);
res.status(500).json({ error: 'An error occurred while fetching locations.' });
} else {
res.status(200).json(results);
}
});
};
export { deleteLocation, getLocationById, getLocations, getLocationsByAccountId, saveLocation, updateLocation };