Add DB.GetBookId
This commit is contained in:
parent
07a133e49a
commit
44d3a191f6
6 changed files with 42 additions and 59 deletions
37
admin.go
37
admin.go
|
@ -20,20 +20,14 @@ func deleteHandler(h handler) {
|
|||
var titles []string
|
||||
var isNew bool
|
||||
ids := strings.Split(mux.Vars(h.r)["ids"], "/")
|
||||
for _, idStr := range ids {
|
||||
if !bson.IsObjectIdHex(idStr) {
|
||||
continue
|
||||
}
|
||||
|
||||
id := bson.ObjectIdHex(idStr)
|
||||
books, _, err := h.db.GetBooks(bson.M{"_id": id}, 0, 0)
|
||||
for _, id := range ids {
|
||||
book, err := h.db.GetBookId(id)
|
||||
if err != nil {
|
||||
h.sess.Notify("Book not found!", "The book with id '"+idStr+"' is not there", "error")
|
||||
h.sess.Notify("Book not found!", "The book with id '"+id+"' is not there", "error")
|
||||
continue
|
||||
}
|
||||
book := books[0]
|
||||
DeleteBook(book, h.db)
|
||||
h.db.DeleteBook(id)
|
||||
h.db.DeleteBook(bson.ObjectIdHex(id))
|
||||
|
||||
if !book.Active {
|
||||
isNew = true
|
||||
|
@ -52,20 +46,19 @@ func deleteHandler(h handler) {
|
|||
}
|
||||
|
||||
func editHandler(h handler) {
|
||||
idStr := mux.Vars(h.r)["id"]
|
||||
if !h.sess.IsAdmin() || !bson.IsObjectIdHex(idStr) {
|
||||
id := mux.Vars(h.r)["id"]
|
||||
if !h.sess.IsAdmin() {
|
||||
notFound(h)
|
||||
return
|
||||
}
|
||||
id := bson.ObjectIdHex(idStr)
|
||||
books, _, err := h.db.GetBooks(bson.M{"_id": id}, 0, 0)
|
||||
book, err := h.db.GetBookId(id)
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
return
|
||||
}
|
||||
|
||||
var data bookData
|
||||
data.Book = books[0]
|
||||
data.Book = book
|
||||
data.S = GetStatus(h)
|
||||
loadTemplate(h.w, "edit", data)
|
||||
}
|
||||
|
@ -183,24 +176,18 @@ func storeHandler(h handler) {
|
|||
|
||||
var titles []string
|
||||
ids := strings.Split(mux.Vars(h.r)["ids"], "/")
|
||||
for _, idStr := range ids {
|
||||
if !bson.IsObjectIdHex(idStr) {
|
||||
continue
|
||||
}
|
||||
|
||||
id := bson.ObjectIdHex(idStr)
|
||||
books, _, err := h.db.GetBooks(bson.M{"_id": id}, 0, 0)
|
||||
for _, id := range ids {
|
||||
book, err := h.db.GetBookId(id)
|
||||
if err != nil {
|
||||
h.sess.Notify("Book not found!", "The book with id '"+idStr+"' is not there", "error")
|
||||
h.sess.Notify("Book not found!", "The book with id '"+id+"' is not there", "error")
|
||||
continue
|
||||
}
|
||||
book := books[0]
|
||||
if err != nil {
|
||||
h.sess.Notify("An error ocurred!", err.Error(), "error")
|
||||
log.Error("Error storing book '", book.Title, "': ", err.Error())
|
||||
continue
|
||||
}
|
||||
h.db.UpdateBook(id, bson.M{"active": true})
|
||||
h.db.UpdateBook(bson.ObjectIdHex(id), bson.M{"active": true})
|
||||
titles = append(titles, book.Title)
|
||||
}
|
||||
if titles != nil {
|
||||
|
|
10
cover.go
10
cover.go
|
@ -23,17 +23,11 @@ import (
|
|||
|
||||
func coverHandler(h handler) {
|
||||
vars := mux.Vars(h.r)
|
||||
if !bson.IsObjectIdHex(vars["id"]) {
|
||||
book, err := h.db.GetBookId(vars["id"])
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
return
|
||||
}
|
||||
id := bson.ObjectIdHex(vars["id"])
|
||||
books, _, err := h.db.GetBooks(bson.M{"_id": id}, 0, 0)
|
||||
if err != nil || len(books) == 0 {
|
||||
notFound(h)
|
||||
return
|
||||
}
|
||||
book := books[0]
|
||||
|
||||
if !book.Active {
|
||||
if !h.sess.IsAdmin() {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"labix.org/v2/mgo"
|
||||
"labix.org/v2/mgo/bson"
|
||||
)
|
||||
|
@ -59,6 +60,17 @@ func getBooks(coll *mgo.Collection, query bson.M, length int, start int) (books
|
|||
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()
|
||||
return book, err
|
||||
}
|
||||
|
||||
func deleteBook(coll *mgo.Collection, id bson.ObjectId) error {
|
||||
return coll.Remove(bson.M{"_id": id})
|
||||
}
|
||||
|
|
|
@ -54,6 +54,11 @@ func (db *DB) GetBooks(query bson.M, length int, start int) (books []Book, num i
|
|||
return getBooks(booksColl, query, length, start)
|
||||
}
|
||||
|
||||
func (db *DB) GetBookId(id string) (Book, error) {
|
||||
booksColl := db.session.DB(db.name).C(books_coll)
|
||||
return getBookId(booksColl, id)
|
||||
}
|
||||
|
||||
func (db *DB) DeleteBook(id bson.ObjectId) error {
|
||||
booksColl := db.session.DB(db.name).C(books_coll)
|
||||
return deleteBook(booksColl, id)
|
||||
|
|
10
reader.go
10
reader.go
|
@ -178,12 +178,11 @@ func openReadEpub(h handler) (*epubgo.Epub, database.Book) {
|
|||
if !bson.IsObjectIdHex(id) {
|
||||
return nil, book
|
||||
}
|
||||
books, _, err := h.db.GetBooks(bson.M{"_id": bson.ObjectIdHex(id)}, 0, 0)
|
||||
if err != nil || len(books) == 0 {
|
||||
book, err := h.db.GetBookId(id)
|
||||
if err != nil {
|
||||
return nil, book
|
||||
}
|
||||
|
||||
book = books[0]
|
||||
if !book.Active {
|
||||
if !h.sess.IsAdmin() {
|
||||
return nil, book
|
||||
|
@ -205,12 +204,11 @@ func contentHandler(h handler) {
|
|||
return
|
||||
}
|
||||
|
||||
books, _, err := h.db.GetBooks(bson.M{"_id": bson.ObjectIdHex(id)}, 0, 0)
|
||||
if err != nil || len(books) == 0 {
|
||||
book, err := h.db.GetBookId(id)
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
return
|
||||
}
|
||||
book := books[0]
|
||||
if !book.Active {
|
||||
if !h.sess.IsAdmin() {
|
||||
notFound(h)
|
||||
|
|
27
trantor.go
27
trantor.go
|
@ -44,40 +44,27 @@ type bookData struct {
|
|||
}
|
||||
|
||||
func bookHandler(h handler) {
|
||||
idStr := mux.Vars(h.r)["id"]
|
||||
if !bson.IsObjectIdHex(idStr) {
|
||||
notFound(h)
|
||||
return
|
||||
}
|
||||
|
||||
id := mux.Vars(h.r)["id"]
|
||||
var data bookData
|
||||
data.S = GetStatus(h)
|
||||
id := bson.ObjectIdHex(idStr)
|
||||
books, _, err := h.db.GetBooks(bson.M{"_id": id}, 0, 0)
|
||||
if err != nil || len(books) == 0 {
|
||||
book, err := h.db.GetBookId(id)
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
return
|
||||
}
|
||||
data.Book = books[0]
|
||||
data.Book = book
|
||||
data.Description = strings.Split(data.Book.Description, "\n")
|
||||
loadTemplate(h.w, "book", data)
|
||||
}
|
||||
|
||||
func downloadHandler(h handler) {
|
||||
idStr := mux.Vars(h.r)["id"]
|
||||
if !bson.IsObjectIdHex(idStr) {
|
||||
id := mux.Vars(h.r)["id"]
|
||||
book, err := h.db.GetBookId(id)
|
||||
if err != nil {
|
||||
notFound(h)
|
||||
return
|
||||
}
|
||||
|
||||
id := bson.ObjectIdHex(idStr)
|
||||
books, _, err := h.db.GetBooks(bson.M{"_id": id}, 0, 0)
|
||||
if err != nil || len(books) == 0 {
|
||||
notFound(h)
|
||||
return
|
||||
}
|
||||
book := books[0]
|
||||
|
||||
if !book.Active {
|
||||
if !h.sess.IsAdmin() {
|
||||
notFound(h)
|
||||
|
|
Reference in a new issue