GraphQL API and utilities for the rpdata project
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

48 lines
975 B

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
}