-
Notifications
You must be signed in to change notification settings - Fork 0
/
storeClass.js
119 lines (100 loc) · 2.79 KB
/
storeClass.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
const Message = require('./messageClass');
const User = require('./userClass');
function Store(){
let storage = [];
let userStorage = [];
let id = getID();
let Created_on = getcurrTime();
let Description = 'Message storage'
function sendMsg(msg){
if (areUsersValid(msg.to, msg.from, userStorage) === true){
storage.push(msg)
}
};
function storeUser(user) {
userStorage.push(user);
}
function deleteMsg(msg){
if (areUsersValid(msg.to, msg.from, userStorage) === true){
for (let object of storage){
if(object === msg){
storage = storage.filter(left => left !== msg)
}
return storage;
}
}
};
function purgeStorage(){
if (storage.length !== 0){
storage.length = 0;
console.log('Storage purged');
console.log(storage);
}else {
console.log('Storage already empty');
}
};
function getInbox(username){
if (isUserValid(username, userStorage)){
let inbox = storage.filter(msg => msg.to === username);
console.log(inbox);
return inbox;
}
};
function getOutbox(username){
if (isUserValid(username, userStorage)){
let outbox = storage.filter(msg => msg.from === username);
console.log(outbox);
return outbox;
}
};
let user1 = new User('Temi','TemiOgb','[email protected]');
let user2 = new User('Chris','ChrisR','[email protected]');
let user3 = new User('Sean','SeanCon','[email protected]');
storeUser(user1);
storeUser(user2);
storeUser(user3);
let message1 = new Message("Grab my phone", user1.username, user2.username);
let message2 = new Message("Check it", user1.username, user3.username);
let message3 = new Message("Thanks", user2.username, user1.username);
let message4 = new Message("Hi", user2.username, user3.username);
let message5 = new Message("Hello", user3.username, user1.username);
let message6 = new Message("Waddup", user3.username, user2.username);
// let goodnight6 = new Message("Coming", 'Cristiano', 'Gregg');
// let goodnight7 = new Message("Why Not?", 'Sade', 'Gregg');
sendMsg(message1);
sendMsg(message2);
sendMsg(message3);
sendMsg(message4);
sendMsg(message5);
sendMsg(message6);
//getInbox('ChrisR');
//getOutbox('TemiOgb');
deleteMsg(message1);
//purgeStorage();
console.log(storage);
//console.log(userStorage);
//console.log(newarray);
//console.log(message1['to']);
};
Store();
function getID(){
return Math.round(Math.random()*1000)
}
function getcurrTime(){
return Date.now();
}
function areUsersValid(to, from, userArray) {
let newArray = userArray.map(user => user.username);
if (newArray.includes(to) && newArray.includes(from)){
return true;
}
return false;
};
function isUserValid(username, userArray) {
let newArray = userArray.map(user => user.username);
if (newArray.includes(username)){
return true;
}
return false;
};
//Pushing to git repo