parent
fcf9b1eb8d
commit
8e82ee3702
5 changed files with 26 additions and 15 deletions
|
@ -91,11 +91,15 @@ func addBook(coll *mgo.Collection, book map[string]interface{}) error {
|
|||
}
|
||||
|
||||
func getBooks(coll *mgo.Collection, query string, length int, start int) (books []Book, num int, err error) {
|
||||
return _getBooks(coll, buildQuery(query), length, start)
|
||||
q := buildQuery(query)
|
||||
q["active"] = true
|
||||
return _getBooks(coll, q, length, start)
|
||||
}
|
||||
|
||||
func getNewBooks(coll *mgo.Collection, length int, start int) (books []Book, num int, err error) {
|
||||
return _getBooks(coll, bson.M{"$nor": []bson.M{{"active": true}}}, length, start)
|
||||
func getNewBooks(coll *mgo.Collection, query string, length int, start int) (books []Book, num int, err error) {
|
||||
q := buildQuery(query)
|
||||
q["$nor"] = []bson.M{{"active": true}}
|
||||
return _getBooks(coll, q, length, start)
|
||||
}
|
||||
|
||||
func getBooksIter(coll *mgo.Collection) Iter {
|
||||
|
@ -193,7 +197,7 @@ func isBookActive(coll *mgo.Collection, id string) bool {
|
|||
|
||||
func buildQuery(q string) bson.M {
|
||||
text := ""
|
||||
query := bson.M{"active": true}
|
||||
query := bson.M{}
|
||||
words := strings.Split(q, " ")
|
||||
for _, w := range words {
|
||||
tag := strings.SplitN(w, ":", 2)
|
||||
|
|
|
@ -14,7 +14,7 @@ func TestAddBook(t *testing.T) {
|
|||
|
||||
tAddBook(t, db)
|
||||
|
||||
books, num, err := db.GetNewBooks(1, 0)
|
||||
books, num, err := db.GetNewBooks("", 1, 0)
|
||||
if err != nil {
|
||||
t.Fatal("db.GetBooks() return an error: ", err)
|
||||
}
|
||||
|
@ -34,7 +34,7 @@ func TestActiveBook(t *testing.T) {
|
|||
defer db.del()
|
||||
|
||||
tAddBook(t, db)
|
||||
books, _, _ := db.GetNewBooks(1, 0)
|
||||
books, _, _ := db.GetNewBooks("", 1, 0)
|
||||
id := books[0].Id
|
||||
|
||||
err := db.ActiveBook(id)
|
||||
|
|
|
@ -75,9 +75,9 @@ func (db *DB) GetBooksIter() Iter {
|
|||
return getBooksIter(booksColl)
|
||||
}
|
||||
|
||||
func (db *DB) GetNewBooks(length int, start int) (books []Book, num int, err error) {
|
||||
func (db *DB) GetNewBooks(query string, length int, start int) (books []Book, num int, err error) {
|
||||
booksColl := db.session.DB(db.name).C(books_coll)
|
||||
return getNewBooks(booksColl, length, start)
|
||||
return getNewBooks(booksColl, query, length, start)
|
||||
}
|
||||
|
||||
func (db *DB) GetBookId(id string) (Book, error) {
|
||||
|
|
Reference in a new issue