53 lines
1.1 KiB
Go
53 lines
1.1 KiB
Go
package model
|
|
|
|
import (
|
|
"database/sql/driver"
|
|
"encoding/json"
|
|
"errors"
|
|
)
|
|
|
|
type Chat struct {
|
|
ID uint64 `gorm:"primaryKey; autoIncrement" json:"id"`
|
|
TelegramID int64 `gorm:"column:telegram_id; not null" json:"telegramId"`
|
|
UserID int64 `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 []int64
|
|
|
|
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
|
|
}
|
|
jsonData, err := json.Marshal(cm)
|
|
return string(jsonData), err
|
|
}
|
|
|
|
func (ChatMembers) GormDataType() string {
|
|
return "json"
|
|
}
|