package changes import ( "strconv" "time" "git.aiterp.net/rpdata/api/internal/counter" "git.aiterp.net/rpdata/api/models" ) // Submit a change to the database. The objects may be any supported model, or arrays. func Submit(model, op, author string, listed bool, keys []models.ChangeKey, objects ...interface{}) (models.Change, error) { submitMutex.Lock() defer submitMutex.Unlock() id, err := counter.Next("auto_increment", "Change") if err != nil { return models.Change{}, err } change := models.Change{ ID: "Change_" + strconv.Itoa(id), Model: model, Date: time.Now(), Op: op, Author: author, Keys: keys, Listed: listed, } for _, object := range objects { switch object := object.(type) { case models.Log: change.Logs = append(change.Logs, object) case []models.Log: change.Logs = append(change.Logs, object...) case models.Character: change.Characters = append(change.Characters, object) case []models.Character: change.Characters = append(change.Characters, object...) case models.Channel: change.Channels = append(change.Channels, object) case []models.Channel: change.Channels = append(change.Channels, object...) case models.Post: change.Posts = append(change.Posts, object) case []models.Post: change.Posts = append(change.Posts, object...) case models.Story: change.Stories = append(change.Stories, object) case []models.Story: change.Stories = append(change.Stories, object...) case models.Tag: change.Tags = append(change.Tags, object) case []models.Tag: change.Tags = append(change.Tags, object...) case models.Chapter: change.Chapters = append(change.Chapters, object) case []models.Chapter: change.Chapters = append(change.Chapters, object...) } } // TODO: Push to subscribers err = collection.Insert(&change) if err != nil { return models.Change{}, err } return change, nil }