package posts import ( "git.aiterp.net/rpdata/api/internal/counter" "git.aiterp.net/rpdata/api/models" "github.com/globalsign/mgo/bson" ) // Remove removes a post, moving all subsequent post up one position func Remove(post models.Post) (models.Post, error) { mutex.Lock() defer mutex.Unlock() err := collection.RemoveId(post.ID) if err != nil { return models.Post{}, err } _, err = collection.UpdateAll(bson.M{"logId": post.LogID, "position": bson.M{"$gt": post.Position}}, bson.M{"$inc": bson.M{"position": -1}}) if err != nil { return models.Post{}, err } counter.NextMany("next_post_id", post.LogID, -1) return post, nil } // RemoveAllInLog removes all posts for the given log. func RemoveAllInLog(log models.Log) error { _, err := collection.RemoveAll(bson.M{"logId": log.ShortID}) return err }