-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.js
181 lines (145 loc) · 2.99 KB
/
report.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
const crypto = require('crypto');
const User = require('./user');
const consts = require('./consts').report;
const DB = require('./db');
class Report{
constructor(id) {
if(!Report.has(id)) throw new Error("Report not exist");
this.id = id;
}
get(k) {
return Report.db.data[this.id][k];
}
set(k, v) {
Report.db.data[this.id][k] = v;
Report.db.update();
}
remove(k) {
if(!k) {
delete Report.db.data[this.id];
} else {
delete Report.db.data[this.id][k];
}
Report.db.update();
}
get user() {
return new User(this.userid);
}
get name() {
return this.user.name;
}
get size() {
return this.get("size");
}
set size(size) {
this.set("size", size);
}
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 description() {
return this.get("description");
}
set description(description) {
this.set("description", description);
}
get checkedsize() {
return this.get("checkedsize");
}
set checkedsize(checkedsize) {
this.set("checkedsize", checkedsize);
}
get score() {
return this.get("score");
}
set score(score) {
this.set("score", score);
}
get files() {
return this.get("files");
}
set files(files) {
this.set("files", files);
}
get stime() {
return this.get("stime");
}
static add(obj) {
Report.db.data[obj.id] = obj;
Report.db.update();
return new Report(obj.id);
}
static create(opts) {
if(!opts) throw new TypeError("Options not defined");
opts.id = Report.createId();
opts.stime = Date.now();
return Report.add(opts);
}
static has(id) {
return (id in Report.db.data);
}
static createId(){
const id = crypto.randomBytes(consts.length).toString("hex");
if(Report.has(id)) {
return Report.createId();
}
delete Report.db.data[id];
Report.db.update();
return id;
}
static getReportsById(id) {
const reports = [];
for(let i in Report.db.data) {
if(Report.db.data[i].userid === id) {
reports.push(new Report(i));
}
}
return reports;
}
static getReportsByType(type) {
const reports = [];
for(let i in Report.db.data) {
const report = new Report(i);
if(report.user.type === type) {
reports.push(report);
}
}
return reports;
}
static get all() {
const reports = [];
for(let i in Report.db.data) {
reports.push(new Report(i));
}
return reports;
}
}
Report.db = new DB(consts.file);
module.exports = Report;