Add books Iter
This commit is contained in:
parent
2f2ff3dd8f
commit
4e9e0fb2a5
2 changed files with 26 additions and 7 deletions
|
@ -98,14 +98,13 @@ func getNewBooks(coll *mgo.Collection, length int, start int) (books []Book, num
|
|||
return _getBooks(coll, bson.M{"$nor": []bson.M{{"active": true}}}, length, start)
|
||||
}
|
||||
|
||||
func _getBooks(coll *mgo.Collection, query bson.M, length int, start int) (books []Book, num int, err error) {
|
||||
sort := []string{"$textScore:score"}
|
||||
if _, present := query["bad_quality"]; present {
|
||||
sort = append(sort, "-bad_quality")
|
||||
func getBooksIter(coll *mgo.Collection, query string) Iter {
|
||||
q := getBookQuery(coll, buildQuery(query))
|
||||
return q.Iter()
|
||||
}
|
||||
sort = append(sort, "-_id")
|
||||
|
||||
q := coll.Find(query).Select(bson.M{"score": bson.M{"$meta": "textScore"}}).Sort(sort...)
|
||||
func _getBooks(coll *mgo.Collection, query bson.M, length int, start int) (books []Book, num int, err error) {
|
||||
q := getBookQuery(coll, query)
|
||||
num, err = q.Count()
|
||||
if err != nil {
|
||||
return
|
||||
|
@ -121,6 +120,16 @@ func _getBooks(coll *mgo.Collection, query bson.M, length int, start int) (books
|
|||
return
|
||||
}
|
||||
|
||||
func getBookQuery(coll *mgo.Collection, query bson.M) *mgo.Query {
|
||||
sort := []string{"$textScore:score"}
|
||||
if _, present := query["bad_quality"]; present {
|
||||
sort = append(sort, "-bad_quality")
|
||||
}
|
||||
sort = append(sort, "-_id")
|
||||
|
||||
return coll.Find(query).Select(bson.M{"score": bson.M{"$meta": "textScore"}}).Sort(sort...)
|
||||
}
|
||||
|
||||
func getBookId(coll *mgo.Collection, id string) (Book, error) {
|
||||
var book Book
|
||||
err := coll.Find(bson.M{"id": id}).One(&book)
|
||||
|
|
|
@ -21,6 +21,11 @@ type DB struct {
|
|||
name string
|
||||
}
|
||||
|
||||
type Iter interface {
|
||||
Close() error
|
||||
Next(interface{}) bool
|
||||
}
|
||||
|
||||
func Init(host string, name string) *DB {
|
||||
var err error
|
||||
db := new(DB)
|
||||
|
@ -65,6 +70,11 @@ func (db *DB) GetBooks(query string, length int, start int) (books []Book, num i
|
|||
return getBooks(booksColl, query, length, start)
|
||||
}
|
||||
|
||||
func (db *DB) GetBooksIter(query string) Iter {
|
||||
booksColl := db.session.DB(db.name).C(books_coll)
|
||||
return getBooksIter(booksColl, query)
|
||||
}
|
||||
|
||||
func (db *DB) GetNewBooks(length int, start int) (books []Book, num int, err error) {
|
||||
booksColl := db.session.DB(db.name).C(books_coll)
|
||||
return getNewBooks(booksColl, length, start)
|
||||
|
|
Reference in a new issue