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.

36 lines
791 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. copies := make([]models.Post, len(posts))
  10. mutex.RLock()
  11. defer mutex.RUnlock()
  12. startPosition, err := counter.NextMany("next_post_id", log.ShortID, len(posts))
  13. if err != nil {
  14. return nil, err
  15. }
  16. for i, post := range posts {
  17. post.ID = generateID(post.Time)
  18. post.LogID = log.ShortID
  19. post.Position = startPosition + i
  20. docs[i] = post
  21. copies[i] = post
  22. }
  23. err = collection.Insert(docs...)
  24. if err != nil {
  25. return nil, err
  26. }
  27. return copies, nil
  28. }