vegapokerbot/internal/db/model/chat.go

69 lines
1.4 KiB
Go
Raw Permalink Normal View History

2024-05-07 21:49:09 +03:00
package model
import (
"database/sql/driver"
"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"`
KeyboardType KeyboardType `gorm:"column:keyboard_type;"`
UserID uint64 `gorm:"column:user_id; not null"`
2024-05-07 21:49:09 +03:00
Integrations []Integration `gorm:"foreignKey:ChatID" json:"integrations"`
}
func (Chat) TableName() string {
return "chat"
}
type KeyboardType uint8
2024-05-07 21:49:09 +03:00
const (
StandardKeyboard KeyboardType = iota
StoryPointsKeyboard
)
var (
lbTypesToIds = map[string]KeyboardType{
"normal": StandardKeyboard,
"sp": StoryPointsKeyboard,
}
idsToKBTypes = map[KeyboardType]string{
StandardKeyboard: "normal",
StoryPointsKeyboard: "sp",
}
)
func (it *KeyboardType) Scan(value interface{}) error {
2024-05-07 21:49:09 +03:00
switch v := value.(type) {
case nil:
*it = 0 // or whatever default you want to set if the field is null in db
2024-05-07 21:49:09 +03:00
return nil
case KeyboardType:
*it = v
2024-05-07 21:49:09 +03:00
case string:
// lookup the value from the map and assign it
val, ok := lbTypesToIds[v]
if !ok {
return errors.New("invalid KeyboardType")
2024-05-07 21:49:09 +03:00
}
*it = val
2024-05-07 21:49:09 +03:00
default:
return errors.New("invalid type")
}
return nil
}
func (it KeyboardType) Value() (driver.Value, error) {
val, ok := idsToKBTypes[it]
if ok {
return val, nil
2024-05-07 21:49:09 +03:00
}
return InvalidIntegration, nil
2024-05-07 21:49:09 +03:00
}
2024-05-09 17:42:42 +03:00
func (KeyboardType) GormDataType() string {
return "varchar(6)"
2024-05-09 17:42:42 +03:00
}