Remove the dependency of bson to interact with the books database
This commit is contained in:
parent
93d31b53fe
commit
40800c6527
3 changed files with 14 additions and 17 deletions
12
admin.go
12
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 {
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Reference in a new issue