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.
33 lines
826 B
33 lines
826 B
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
|
|
}
|