@@ -7,7 +7,56 @@ const { db } = require("lin-mizar/lin/db");
77
88const { 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
1261Poem . 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-
113113module . exports = { Poem } ;
0 commit comments