package changes import ( "strconv" "time" "git.aiterp.net/rpdata/api/internal/counter" "git.aiterp.net/rpdata/api/models" ) // Submit a change to the database func Submit(model, op, author string, 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, } for _, object := range objects { switch object := object.(type) { case models.Log: change.Logs = append(change.Logs, object) case models.Character: change.Characters = append(change.Characters, object) case models.Post: change.Posts = append(change.Posts, object) } } // TODO: Push to subscribers err = collection.Insert(&change) if err != nil { return models.Change{}, err } return change, nil }