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

  1. package posts
  2. import (
  3. "git.aiterp.net/rpdata/api/internal/counter"
  4. "git.aiterp.net/rpdata/api/models"
  5. )
  6. // AddMany adds multiple posts in on query. Each post gets a new ID and is associated with the log.
  7. func AddMany(log models.Log, posts []models.Post) ([]models.Post, error) {
  8. docs := make([]interface{}, len(posts))
  9. mutex.RLock()
  10. defer mutex.RUnlock()
  11. startPosition, err := counter.NextMany("next_post_id", log.ShortID, len(posts))
  12. if err != nil {
  13. return nil, err
  14. }
  15. for i, post := range posts {
  16. post.ID = generateID(post.Time)
  17. post.LogID = log.ShortID
  18. post.Position = startPosition + i
  19. docs[i] = post
  20. }
  21. err = collection.Insert(docs...)
  22. if err != nil {
  23. return nil, err
  24. }
  25. return posts, nil
  26. }