vegapokerbot/internal/db/model/chat.go

53 lines
1.1 KiB
Go
Raw Normal View History

2024-05-07 21:49:09 +03:00
package model
import (
"database/sql/driver"
"encoding/json"
"errors"
)
type Chat struct {
ID uint64 `gorm:"primaryKey; autoIncrement" json:"id"`
2024-05-09 17:42:42 +03:00
TelegramID int64 `gorm:"column:telegram_id; not null" json:"telegramId"`
UserID int64 `gorm:"column:user_id; not null"`
2024-05-07 21:49:09 +03:00
Members ChatMembers `gorm:"column:members; not null" json:"members"`
Integrations []Integration `gorm:"foreignKey:ChatID" json:"integrations"`
}
func (Chat) TableName() string {
return "chat"
}
2024-05-09 17:42:42 +03:00
type ChatMembers []int64
2024-05-07 21:49:09 +03:00
func (cm *ChatMembers) Scan(value interface{}) error {
switch v := value.(type) {
case nil:
*cm = nil
return nil
case string:
if err := json.Unmarshal([]byte(v), cm); err != nil {
return err
}
case []byte:
if err := json.Unmarshal(v, cm); err != nil {
return err
}
default:
return errors.New("invalid type")
}
return nil
}
func (cm ChatMembers) Value() (driver.Value, error) {
if cm == nil {
2024-05-09 17:42:42 +03:00
return "[]", nil
2024-05-07 21:49:09 +03:00
}
jsonData, err := json.Marshal(cm)
return string(jsonData), err
}
2024-05-09 17:42:42 +03:00
func (ChatMembers) GormDataType() string {
return "json"
}