49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package model
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"errors"
|
|
)
|
|
|
|
type Chat struct {
|
|
ID uint64 `gorm:"primaryKey; autoIncrement" json:"id"`
|
|
TelegramID uint64 `gorm:"column:telegram_id; not null" json:"telegramId"`
|
|
UserID uint64 `gorm:"column:user_id; not null"`
|
|
Members ChatMembers `gorm:"column:members; not null" json:"members"`
|
|
Integrations []Integration `gorm:"foreignKey:ChatID" json:"integrations"`
|
|
}
|
|
|
|
func (Chat) TableName() string {
|
|
return "chat"
|
|
}
|
|
|
|
type ChatMembers []uint64
|
|
|
|
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 {
|
|
return nil, nil
|
|
}
|
|
jsonData, err := json.Marshal(cm)
|
|
return string(jsonData), err
|
|
}
|