Add history of modifications

This commit is contained in:
Las Zenow 2015-01-22 21:58:27 -06:00
parent e732909773
commit 6993fcb9e8
3 changed files with 17 additions and 32 deletions

View file

@ -99,6 +99,7 @@ func saveHandler(h handler) {
"lang": lang}
err := h.db.UpdateBook(id, book)
if err != nil {
log.Error("Updating book: ", err)
notFound(h)
return
}

View file

@ -4,6 +4,7 @@ import (
log "github.com/cihub/seelog"
"strings"
"time"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
@ -38,6 +39,11 @@ type Book struct {
BadQualityReporters []string `bad_quality_reporters`
}
type history struct {
Date time.Time
Changes bson.M
}
func indexBooks(coll *mgo.Collection) {
indexes := []mgo.Index{
{
@ -119,18 +125,22 @@ func deleteBook(coll *mgo.Collection, id string) error {
func updateBook(coll *mgo.Collection, id string, data map[string]interface{}) error {
var book map[string]interface{}
record := history{time.Now(), bson.M{}}
err := coll.Find(bson.M{"id": id}).One(&book)
if err != nil {
return err
}
for k, v := range data {
book[k] = v
for k, _ := range data {
record.Changes[k] = book[k]
if k == "lang" {
if lang := metadataLang(data); lang != "" {
data["_lang"] = lang
}
}
}
if lang := metadataLang(book); lang != "" {
data["_lang"] = lang
}
return coll.Update(bson.M{"id": id}, bson.M{"$set": data})
return coll.Update(bson.M{"id": id}, bson.M{"$set": data, "$push": bson.M{"history": record}})
}
func flagBadQuality(coll *mgo.Collection, id string, user string) error {

View file

@ -51,32 +51,6 @@ func TestActiveBook(t *testing.T) {
}
}
func TestUpdateBookKeywords(t *testing.T) {
db := Init(test_host, test_coll)
defer db.del()
tAddBook(t, db)
books, _, _ := db.GetNewBooks(1, 0)
db.UpdateBook(books[0].Id, map[string]interface{}{"title": "Some other title"})
books, _, _ = db.GetNewBooks(1, 0)
keywords := books[0].Keywords
alice := false
bob := false
for _, e := range keywords {
if e == "alice" {
alice = true
}
if e == "bob" {
bob = true
}
}
if !alice || !bob {
t.Error("Alce or Bob are not in the keywords:", keywords)
}
}
func TestFlag(t *testing.T) {
db := Init(test_host, test_coll)
defer db.del()