37 lines
620 B
Go
37 lines
620 B
Go
package models
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"time"
|
|
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type Message struct {
|
|
ID uint `gorm:"primarykey"`
|
|
Src string `json:"src"`
|
|
Dst string `json:"dst"`
|
|
Msg string `json:"msg"`
|
|
Ts int `json:"ts"`
|
|
}
|
|
|
|
func (m *Message) Convert(msgBody []byte) error {
|
|
if err := json.Unmarshal(msgBody, &m); err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *Message) Insert(ctx context.Context, db *gorm.DB) error {
|
|
m.Ts = int(time.Now().Unix())
|
|
if err := db.WithContext(ctx).Create(&m).Error; err != nil {
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (m *Message) TableName() string {
|
|
return "outbox"
|
|
}
|