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 (
|
import (
|
||||||
"git.gitorious.org/trantor/trantor.git/database"
|
"git.gitorious.org/trantor/trantor.git/database"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"labix.org/v2/mgo/bson"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -27,7 +26,7 @@ func deleteHandler(h handler) {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
DeleteBook(book, h.db)
|
DeleteBook(book, h.db)
|
||||||
h.db.DeleteBook(bson.ObjectIdHex(id))
|
h.db.DeleteBook(id)
|
||||||
|
|
||||||
if !book.Active {
|
if !book.Active {
|
||||||
isNew = true
|
isNew = true
|
||||||
|
@ -74,13 +73,12 @@ func cleanEmptyStr(s []string) []string {
|
||||||
}
|
}
|
||||||
|
|
||||||
func saveHandler(h handler) {
|
func saveHandler(h handler) {
|
||||||
idStr := mux.Vars(h.r)["id"]
|
id := mux.Vars(h.r)["id"]
|
||||||
if !h.sess.IsAdmin() || !bson.IsObjectIdHex(idStr) {
|
if !h.sess.IsAdmin() {
|
||||||
notFound(h)
|
notFound(h)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
id := bson.ObjectIdHex(idStr)
|
|
||||||
title := h.r.FormValue("title")
|
title := h.r.FormValue("title")
|
||||||
publisher := h.r.FormValue("publisher")
|
publisher := h.r.FormValue("publisher")
|
||||||
date := h.r.FormValue("date")
|
date := h.r.FormValue("date")
|
||||||
|
@ -104,7 +102,7 @@ func saveHandler(h handler) {
|
||||||
h.sess.Notify("Book Modified!", "", "success")
|
h.sess.Notify("Book Modified!", "", "success")
|
||||||
h.sess.Save(h.w, h.r)
|
h.sess.Save(h.w, h.r)
|
||||||
if h.db.BookActive(id) {
|
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 {
|
} else {
|
||||||
http.Redirect(h.w, h.r, "/new/", http.StatusFound)
|
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())
|
log.Error("Error storing book '", book.Title, "': ", err.Error())
|
||||||
continue
|
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)
|
titles = append(titles, book.Title)
|
||||||
}
|
}
|
||||||
if titles != nil {
|
if titles != nil {
|
||||||
|
|
|
@ -83,18 +83,18 @@ func getBookId(coll *mgo.Collection, id string) (Book, error) {
|
||||||
return book, err
|
return book, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func deleteBook(coll *mgo.Collection, id bson.ObjectId) error {
|
func deleteBook(coll *mgo.Collection, id string) error {
|
||||||
return coll.Remove(bson.M{"_id": id})
|
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)
|
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
|
var book Book
|
||||||
err := coll.Find(bson.M{"_id": id}).One(&book)
|
err := coll.FindId(bson.ObjectIdHex(id)).One(&book)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,18 +63,17 @@ func (db *DB) GetBookId(id string) (Book, error) {
|
||||||
return getBookId(booksColl, id)
|
return getBookId(booksColl, id)
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: don't export bson data
|
func (db *DB) DeleteBook(id string) error {
|
||||||
func (db *DB) DeleteBook(id bson.ObjectId) error {
|
|
||||||
booksColl := db.session.DB(db.name).C(books_coll)
|
booksColl := db.session.DB(db.name).C(books_coll)
|
||||||
return deleteBook(booksColl, id)
|
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)
|
booksColl := db.session.DB(db.name).C(books_coll)
|
||||||
return updateBook(booksColl, id, data)
|
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)
|
booksColl := db.session.DB(db.name).C(books_coll)
|
||||||
return bookActive(booksColl, id)
|
return bookActive(booksColl, id)
|
||||||
}
|
}
|
||||||
|
|
Reference in a new issue