|
|
package logs
import ( "errors" "strconv" "time"
"git.aiterp.net/rpdata/api/internal/counter" "git.aiterp.net/rpdata/api/models" "git.aiterp.net/rpdata/api/models/channels" "github.com/globalsign/mgo/bson" )
// Add creates a new Log
func Add(date time.Time, channelName, title, eventName, description string, open bool) (models.Log, error) { nextID, err := counter.Next("auto_increment", "Log") if err != nil { return models.Log{}, errors.New("Failed to allocate short ID: " + err.Error()) }
_, err = channels.Ensure(channelName, open) if err != nil { return models.Log{}, errors.New("Failed to ensure channel: " + err.Error()) }
log := models.Log{ ID: makeLogID(date, channelName), ShortID: "L" + strconv.Itoa(nextID), Date: date, ChannelName: channelName, Title: title, EventName: eventName, Description: description, Open: open, CharacterIDs: nil, }
err = collection.Insert(log) if err != nil { return models.Log{}, err }
// There can be only one open log in the same channel. TODO: Transaction
if open { query := bson.M{ "_id": bson.M{"$ne": log.ID}, "open": true, "channel": log.ChannelName, }
go collection.UpdateAll(query, bson.M{"$set": bson.M{"open": false}}) }
return log, nil }
|