models, graph2: Added Comment type and queries, and hooked it up to chapter.
the build was successful
the build was successful
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
package comments
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/binary"
|
||||
"strconv"
|
||||
|
||||
"git.aiterp.net/rpdata/api/internal/store"
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo"
|
||||
)
|
||||
|
||||
var collection *mgo.Collection
|
||||
|
||||
func find(query interface{}) (models.Comment, error) {
|
||||
comment := models.Comment{}
|
||||
err := collection.Find(query).One(&comment)
|
||||
|
||||
return comment, err
|
||||
}
|
||||
|
||||
func list(query interface{}, limit int) ([]models.Comment, error) {
|
||||
allocSize := 32
|
||||
if limit >= 0 {
|
||||
allocSize = limit
|
||||
} else {
|
||||
limit = 0
|
||||
}
|
||||
|
||||
comments := make([]models.Comment, 0, allocSize)
|
||||
err := collection.Find(query).Sort("createdDate").Limit(limit).All(&comments)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return comments, nil
|
||||
}
|
||||
|
||||
func makeCommentID() string {
|
||||
result := "SCC"
|
||||
offset := 0
|
||||
data := make([]byte, 48)
|
||||
|
||||
rand.Read(data)
|
||||
for len(result) < 32 {
|
||||
result += strconv.FormatUint(binary.LittleEndian.Uint64(data[offset:]), 36)
|
||||
offset += 8
|
||||
|
||||
if offset >= 48 {
|
||||
rand.Read(data)
|
||||
offset = 0
|
||||
}
|
||||
}
|
||||
|
||||
return result[:32]
|
||||
}
|
||||
|
||||
func init() {
|
||||
store.HandleInit(func(db *mgo.Database) {
|
||||
collection = db.C("story.comments")
|
||||
|
||||
collection.EnsureIndexKey("chapterId")
|
||||
collection.EnsureIndexKey("author")
|
||||
collection.EnsureIndexKey("createdDate")
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package comments
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// Find finds a comment by ID.
|
||||
func Find(id string) (models.Comment, error) {
|
||||
return find(bson.M{"_id": id})
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package comments
|
||||
|
||||
import (
|
||||
"git.aiterp.net/rpdata/api/models"
|
||||
"github.com/globalsign/mgo/bson"
|
||||
)
|
||||
|
||||
// ListChapterID lists all comments by chapter-ID
|
||||
func ListChapterID(chapterID string, limit int) ([]models.Comment, error) {
|
||||
return list(bson.M{"chapterId": chapterID}, limit)
|
||||
}
|
||||
Reference in New Issue
Block a user