Move files to a local folder

This commit is contained in:
Las Zenow 2014-08-21 19:24:23 -05:00
parent 97ed9ec073
commit f4690b2bba
18 changed files with 556 additions and 288 deletions

View file

@ -1,7 +1,6 @@
package database
import (
"errors"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"gopkgs.com/unidecode.v1"
@ -14,7 +13,7 @@ const (
)
type Book struct {
Id string `bson:"_id"`
Id string
Title string
Author []string
Contributor string
@ -31,10 +30,8 @@ type Book struct {
Coverage string
Rights string
Meta string
File bson.ObjectId
FileSize int
Cover bson.ObjectId
CoverSmall bson.ObjectId
Cover bool
Active bool
Keywords []string
}
@ -66,35 +63,41 @@ func _getBooks(coll *mgo.Collection, query bson.M, length int, start int) (books
}
err = q.All(&books)
for i, b := range books {
books[i].Id = bson.ObjectId(b.Id).Hex()
}
return
}
func getBookId(coll *mgo.Collection, id string) (Book, error) {
var book Book
if !bson.IsObjectIdHex(id) {
return book, errors.New("Not valid book id")
}
err := coll.FindId(bson.ObjectIdHex(id)).One(&book)
book.Id = bson.ObjectId(book.Id).Hex()
err := coll.Find(bson.M{"id": id}).One(&book)
return book, err
}
func deleteBook(coll *mgo.Collection, id string) error {
return coll.RemoveId(bson.ObjectIdHex(id))
return coll.Remove(bson.M{"id": id})
}
func updateBook(coll *mgo.Collection, id string, data map[string]interface{}) error {
data["keywords"] = keywords(data)
return coll.UpdateId(bson.ObjectIdHex(id), bson.M{"$set": data})
var book map[string]interface{}
err := coll.Find(bson.M{"id": id}).One(&book)
if err != nil {
return err
}
for k, v := range data {
book[k] = v
}
data["keywords"] = keywords(book)
return coll.Update(bson.M{"id": id}, bson.M{"$set": data})
}
func bookActive(coll *mgo.Collection, id string) bool {
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})
}
func isBookActive(coll *mgo.Collection, id string) bool {
var book Book
err := coll.FindId(bson.ObjectIdHex(id)).One(&book)
err := coll.Find(bson.M{"id": id}).One(&book)
if err != nil {
return false
}
@ -123,15 +126,29 @@ func buildQuery(q string) bson.M {
func keywords(b map[string]interface{}) (k []string) {
title, _ := b["title"].(string)
k = tokens(title)
author, _ := b["author"].([]string)
for _, a := range author {
k = append(k, tokens(a)...)
}
k = append(k, listKeywords(b["author"])...)
publisher, _ := b["publisher"].(string)
k = append(k, tokens(publisher)...)
subject, _ := b["subject"].([]string)
for _, s := range subject {
k = append(k, tokens(s)...)
k = append(k, listKeywords(b["subject"])...)
return
}
func listKeywords(v interface{}) (k []string) {
list, ok := v.([]string)
if !ok {
list, _ := v.([]interface{})
for _, e := range list {
str := e.(string)
k = append(k, tokens(str)...)
}
return
}
for _, e := range list {
k = append(k, tokens(e)...)
}
return
}

View file

@ -5,6 +5,7 @@ import "testing"
var book = map[string]interface{}{
"title": "some title",
"author": []string{"Alice", "Bob"},
"id": "r_m-IOzzIbA6QK5w",
}
func TestAddBook(t *testing.T) {
@ -15,7 +16,7 @@ func TestAddBook(t *testing.T) {
books, num, err := db.GetNewBooks(1, 0)
if err != nil {
t.Fatalf("db.GetBooks() return an error: ", err)
t.Fatal("db.GetBooks() return an error: ", err)
}
if num < 1 {
t.Fatalf("db.GetBooks() didn't find any result.")
@ -24,13 +25,61 @@ func TestAddBook(t *testing.T) {
t.Fatalf("db.GetBooks() didn't return any result.")
}
if books[0].Title != book["title"] {
t.Errorf("Book title don't match : '", books[0].Title, "' <=> '", book["title"], "'")
t.Error("Book title don't match : '", books[0].Title, "' <=> '", book["title"], "'")
}
}
func TestActiveBook(t *testing.T) {
db := Init(test_host, test_coll)
defer db.del()
tAddBook(t, db)
books, _, _ := db.GetNewBooks(1, 0)
id := books[0].Id
err := db.ActiveBook(id)
if err != nil {
t.Fatal("db.ActiveBook(", id, ") return an error: ", err)
}
b, err := db.GetBookId(id)
if err != nil {
t.Fatal("db.GetBookId(", id, ") return an error: ", err)
}
if b.Author[0] != books[0].Author[0] {
t.Error("Book author don't match : '", b.Author, "' <=> '", book["author"], "'")
}
}
func TestUpdateBookKeywords(t *testing.T) {
db := Init(test_host, test_coll)
defer db.del()
tAddBook(t, db)
books, _, _ := db.GetNewBooks(1, 0)
db.UpdateBook(books[0].Id, map[string]interface{}{"title": "Some other title"})
books, _, _ = db.GetNewBooks(1, 0)
keywords := books[0].Keywords
alice := false
bob := false
for _, e := range keywords {
if e == "alice" {
alice = true
}
if e == "bob" {
bob = true
}
}
if !alice || !bob {
t.Error("Alce or Bob are not in the keywords:", keywords)
}
}
func tAddBook(t *testing.T, db *DB) {
err := db.AddBook(book)
if err != nil {
t.Errorf("db.AddBook(", book, ") return an error: ", err)
t.Error("db.AddBook(", book, ") return an error: ", err)
}
}

View file

@ -73,9 +73,14 @@ func (db *DB) UpdateBook(id string, data map[string]interface{}) error {
return updateBook(booksColl, id, data)
}
func (db *DB) BookActive(id string) bool {
func (db *DB) ActiveBook(id string) error {
booksColl := db.session.DB(db.name).C(books_coll)
return bookActive(booksColl, id)
return activeBook(booksColl, id)
}
func (db *DB) IsBookActive(id string) bool {
booksColl := db.session.DB(db.name).C(books_coll)
return isBookActive(booksColl, id)
}
func (db *DB) User(name string) *User {
@ -154,10 +159,6 @@ func (db *DB) UpdateDownloadedBooks() error {
return u.UpdateMostBooks("download")
}
func (db *DB) GetFS(prefix string) *mgo.GridFS {
return db.session.DB(db.name).GridFS(prefix)
}
func (db *DB) GetTags() ([]string, error) {
tagsColl := db.session.DB(db.name).C(tags_coll)
return GetTags(tagsColl)