Skip to content

Commit ed84dd6

Browse files
author
pedro
committed
update:更新model的写法
1 parent d93cbe4 commit ed84dd6

2 files changed

Lines changed: 65 additions & 65 deletions

File tree

app/plugins/notify/app/model.js

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,21 @@ Event.init(
3030
}
3131
);
3232

33-
class Message extends Sequelize.Model {}
33+
class Message extends Sequelize.Model {
34+
toJSON () {
35+
let origin = {
36+
id: this.id,
37+
message: this.message,
38+
event: this.event,
39+
time: this.time,
40+
pushed: this.pushed,
41+
readed: this.readed,
42+
user_id: this.userId,
43+
user_name: this.userName
44+
};
45+
return origin;
46+
}
47+
}
3448

3549
Message.init(
3650
{
@@ -80,18 +94,4 @@ Message.init(
8094
}
8195
);
8296

83-
Message.prototype.toJSON = function () {
84-
let origin = {
85-
id: this.id,
86-
message: this.message,
87-
event: this.event,
88-
time: this.time,
89-
pushed: this.pushed,
90-
readed: this.readed,
91-
user_id: this.userId,
92-
user_name: this.userName
93-
};
94-
return origin;
95-
};
96-
9797
module.exports = { Message, Event };

app/plugins/poem/app/model.js

Lines changed: 50 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,56 @@ const { db } = require("lin-mizar/lin/db");
77

88
const { config } = require("lin-mizar/lin/config");
99

10-
class Poem extends Sequelize.Model {}
10+
class Poem extends Sequelize.Model {
11+
static async search (q) {
12+
const poems = await Poem.findAll({
13+
where: {
14+
title: {
15+
[Sequelize.Op.like]: "%" + q + "%"
16+
}
17+
}
18+
});
19+
return poems;
20+
}
21+
22+
static async getAll (validator) {
23+
const condition = {
24+
delete_time: null
25+
};
26+
validator.get("query.author") &&
27+
(condition["author"] = validator.get("query.author"));
28+
const poems = await Poem.findAll({
29+
where: {
30+
delete_time: null
31+
},
32+
limit: validator.get("query.count")
33+
? validator.get("query.count")
34+
: config.getItem("poem.limit")
35+
});
36+
return poems;
37+
}
38+
39+
static async getAuthors () {
40+
const authors = await db.query(
41+
"select author from poem group by author having count(author)>0"
42+
);
43+
let res = authors[0].map(it => it["author"]);
44+
return res;
45+
}
46+
47+
toJSON () {
48+
let origin = {
49+
id: this.id,
50+
title: this.title,
51+
author: this.author,
52+
dynasty: this.dynasty,
53+
content: this.content,
54+
image: this.image,
55+
create_time: this.createTime
56+
};
57+
return origin;
58+
}
59+
}
1160

1261
Poem.init(
1362
{
@@ -61,53 +110,4 @@ Poem.init(
61110
)
62111
);
63112

64-
Poem.prototype.toJSON = function () {
65-
let origin = {
66-
id: this.id,
67-
title: this.title,
68-
author: this.author,
69-
dynasty: this.dynasty,
70-
content: this.content,
71-
image: this.image,
72-
create_time: this.createTime
73-
};
74-
return origin;
75-
};
76-
77-
Poem.getAll = async function (validator) {
78-
const condition = {
79-
delete_time: null
80-
};
81-
validator.get("query.author") &&
82-
(condition["author"] = validator.get("query.author"));
83-
const poems = await Poem.findAll({
84-
where: {
85-
delete_time: null
86-
},
87-
limit: validator.get("query.count")
88-
? validator.get("query.count")
89-
: config.getItem("poem.limit")
90-
});
91-
return poems;
92-
};
93-
94-
Poem.search = async function (q) {
95-
const poems = await Poem.findAll({
96-
where: {
97-
title: {
98-
[Sequelize.Op.like]: "%" + q + "%"
99-
}
100-
}
101-
});
102-
return poems;
103-
};
104-
105-
Poem.getAuthors = async function () {
106-
const authors = await db.query(
107-
"select author from poem group by author having count(author)>0"
108-
);
109-
let res = authors[0].map(it => it["author"]);
110-
return res;
111-
};
112-
113113
module.exports = { Poem };

0 commit comments

Comments
 (0)