Add DB.GetBookId

This commit is contained in:
Las Zenow 2014-07-02 20:40:24 -05:00
parent 07a133e49a
commit 44d3a191f6
6 changed files with 42 additions and 59 deletions

View file

@ -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})
}

View file

@ -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)