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.
 
 

34 lines
728 B

package posts
import (
"git.aiterp.net/rpdata/api/internal/counter"
"git.aiterp.net/rpdata/api/models"
)
// AddMany adds multiple posts in on query. Each post gets a new ID and is associated with the log.
func AddMany(log models.Log, posts []models.Post) ([]models.Post, error) {
docs := make([]interface{}, len(posts))
mutex.RLock()
defer mutex.RUnlock()
startPosition, err := counter.NextMany("next_post_id", log.ShortID, len(posts))
if err != nil {
return nil, err
}
for i, post := range posts {
post.ID = generateID(post.Time)
post.LogID = log.ShortID
post.Position = startPosition + i
docs[i] = post
}
err = collection.Insert(docs...)
if err != nil {
return nil, err
}
return posts, nil
}