mirror of https://github.com/rocboss/paopao-ce
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
1.4 KiB
65 lines
1.4 KiB
// Copyright 2022 ROC. All rights reserved.
|
|
// Use of this source code is governed by a MIT style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package jinzhu
|
|
|
|
import (
|
|
"github.com/rocboss/paopao-ce/internal/core"
|
|
"github.com/rocboss/paopao-ce/internal/core/ms"
|
|
"github.com/rocboss/paopao-ce/internal/dao/jinzhu/dbr"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var (
|
|
_ core.MessageService = (*messageSrv)(nil)
|
|
)
|
|
|
|
type messageSrv struct {
|
|
db *gorm.DB
|
|
}
|
|
|
|
func newMessageService(db *gorm.DB) core.MessageService {
|
|
return &messageSrv{
|
|
db: db,
|
|
}
|
|
}
|
|
|
|
func (s *messageSrv) CreateMessage(msg *ms.Message) (*ms.Message, error) {
|
|
return msg.Create(s.db)
|
|
}
|
|
|
|
func (s *messageSrv) GetUnreadCount(userID int64) (int64, error) {
|
|
return (&dbr.Message{}).CountUnread(s.db, userID)
|
|
}
|
|
|
|
func (s *messageSrv) GetMessageByID(id int64) (*ms.Message, error) {
|
|
return (&dbr.Message{
|
|
Model: &dbr.Model{
|
|
ID: id,
|
|
},
|
|
}).Get(s.db)
|
|
}
|
|
|
|
func (s *messageSrv) ReadMessage(message *ms.Message) error {
|
|
message.IsRead = 1
|
|
return message.Update(s.db)
|
|
}
|
|
|
|
func (s *messageSrv) GetMessages(userId int64, offset, limit int) ([]*ms.MessageFormated, error) {
|
|
messages, err := (&dbr.Message{}).List(s.db, userId, offset, limit)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
mfs := []*dbr.MessageFormated{}
|
|
for _, message := range messages {
|
|
mf := message.Format()
|
|
mfs = append(mfs, mf)
|
|
}
|
|
return mfs, nil
|
|
}
|
|
|
|
func (s *messageSrv) GetMessageCount(userId int64) (int64, error) {
|
|
return (&dbr.Message{}).Count(s.db, userId)
|
|
}
|