add change table.

This commit is contained in:
2021-03-22 21:45:12 +01:00
parent 0bc86ced98
commit 0f06a3750f
8 changed files with 427 additions and 11 deletions
+38 -10
View File
@@ -1,7 +1,9 @@
package models
import (
"fmt"
"reflect"
"strings"
"time"
)
@@ -15,19 +17,23 @@ type Change struct {
Keys []ChangeKey `bson:"keys"`
Date time.Time `bson:"date"`
Logs []*Log `bson:"logs"`
Characters []*Character `bson:"characters"`
Channels []*Channel `bson:"channels"`
Posts []*Post `bson:"posts"`
Stories []*Story `bson:"stories"`
Tags []*Tag `bson:"tags"`
Chapters []*Chapter `bson:"chapters"`
Comments []*Comment `bson:"comments"`
Files []*File `bson:"files"`
ChangeObjectSet
}
type ChangeObjectSet struct {
Logs []*Log `bson:"logs" json:"logs,omitempty"`
Characters []*Character `bson:"characters" json:"characters,omitempty"`
Channels []*Channel `bson:"channels" json:"channels,omitempty"`
Posts []*Post `bson:"posts" json:"posts,omitempty"`
Stories []*Story `bson:"stories" json:"stories,omitempty"`
Tags []*Tag `bson:"tags" json:"tags,omitempty"`
Chapters []*Chapter `bson:"chapters" json:"chapters,omitempty"`
Comments []*Comment `bson:"comments" json:"comments,omitempty"`
Files []*File `bson:"files" json:"files,omitempty"`
}
// AddObject adds the model into the appropriate array.
func (change *Change) AddObject(object interface{}) bool {
func (change *ChangeObjectSet) AddObject(object interface{}) bool {
if v := reflect.ValueOf(object); v.Kind() == reflect.Struct {
ptr := reflect.PtrTo(v.Type())
ptrValue := reflect.New(ptr.Elem())
@@ -174,10 +180,32 @@ type ChangeKey struct {
ID string `bson:"id"`
}
func (ck *ChangeKey) String() string {
return ck.Model.String() + ":" + ck.ID
}
func (ck *ChangeKey) Decode(str string) error {
split := strings.Split(str, ":")
if len(split) != 2 {
return fmt.Errorf("invalid change key: %s", str)
}
model := ChangeModel(split[0])
if !model.IsValid() {
return fmt.Errorf("invalid change key model: %s", model)
}
ck.Model = model
ck.ID = split[1]
return nil
}
// ChangeFilter is a filter for listing changes.
type ChangeFilter struct {
Keys []ChangeKey
EarliestDate *time.Time
LatestDate *time.Time
Author *string
Limit *int
}