parent
fcf9b1eb8d
commit
8e82ee3702
5 changed files with 26 additions and 15 deletions
17
lib/admin.go
17
lib/admin.go
|
@ -127,12 +127,13 @@ type newBook struct {
|
||||||
B database.Book
|
B database.Book
|
||||||
}
|
}
|
||||||
type newData struct {
|
type newData struct {
|
||||||
S Status
|
S Status
|
||||||
Found int
|
Found int
|
||||||
Books []newBook
|
Books []newBook
|
||||||
Page int
|
Page int
|
||||||
Next string
|
Next string
|
||||||
Prev string
|
Prev string
|
||||||
|
Search string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newHandler(h handler) {
|
func newHandler(h handler) {
|
||||||
|
@ -146,6 +147,7 @@ func newHandler(h handler) {
|
||||||
http.Error(h.w, err.Error(), http.StatusInternalServerError)
|
http.Error(h.w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
req := strings.Join(h.r.Form["q"], " ")
|
||||||
page := 0
|
page := 0
|
||||||
if len(h.r.Form["p"]) != 0 {
|
if len(h.r.Form["p"]) != 0 {
|
||||||
page, err = strconv.Atoi(h.r.Form["p"][0])
|
page, err = strconv.Atoi(h.r.Form["p"][0])
|
||||||
|
@ -153,7 +155,7 @@ func newHandler(h handler) {
|
||||||
page = 0
|
page = 0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res, num, _ := h.db.GetNewBooks(newItemsPage, page*newItemsPage)
|
res, num, _ := h.db.GetNewBooks(req, newItemsPage, page*newItemsPage)
|
||||||
|
|
||||||
var data newData
|
var data newData
|
||||||
data.S = GetStatus(h)
|
data.S = GetStatus(h)
|
||||||
|
@ -176,6 +178,7 @@ func newHandler(h handler) {
|
||||||
if page > 0 {
|
if page > 0 {
|
||||||
data.Prev = "/new/?p=" + strconv.Itoa(page-1)
|
data.Prev = "/new/?p=" + strconv.Itoa(page-1)
|
||||||
}
|
}
|
||||||
|
data.Search = req
|
||||||
h.template.load(h, "new", data)
|
h.template.load(h, "new", data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
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) {
|
func getNewBooks(coll *mgo.Collection, query string, length int, start int) (books []Book, num int, err error) {
|
||||||
return _getBooks(coll, bson.M{"$nor": []bson.M{{"active": true}}}, length, start)
|
q := buildQuery(query)
|
||||||
|
q["$nor"] = []bson.M{{"active": true}}
|
||||||
|
return _getBooks(coll, q, length, start)
|
||||||
}
|
}
|
||||||
|
|
||||||
func getBooksIter(coll *mgo.Collection) Iter {
|
func getBooksIter(coll *mgo.Collection) Iter {
|
||||||
|
@ -193,7 +197,7 @@ func isBookActive(coll *mgo.Collection, id string) bool {
|
||||||
|
|
||||||
func buildQuery(q string) bson.M {
|
func buildQuery(q string) bson.M {
|
||||||
text := ""
|
text := ""
|
||||||
query := bson.M{"active": true}
|
query := bson.M{}
|
||||||
words := strings.Split(q, " ")
|
words := strings.Split(q, " ")
|
||||||
for _, w := range words {
|
for _, w := range words {
|
||||||
tag := strings.SplitN(w, ":", 2)
|
tag := strings.SplitN(w, ":", 2)
|
||||||
|
|
|
@ -14,7 +14,7 @@ func TestAddBook(t *testing.T) {
|
||||||
|
|
||||||
tAddBook(t, db)
|
tAddBook(t, db)
|
||||||
|
|
||||||
books, num, err := db.GetNewBooks(1, 0)
|
books, num, err := db.GetNewBooks("", 1, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal("db.GetBooks() return an error: ", err)
|
t.Fatal("db.GetBooks() return an error: ", err)
|
||||||
}
|
}
|
||||||
|
@ -34,7 +34,7 @@ func TestActiveBook(t *testing.T) {
|
||||||
defer db.del()
|
defer db.del()
|
||||||
|
|
||||||
tAddBook(t, db)
|
tAddBook(t, db)
|
||||||
books, _, _ := db.GetNewBooks(1, 0)
|
books, _, _ := db.GetNewBooks("", 1, 0)
|
||||||
id := books[0].Id
|
id := books[0].Id
|
||||||
|
|
||||||
err := db.ActiveBook(id)
|
err := db.ActiveBook(id)
|
||||||
|
|
|
@ -75,9 +75,9 @@ func (db *DB) GetBooksIter() Iter {
|
||||||
return getBooksIter(booksColl)
|
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)
|
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) {
|
func (db *DB) GetBookId(id string) (Book, error) {
|
||||||
|
|
|
@ -1,5 +1,9 @@
|
||||||
{{template "header.html" .S}}
|
{{template "header.html" .S}}
|
||||||
|
|
||||||
|
<form class="centered" action="/new/">
|
||||||
|
<input type="search" class="search-query span8" name="q" {{if .Search}}value="{{.Search}}"{{else}}placeholder="Search"{{end}} />
|
||||||
|
</form>
|
||||||
|
|
||||||
{{if .Books}}
|
{{if .Books}}
|
||||||
<div class="centered btn-group">
|
<div class="centered btn-group">
|
||||||
<a href="/store/{{range .Books}}{{.B.Id}}/{{end}}" class="btn btn-large btn-success"><i class="icon-ok"></i> Save All</a>
|
<a href="/store/{{range .Books}}{{.B.Id}}/{{end}}" class="btn btn-large btn-success"><i class="icon-ok"></i> Save All</a>
|
||||||
|
|
Reference in a new issue