Add bad quality flag

This commit is contained in:
Las Zenow 2014-09-07 20:22:24 -05:00
parent 45efecbfc8
commit f5363e17bc
6 changed files with 97 additions and 10 deletions

View file

@ -36,6 +36,7 @@ type Book struct {
FileSize int
Cover bool
Active bool
BadQuality int `bad_quality`
Keywords []string
}
@ -50,6 +51,10 @@ func indexBooks(coll *mgo.Collection) {
Key: []string{"active", "-_id"},
Background: true,
},
{
Key: []string{"active", "-bad_quality", "-_id"},
Background: true,
},
}
for _, k := range []string{"keywords", "lang", "title", "author", "subject"} {
idx := mgo.Index{
@ -81,7 +86,13 @@ func getNewBooks(coll *mgo.Collection, length int, start int) (books []Book, num
}
func _getBooks(coll *mgo.Collection, query bson.M, length int, start int) (books []Book, num int, err error) {
q := coll.Find(query).Sort("-_id")
sort := []string{}
if _, present := query["bad_quality"]; present {
sort = append(sort, "-bad_quality")
}
sort = append(sort, "-_id")
q := coll.Find(query).Sort(sort...)
num, err = q.Count()
if err != nil {
return
@ -121,6 +132,10 @@ func updateBook(coll *mgo.Collection, id string, data map[string]interface{}) er
return coll.Update(bson.M{"id": id}, bson.M{"$set": data})
}
func flagBadQuality(coll *mgo.Collection, id string) error {
return coll.Update(bson.M{"id": id}, bson.M{"$inc": bson.M{"bad_quality": 1}})
}
func activeBook(coll *mgo.Collection, id string) error {
data := map[string]interface{}{"active": true}
return coll.Update(bson.M{"id": id}, bson.M{"$set": data})
@ -142,7 +157,11 @@ func buildQuery(q string) bson.M {
for _, w := range words {
tag := strings.SplitN(w, ":", 2)
if len(tag) > 1 {
query[tag[0]] = bson.RegEx{tag[1], "i"}
if tag[0] == "flag" {
query[tag[1]] = bson.M{"$gt": 0}
} else {
query[tag[0]] = bson.RegEx{tag[1], "i"} //FIXME: this should be a list
}
} else {
toks := tokens(w)
keywords = append(keywords, toks...)

View file

@ -77,9 +77,55 @@ func TestUpdateBookKeywords(t *testing.T) {
}
}
func TestFlag(t *testing.T) {
db := Init(test_host, test_coll)
defer db.del()
tAddBook(t, db)
id, _ := book["id"].(string)
db.ActiveBook(id)
id2 := "tfgrBvd2ps_K4iYt"
b2 := book
b2["id"] = id2
err := db.AddBook(b2)
if err != nil {
t.Error("db.AddBook(", book, ") return an error:", err)
}
db.ActiveBook(id2)
id3 := "tfgrBvd2ps_K4iY2"
b3 := book
b3["id"] = id3
err = db.AddBook(b3)
if err != nil {
t.Error("db.AddBook(", book, ") return an error:", err)
}
db.ActiveBook(id3)
db.FlagBadQuality(id)
db.FlagBadQuality(id)
db.FlagBadQuality(id3)
b, _ := db.GetBookId(id)
if b.BadQuality != 2 {
t.Error("The bad quality flag was not increased")
}
b, _ = db.GetBookId(id3)
if b.BadQuality != 1 {
t.Error("The bad quality flag was not increased")
}
books, _, _ := db.GetBooks("flag:bad_quality", 2, 0)
if len(books) != 2 {
t.Fatal("Not the right number of results to the flag search:", len(books))
}
if books[0].Id != id {
t.Error("Search for flag bad_quality is not sort right")
}
}
func tAddBook(t *testing.T, db *DB) {
err := db.AddBook(book)
if err != nil {
t.Error("db.AddBook(", book, ") return an error: ", err)
t.Error("db.AddBook(", book, ") return an error:", err)
}
}

View file

@ -85,6 +85,11 @@ func (db *DB) UpdateBook(id string, data map[string]interface{}) error {
return updateBook(booksColl, id, data)
}
func (db *DB) FlagBadQuality(id string) error {
booksColl := db.session.DB(db.name).C(books_coll)
return flagBadQuality(booksColl, id)
}
func (db *DB) ActiveBook(id string) error {
booksColl := db.session.DB(db.name).C(books_coll)
return activeBook(booksColl, id)