-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathbookingsStore.js
More file actions
36 lines (30 loc) · 848 Bytes
/
bookingsStore.js
File metadata and controls
36 lines (30 loc) · 848 Bytes
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
// bookingsStore.js
const fs = require('fs');
class BookingsStore {
constructor(filePath) {
this.filePath = filePath;
this.bookings = {};
if (fs.existsSync(this.filePath)) {
try {
const data = fs.readFileSync(this.filePath, 'utf8');
this.bookings = JSON.parse(data);
} catch (err) {
console.error(`Error reading file from disk: ${err}`);
}
}
}
getUserBookings(username) {
return this.bookings[username] || [];
}
addBooking(username, booking) {
if (!this.bookings[username]) this.bookings[username] = [];
this.bookings[username].push(booking);
this._writeBookingsToFile();
}
_writeBookingsToFile() {
fs.writeFile(this.filePath, JSON.stringify(this.bookings), (err) => {
if (err) console.error(err);
});
}
}
module.exports = BookingsStore;