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.
 
 

55 lines
1.3 KiB

package logs
import (
"errors"
"strconv"
"time"
"git.aiterp.net/rpdata/api/internal/counter"
"git.aiterp.net/rpdata/api/model/channel"
"git.aiterp.net/rpdata/api/models"
"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 = channel.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
}