-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapplication.js
197 lines (159 loc) · 3.44 KB
/
application.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
const crypto = require('crypto');
const User = require('./user');
const DB = require('./db');
const consts = require('./consts').application;
class Application{
constructor(id) {
if(!Application.has(id)) throw new Error("Application not exist");
this.id = id;
}
get(k) {
return Application.db.data[this.id][k];
}
set(k, v) {
Application.db.data[this.id][k] = v;
Application.db.update();
}
remove(k) {
if(!k) {
delete Application.db.data[this.id];
} else {
delete Application.db.data[this.id][k];
}
Application.db.update();
}
get user() {
return new User(this.userid);
}
get name() {
return this.user.name;
}
get type() {
return this.get("type");
}
set type(size) {
this.set("type", type);
}
get userid() {
return this.get("userid");
}
set userid(userid) {
this.set("userid", userid);
}
get title() {
return this.get("title");
}
set title(title) {
this.set("title", title)
}
get begin() {
return this.get("begin");
}
set begin(begin) {
this.set("begin", begin);
}
get time() {
return this.get("time");
}
set time(time) {
this.set("time", time);
}
get place() {
return this.get("place");
}
set place(place) {
this.set("place", place);
}
get times() {
return [
this.get("time1"),
this.get("time2"),
this.get("time3")
];
}
get places() {
return [
this.get("place1"),
this.get("place2"),
this.get("place3")
];
}
get reply() {
return this.get("reply");
}
set reply(reply) {
this.set("reply", reply);
}
get score() {
return this.get("score");
}
set score(score) {
this.set("score", score);
}
get description() {
return this.get("description");
}
set description(description) {
this.set("description", description);
}
get files() {
return this.get("files");
}
set files(files) {
this.set("files", files);
}
get stime() {
return this.get("stime");
}
static add(obj) {
Application.db.data[obj.id] = obj;
Application.db.update();
return new Application(obj.id);
}
static create(opts) {
if(!opts) throw new TypeError("Options not defined");
opts.id = Application.createId();
opts.stime = Date.now();
return Application.add(opts);
}
static has(id) {
return (id in Application.db.data);
}
static createId(){
const id = crypto.randomBytes(consts.length).toString("hex");
if(Application.has(id)) {
return Application.createId();
}
delete Application.db.data[id];
Application.db.update();
return id;
}
static getApplicationsById(id) {
const applications = [];
for(let i in Application.db.data) {
if(Application.db.data[i].userid === id) {
applications.push(new Application(i));
}
}
return applications;
}
static getApplicationsByType(type) {
const applications = [];
for(let i in Application.db.data) {
const application = new Application(i);
if(application.user.type === type || (application.type === "room" && type === "room")) {
applications.push(application);
}
}
return applications;
}
static get all() {
const applications = [];
for(let i in Application.db.data) {
applications.push(new Application(i));
}
return applications;
}
}
Application.db = new DB(consts.file);
module.exports = Application;