package posts import ( "time" "git.aiterp.net/rpdata/api/models" "github.com/globalsign/mgo/bson" ) // Edit edits a post and returns the result if the edit succeeded. func Edit(post models.Post, time *time.Time, kind *string, nick *string, text *string) (models.Post, error) { mutex.RLock() defer mutex.RUnlock() changes := bson.M{} if time != nil && !time.IsZero() && !time.Equal(post.Time) { changes["time"] = *time post.Time = *time } if kind != nil && *kind != "" && *kind != post.Kind { changes["kind"] = *kind post.Kind = *kind } if nick != nil && *nick != "" && *nick != post.Nick { changes["nick"] = *nick post.Nick = *nick } if text != nil && *text != "" && *text != post.Text { changes["text"] = *text post.Text = *text } if len(changes) == 0 { return post, nil } err := collection.UpdateId(post.ID, bson.M{"$set": changes}) if err != nil { return models.Post{}, err } return post, nil }