From 40800c6527be2e79563f3f12a343dff9ead89cfc Mon Sep 17 00:00:00 2001 From: Las Zenow Date: Wed, 2 Jul 2014 21:09:41 -0500 Subject: [PATCH] Remove the dependency of bson to interact with the books database --- admin.go | 12 +++++------- database/books.go | 12 ++++++------ database/database.go | 7 +++---- 3 files changed, 14 insertions(+), 17 deletions(-) diff --git a/admin.go b/admin.go index c82b789..6015dfe 100644 --- a/admin.go +++ b/admin.go @@ -5,7 +5,6 @@ import log "github.com/cihub/seelog" import ( "git.gitorious.org/trantor/trantor.git/database" "github.com/gorilla/mux" - "labix.org/v2/mgo/bson" "net/http" "strconv" "strings" @@ -27,7 +26,7 @@ func deleteHandler(h handler) { continue } DeleteBook(book, h.db) - h.db.DeleteBook(bson.ObjectIdHex(id)) + h.db.DeleteBook(id) if !book.Active { isNew = true @@ -74,13 +73,12 @@ func cleanEmptyStr(s []string) []string { } func saveHandler(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) title := h.r.FormValue("title") publisher := h.r.FormValue("publisher") date := h.r.FormValue("date") @@ -104,7 +102,7 @@ func saveHandler(h handler) { h.sess.Notify("Book Modified!", "", "success") h.sess.Save(h.w, h.r) if h.db.BookActive(id) { - http.Redirect(h.w, h.r, "/book/"+idStr, http.StatusFound) + http.Redirect(h.w, h.r, "/book/"+id, http.StatusFound) } else { http.Redirect(h.w, h.r, "/new/", http.StatusFound) } @@ -186,7 +184,7 @@ func storeHandler(h handler) { log.Error("Error storing book '", book.Title, "': ", err.Error()) continue } - h.db.UpdateBook(bson.ObjectIdHex(id), bson.M{"active": true}) + h.db.UpdateBook(id, map[string]interface{}{"active": true}) titles = append(titles, book.Title) } if titles != nil { diff --git a/database/books.go b/database/books.go index 1672ae9..c408e52 100644 --- a/database/books.go +++ b/database/books.go @@ -83,18 +83,18 @@ func getBookId(coll *mgo.Collection, id string) (Book, error) { return book, err } -func deleteBook(coll *mgo.Collection, id bson.ObjectId) error { - return coll.Remove(bson.M{"_id": id}) +func deleteBook(coll *mgo.Collection, id string) error { + return coll.RemoveId(bson.ObjectIdHex(id)) } -func updateBook(coll *mgo.Collection, id bson.ObjectId, data map[string]interface{}) error { +func updateBook(coll *mgo.Collection, id string, data map[string]interface{}) error { data["keywords"] = keywords(data) - return coll.Update(bson.M{"_id": id}, bson.M{"$set": data}) + return coll.UpdateId(bson.ObjectIdHex(id), bson.M{"$set": data}) } -func bookActive(coll *mgo.Collection, id bson.ObjectId) bool { +func bookActive(coll *mgo.Collection, id string) bool { var book Book - err := coll.Find(bson.M{"_id": id}).One(&book) + err := coll.FindId(bson.ObjectIdHex(id)).One(&book) if err != nil { return false } diff --git a/database/database.go b/database/database.go index 06a4613..439c381 100644 --- a/database/database.go +++ b/database/database.go @@ -63,18 +63,17 @@ func (db *DB) GetBookId(id string) (Book, error) { return getBookId(booksColl, id) } -// FIXME: don't export bson data -func (db *DB) DeleteBook(id bson.ObjectId) error { +func (db *DB) DeleteBook(id string) error { booksColl := db.session.DB(db.name).C(books_coll) return deleteBook(booksColl, id) } -func (db *DB) UpdateBook(id bson.ObjectId, data map[string]interface{}) error { +func (db *DB) UpdateBook(id string, data map[string]interface{}) error { booksColl := db.session.DB(db.name).C(books_coll) return updateBook(booksColl, id, data) } -func (db *DB) BookActive(id bson.ObjectId) bool { +func (db *DB) BookActive(id string) bool { booksColl := db.session.DB(db.name).C(books_coll) return bookActive(booksColl, id) }