2024-05-07 21:49:09 +03:00
|
|
|
package model
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
"gorm.io/datatypes"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Integration struct {
|
|
|
|
ID uint64 `gorm:"primaryKey; autoIncrement;"`
|
|
|
|
Type IntegrationType `gorm:"column:type; not null"`
|
2024-05-09 17:42:42 +03:00
|
|
|
ChatID int64 `gorm:"column:chat_id; not null"`
|
2024-05-07 21:49:09 +03:00
|
|
|
Params datatypes.JSONMap `gorm:"column:params; not null"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (Integration) TableName() string {
|
|
|
|
return "integration"
|
|
|
|
}
|
|
|
|
|
|
|
|
type IntegrationType uint8
|
|
|
|
|
|
|
|
const (
|
|
|
|
InvalidIntegration IntegrationType = iota
|
|
|
|
RedmineIntegration
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
integrationTypesToIds = map[string]IntegrationType{
|
|
|
|
"invalid": InvalidIntegration,
|
|
|
|
"redmine": RedmineIntegration,
|
|
|
|
}
|
|
|
|
idsToIntegrationTypes = map[IntegrationType]string{
|
|
|
|
InvalidIntegration: "invalid",
|
|
|
|
RedmineIntegration: "redmine",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (it *IntegrationType) Scan(value interface{}) error {
|
|
|
|
switch v := value.(type) {
|
|
|
|
case nil:
|
|
|
|
*it = 0 // or whatever default you want to set if the field is null in db
|
|
|
|
return nil
|
|
|
|
case IntegrationType:
|
|
|
|
*it = v
|
|
|
|
case string:
|
|
|
|
// lookup the value from the map and assign it
|
|
|
|
val, ok := integrationTypesToIds[v]
|
|
|
|
if !ok {
|
|
|
|
return errors.New("invalid IntegrationType")
|
|
|
|
}
|
|
|
|
*it = val
|
|
|
|
default:
|
|
|
|
return errors.New("invalid type")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (it IntegrationType) Value() (driver.Value, error) {
|
|
|
|
val, ok := idsToIntegrationTypes[it]
|
|
|
|
if ok {
|
|
|
|
return val, nil
|
|
|
|
}
|
2024-05-09 17:42:42 +03:00
|
|
|
return InvalidIntegration, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (IntegrationType) GormDataType() string {
|
|
|
|
return "varchar(24)"
|
2024-05-07 21:49:09 +03:00
|
|
|
}
|