@@ -5,87 +5,15 @@ const {
55 loginRequired,
66 NotFound,
77 paginate,
8- Success,
9- Forbidden
8+ Success
109} = require ( "lin-mizar" ) ;
1110const { getSafeParamId } = require ( "../../../libs/util" ) ;
1211const { IdsValidator } = require ( "./validators" ) ;
13- const { get } = require ( "lodash" ) ;
14- const { Message } = require ( "./model" ) ;
15- const { MessageIsReaded } = require ( "./enums" ) ;
16- const Sequelize = require ( "sequelize" ) ;
12+ const { MessageDao } = require ( "./message-dao" ) ;
1713
1814const messageApi = new LinRouter ( {
1915 prefix : "/message"
2016} ) ;
21- exports . messageApi = messageApi ;
22-
23- class MessageDao {
24- async getMessages ( ctx , start , count1 ) {
25- const currentUser = ctx . currentUser ;
26- let condition = { } ;
27- const readed = get ( ctx . query , "readed" ) ;
28- readed && ( condition [ "readed" ] = readed ) ;
29- const pushed = get ( ctx . query , "pushed" ) ;
30- pushed && ( condition [ "pushed" ] = pushed ) ;
31-
32- const { rows, count } = await Message . findAndCountAll ( {
33- where : {
34- ...condition ,
35- user_id : currentUser . id
36- } ,
37- limit : count1 ,
38- offset : start
39- } ) ;
40- return {
41- rows,
42- count
43- } ;
44- }
45-
46- async readMessage ( id ) {
47- const msg = await Message . findByPk ( id ) ;
48- if ( ! msg ) {
49- throw new NotFound ( {
50- msg : "未找到消息"
51- } ) ;
52- }
53- if ( msg . readed === MessageIsReaded . READED ) {
54- throw new Forbidden ( {
55- msg : "不可重复设置为已读"
56- } ) ;
57- }
58- msg . readed = MessageIsReaded . READED ;
59- msg . save ( ) ;
60- }
61-
62- async readMessages ( v ) {
63- await Message . update (
64- {
65- readed : MessageIsReaded . READED
66- } ,
67- {
68- where : {
69- [ Sequelize . Op . in ] : v . get ( "body.ids" )
70- }
71- }
72- ) ;
73- }
74-
75- async deleteMessage ( id ) {
76- const msg = await Message . findByPk ( id ) ;
77- if ( ! msg ) {
78- throw new NotFound ( {
79- msg : "未找到消息"
80- } ) ;
81- }
82- await Message . destroy ( {
83- where : {
84- id : id
85- }
86- } ) ;
87- }
88- }
8917
9018const messageDao = new MessageDao ( ) ;
9119
@@ -178,3 +106,5 @@ messageApi.get("/test", async ctx => {
178106 msg : "hello baby!"
179107 } ) ;
180108} ) ;
109+
110+ module . exports = { messageApi } ;
0 commit comments