turkmentv_sms_transmitter/internal/models/message.go

37 lines
620 B
Go
Raw Normal View History

2024-10-09 09:17:17 +00:00
package models
2024-08-29 10:14:19 +00:00
import (
"context"
"encoding/json"
2024-08-30 06:46:15 +00:00
"time"
2024-08-29 10:14:19 +00:00
"gorm.io/gorm"
)
type Message struct {
2024-08-30 06:46:15 +00:00
ID uint `gorm:"primarykey"`
2024-08-29 10:14:19 +00:00
Src string `json:"src"`
Dst string `json:"dst"`
Msg string `json:"msg"`
2024-08-30 06:46:15 +00:00
Ts int `json:"ts"`
2024-08-29 10:14:19 +00:00
}
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 {
2024-08-30 06:46:15 +00:00
m.Ts = int(time.Now().Unix())
2024-08-29 10:14:19 +00:00
if err := db.WithContext(ctx).Create(&m).Error; err != nil {
return err
}
return nil
}
2024-08-30 06:46:15 +00:00
func (m *Message) TableName() string {
return "outbox"
}