daddy/internal/model/dsf.go

63 lines
1.4 KiB
Go

package model
import (
"encoding/json"
"time"
"github.com/jinzhu/gorm"
"github.com/jinzhu/gorm/dialects/postgres"
errs "github.com/pkg/errors"
)
const ObjectTypeDecisionSupportFile = "dsf"
const (
StatusDraft = "draft"
StatusReady = "ready"
StatusVoted = "voted"
StatusClosed = "closed"
)
type DecisionSupportFile struct {
gorm.Model
Title string `json:"title"`
SectionsJSON postgres.Jsonb `json:"-" gorm:"column:sections;"`
Sections map[string]interface{} `gorm:"-"`
Status string `json:"status"`
WorkgroupID uint `json:"-"`
Workgroup *Workgroup `json:"workgroup" gorm:"association_autoupdate:false"`
VotedAt time.Time `json:"votedAt"`
ClosedAt time.Time `json:"closedAt"`
}
func (f *DecisionSupportFile) ObjectID() uint {
return f.ID
}
func (f *DecisionSupportFile) ObjectType() string {
return ObjectTypeDecisionSupportFile
}
func (f *DecisionSupportFile) BeforeSave() error {
rawSections, err := json.Marshal(f.Sections)
if err != nil {
return errs.WithStack(err)
}
f.SectionsJSON = postgres.Jsonb{RawMessage: rawSections}
return nil
}
func (f *DecisionSupportFile) AfterFind() (err error) {
sections := make(map[string]interface{})
if err := json.Unmarshal(f.SectionsJSON.RawMessage, &sections); err != nil {
return errs.WithStack(err)
}
f.Sections = sections
return nil
}