-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-manager.js
198 lines (154 loc) · 5.31 KB
/
db-manager.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
188
189
190
191
192
193
194
195
196
197
198
initUsers = require('./database/users.json');
initEvents = require('./database/events.json');
initItems = require('./database/items.json');
class DatabaseManager {
constructor(){
if (!DatabaseManager.initialized){
DatabaseManager.users = initUsers;
DatabaseManager.events = initEvents;
DatabaseManager.items = initItems;
DatabaseManager.initialized = true;
}
}
/*Getters*/
getUser(username){
return DatabaseManager.users[username];
}
getUserFriends(username){
// TODO: nuke passwords and filter by user's friends
return DatabaseManager.users;
}
getEvent(eventId){
return DatabaseManager.events[eventId];
}
getItem(itemId){
return DatabaseManager.items[itemId];
}
getUserOwnedEvents(username){
/*Returns all events owned by the specified user in an array []*/
var user_events = Object.values(DatabaseManager.events).filter(event => event.owner === username);
return user_events;
}
getUserInvitedEvents(username){
/*Returns all events which the user has been invited to in an array []*/
var user_events = Object.values(DatabaseManager.events).filter(event => event.guests.includes(username));
return user_events;
}
getItemsForEvent(eventId){
var event = DatabaseManager.events[eventId];
var retItems = {};
for(var category in event.categories){
event.categories[category].forEach(itemId => {
retItems[itemId] = DatabaseManager.items[itemId];
});
}
return retItems;
}
getUsersForEvent(eventId){
var event = DatabaseManager.events[eventId];
var retUsers = {};
if(!event.guests){
return retUsers;
}
event.guests.forEach(username => {
retUsers[username] = DatabaseManager.users[username];
});
// Add host too
retUsers[event.owner] = DatabaseManager.users[event.owner];
return retUsers;
}
/*Modifiers*/
addItem(name, description, quantity, points, category, eventId){
/*Add event item*/
var item = {
"id": Date.now(), // Make an id somehow
"name": name,
"description": description,
"quantity": quantity,
"points": points,
"claimedBy": {}
}
DatabaseManager.items[item.id] = item;
DatabaseManager.events[eventId].categories[category].push(item.id);
}
removeItem(itemId){
/*Remove an item*/
delete DatabaseManager.items[itemId]
}
updateItem(item){
/*Update an item*/
}
claimItem(itemId, username, quantity){
/* Assign user as bringing an item*/
if(quantity === 0){
delete DatabaseManager.items[itemId].claimedBy[username];
} else {
DatabaseManager.items[itemId].claimedBy[username] = quantity;
}
}
addEvent(title, date, type, owner, guests, categories){
/*Create an event*/
var event = {
"id": Date.now(),
"title": title,
"date": date,
"type": type,
"owner": owner,
"guests": guests,
"categories": {}
}
DatabaseManager.events[event.id] = event;
}
removeEvent(eventId){
/*Remove an event*/
var items = this.getItemsForEvent(eventId);
for(var item in items){
delete DatabaseManager.items[item];
}
delete DatabaseManager.events[eventId];
}
updateEvent(title, date, type, guests, eventId){
var oldGuests = this.getUsersForEvent(eventId);
var event = DatabaseManager.events[eventId];
var removedGuests = Object.values(oldGuests).filter(g => !guests.includes(g.username) && g.username != event.owner);
// If any users were removed, remove them from all the items they had claimed.
if(removedGuests.length > 0){
var items = this.getItemsForEvent(eventId);
removedGuests.forEach(rGuest =>{
for(var itemId in items){
if(rGuest.username in items[itemId].claimedBy){
delete DatabaseManager.items[itemId].claimedBy[rGuest.username];
}
}
});
}
/*Update an event*/
DatabaseManager.events[eventId].title = title;
DatabaseManager.events[eventId].date = date;
DatabaseManager.events[eventId].type = type;
DatabaseManager.events[eventId].guests = guests;
}
addUser(name, username, password){
/*Create an event*/
var user = {
"username": username,
"name": name,
"password": password
}
DatabaseManager.users[username] = user;
}
removeUser(username){
/*Remove an event*/
delete DatabaseManager.users[username];
}
updateUser(user){
/*Update an event*/
}
addCategory(category, eventId) {
DatabaseManager.events[eventId].categories[category] = [];
}
removeCategory(category) {
delete DatabaseManager.events[eventId].categories[category];
}
}
module.exports = DatabaseManager;