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.
 
 

50 lines
1.1 KiB

package logs
import (
"git.aiterp.net/rpdata/api/models"
"github.com/globalsign/mgo/bson"
)
// Edit sets a log's meta-data.
func Edit(log models.Log, title *string, event *string, description *string, open *bool) (models.Log, error) {
changes := bson.M{}
if title != nil && *title != log.Title {
changes["title"] = *title
log.Title = *title
}
if event != nil && *event != log.EventName {
changes["event"] = *event
log.EventName = *event
}
if description != nil && *description != log.Description {
changes["description"] = *description
log.Description = *description
}
if open != nil && *open != log.Open {
changes["open"] = *open
log.Open = *open
}
if len(changes) == 0 {
return log, nil
}
err := collection.UpdateId(log.ID, bson.M{"$set": changes})
if err != nil {
return models.Log{}, err
}
// There can be only one open log. TODO: Transaction
if changes["open"] != nil && *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
}