From 8e82ee3702784204bf023fd0bca16dbe3c0485fc Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Sat, 4 Feb 2017 01:28:10 +0000 Subject: [PATCH] Add search terms to the moderation page * Closes: #22 --- lib/admin.go | 17 ++++++++++------- lib/database/books.go | 12 ++++++++---- lib/database/books_test.go | 4 ++-- lib/database/database.go | 4 ++-- templates/new.html | 4 ++++ 5 files changed, 26 insertions(+), 15 deletions(-) diff --git a/lib/admin.go b/lib/admin.go index 23bc414..03bd7e3 100644 --- a/lib/admin.go +++ b/lib/admin.go @@ -127,12 +127,13 @@ type newBook struct { B database.Book } type newData struct { - S Status - Found int - Books []newBook - Page int - Next string - Prev string + S Status + Found int + Books []newBook + Page int + Next string + Prev string + Search string } func newHandler(h handler) { @@ -146,6 +147,7 @@ func newHandler(h handler) { http.Error(h.w, err.Error(), http.StatusInternalServerError) return } + req := strings.Join(h.r.Form["q"], " ") page := 0 if len(h.r.Form["p"]) != 0 { page, err = strconv.Atoi(h.r.Form["p"][0]) @@ -153,7 +155,7 @@ func newHandler(h handler) { page = 0 } } - res, num, _ := h.db.GetNewBooks(newItemsPage, page*newItemsPage) + res, num, _ := h.db.GetNewBooks(req, newItemsPage, page*newItemsPage) var data newData data.S = GetStatus(h) @@ -176,6 +178,7 @@ func newHandler(h handler) { if page > 0 { data.Prev = "/new/?p=" + strconv.Itoa(page-1) } + data.Search = req h.template.load(h, "new", data) } diff --git a/lib/database/books.go b/lib/database/books.go index 7441f04..1f845a4 100644 --- a/lib/database/books.go +++ b/lib/database/books.go @@ -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) diff --git a/lib/database/books_test.go b/lib/database/books_test.go index 9741952..2c8bc81 100644 --- a/lib/database/books_test.go +++ b/lib/database/books_test.go @@ -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) diff --git a/lib/database/database.go b/lib/database/database.go index 056cd86..d0698e0 100644 --- a/lib/database/database.go +++ b/lib/database/database.go @@ -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) { diff --git a/templates/new.html b/templates/new.html index 50c0a29..853df2a 100644 --- a/templates/new.html +++ b/templates/new.html @@ -1,5 +1,9 @@ {{template "header.html" .S}} +
+ +
+ {{if .Books}}
Save All