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)
|
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) {
|
func getBooksIter(coll *mgo.Collection, query string) Iter {
|
||||||
sort := []string{"$textScore:score"}
|
q := getBookQuery(coll, buildQuery(query))
|
||||||
if _, present := query["bad_quality"]; present {
|
return q.Iter()
|
||||||
sort = append(sort, "-bad_quality")
|
}
|
||||||
}
|
|
||||||
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()
|
num, err = q.Count()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
|
@ -121,6 +120,16 @@ func _getBooks(coll *mgo.Collection, query bson.M, length int, start int) (books
|
||||||
return
|
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) {
|
func getBookId(coll *mgo.Collection, id string) (Book, error) {
|
||||||
var book Book
|
var book Book
|
||||||
err := coll.Find(bson.M{"id": id}).One(&book)
|
err := coll.Find(bson.M{"id": id}).One(&book)
|
||||||
|
|
|
@ -21,6 +21,11 @@ type DB struct {
|
||||||
name string
|
name string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type Iter interface {
|
||||||
|
Close() error
|
||||||
|
Next(interface{}) bool
|
||||||
|
}
|
||||||
|
|
||||||
func Init(host string, name string) *DB {
|
func Init(host string, name string) *DB {
|
||||||
var err error
|
var err error
|
||||||
db := new(DB)
|
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)
|
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) {
|
func (db *DB) GetNewBooks(length int, start int) (books []Book, num int, err error) {
|
||||||
booksColl := db.session.DB(db.name).C(books_coll)
|
booksColl := db.session.DB(db.name).C(books_coll)
|
||||||
return getNewBooks(booksColl, length, start)
|
return getNewBooks(booksColl, length, start)
|
||||||
|
|
Reference in a new issue