turkmentv_sms_transmitter/pkg/data/message.go

37 lines
618 B
Go

package data
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"
}