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 }