Something went wrong. Try again.
This repository has no description
Something went wrong. Try again.
2.2 kB · 66 lines
Go
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667package model
import ( "context" "errors" "fmt" "time"
glex "github.com/streamplace/glex/runtime" "gorm.io/gorm" "stream.place/streamplace/pkg/placestream")
// ServerSettings represents a user's settings for a particular Streamplace nodetype ServerSettings struct { Server string `gorm:"primaryKey;column:server"` RepoDID string `gorm:"primaryKey;column:repo_did"` Record *[]byte `gorm:"column:record"` Created time.Time `gorm:"column:created;not null"` Updated time.Time `gorm:"column:updated;not null"`}
// TableName specifies the table name for the ServerSettings modelfunc (ServerSettings) TableName() string { return "server_settings"}
// ToStreamplaceServerSettings converts the model to a streamplace ServerSettingsfunc (m *ServerSettings) ToStreamplaceServerSettings() (placestream.ServerSettings, error) { if m.Record == nil { return placestream.ServerSettings{}, fmt.Errorf("no record data") } var ss placestream.ServerSettings if err := glex.DecodeCBOR(*m.Record, &ss); err != nil { return placestream.ServerSettings{}, fmt.Errorf("error decoding server settings: %w", err) } return ss, nil}
// UpdateServerSettings creates or updates a server settings recordfunc (m *DBModel) UpdateServerSettings(ctx context.Context, settings *ServerSettings) error { now := time.Now() if settings.Created.IsZero() { settings.Created = now } settings.Updated = now return m.DB.Save(settings).Error}
// GetServerSettings retrieves server settings for a given server and repoDIDfunc (m *DBModel) GetServerSettings(ctx context.Context, server string, repoDID string) (*ServerSettings, error) { var settings ServerSettings err := m.DB.Where("server = ? AND repo_did = ?", server, repoDID).First(&settings).Error if errors.Is(err, gorm.ErrRecordNotFound) { return nil, nil } if err != nil { return nil, err } return &settings, nil}
// DeleteServerSettings deletes server settings for a given server and repoDIDfunc (m *DBModel) DeleteServerSettings(ctx context.Context, server string, repoDID string) error { return m.DB.Where("server = ? AND repo_did = ?", server, repoDID).Delete(&ServerSettings{}).Error}