67 lines
1.5 KiB
Go
Raw Normal View History

2019-06-03 09:32:43 +00:00
package arn
import (
"errors"
"reflect"
)
// DraftIndex has references to unpublished drafts a user created.
type DraftIndex struct {
2019-11-18 06:13:51 +00:00
UserID UserID `json:"userId" primary:"true"`
GroupID GroupID `json:"groupId"`
SoundTrackID ID `json:"soundTrackId"`
CompanyID CompanyID `json:"companyId"`
QuoteID QuoteID `json:"quoteId"`
CharacterID CharacterID `json:"characterId"`
AnimeID AnimeID `json:"animeId"`
AMVID ID `json:"amvId"`
2019-06-03 09:32:43 +00:00
}
// NewDraftIndex ...
func NewDraftIndex(userID UserID) *DraftIndex {
return &DraftIndex{
UserID: userID,
}
}
2019-09-08 23:46:43 +00:00
// DraftID gets the ID for the given type name.
func (index *DraftIndex) DraftID(typeName string) (string, error) {
2019-06-03 09:32:43 +00:00
v := reflect.ValueOf(index).Elem()
fieldValue := v.FieldByName(typeName + "ID")
if !fieldValue.IsValid() {
return "", errors.New("Invalid draft index ID type: " + typeName)
}
return fieldValue.String(), nil
}
2019-09-08 23:46:43 +00:00
// SetDraftID sets the ID for the given type name.
func (index *DraftIndex) SetDraftID(typeName string, id string) error {
2019-06-03 09:32:43 +00:00
v := reflect.ValueOf(index).Elem()
fieldValue := v.FieldByName(typeName + "ID")
if !fieldValue.IsValid() {
return errors.New("Invalid draft index ID type: " + typeName)
}
fieldValue.SetString(id)
return nil
}
2019-09-08 23:46:43 +00:00
// GetID returns the ID.
func (index *DraftIndex) GetID() string {
return index.UserID
}
2019-06-03 09:32:43 +00:00
// GetDraftIndex ...
2019-11-18 06:13:51 +00:00
func GetDraftIndex(id ID) (*DraftIndex, error) {
2019-06-03 09:32:43 +00:00
obj, err := DB.Get("DraftIndex", id)
if err != nil {
return nil, err
}
return obj.(*DraftIndex), nil
}