One flag per user
This commit is contained in:
parent
f5363e17bc
commit
deece4f5d0
5 changed files with 64 additions and 34 deletions
|
@ -16,28 +16,29 @@ const (
|
|||
)
|
||||
|
||||
type Book struct {
|
||||
Id string
|
||||
Title string
|
||||
Author []string
|
||||
Contributor string
|
||||
Publisher string
|
||||
Description string
|
||||
Subject []string
|
||||
Date string
|
||||
Lang []string
|
||||
Isbn string
|
||||
Type string
|
||||
Format string
|
||||
Source string
|
||||
Relation string
|
||||
Coverage string
|
||||
Rights string
|
||||
Meta string
|
||||
FileSize int
|
||||
Cover bool
|
||||
Active bool
|
||||
BadQuality int `bad_quality`
|
||||
Keywords []string
|
||||
Id string
|
||||
Title string
|
||||
Author []string
|
||||
Contributor string
|
||||
Publisher string
|
||||
Description string
|
||||
Subject []string
|
||||
Date string
|
||||
Lang []string
|
||||
Isbn string
|
||||
Type string
|
||||
Format string
|
||||
Source string
|
||||
Relation string
|
||||
Coverage string
|
||||
Rights string
|
||||
Meta string
|
||||
FileSize int
|
||||
Cover bool
|
||||
Active bool
|
||||
BadQuality int `bad_quality`
|
||||
BadQualityReporters []string `bad_quality_reporters`
|
||||
Keywords []string
|
||||
}
|
||||
|
||||
func indexBooks(coll *mgo.Collection) {
|
||||
|
@ -132,8 +133,24 @@ func updateBook(coll *mgo.Collection, id string, data map[string]interface{}) er
|
|||
return coll.Update(bson.M{"id": id}, bson.M{"$set": data})
|
||||
}
|
||||
|
||||
func flagBadQuality(coll *mgo.Collection, id string) error {
|
||||
return coll.Update(bson.M{"id": id}, bson.M{"$inc": bson.M{"bad_quality": 1}})
|
||||
func flagBadQuality(coll *mgo.Collection, id string, user string) error {
|
||||
b, err := getBookId(coll, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, reporter := range b.BadQualityReporters {
|
||||
if reporter == user {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return coll.Update(
|
||||
bson.M{"id": id},
|
||||
bson.M{
|
||||
"$inc": bson.M{"bad_quality": 1},
|
||||
"$addToSet": bson.M{"bad_quality_reporters": user},
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func activeBook(coll *mgo.Collection, id string) error {
|
||||
|
|
|
@ -101,9 +101,9 @@ func TestFlag(t *testing.T) {
|
|||
}
|
||||
db.ActiveBook(id3)
|
||||
|
||||
db.FlagBadQuality(id)
|
||||
db.FlagBadQuality(id)
|
||||
db.FlagBadQuality(id3)
|
||||
db.FlagBadQuality(id, "1")
|
||||
db.FlagBadQuality(id, "2")
|
||||
db.FlagBadQuality(id3, "1")
|
||||
|
||||
b, _ := db.GetBookId(id)
|
||||
if b.BadQuality != 2 {
|
||||
|
|
|
@ -85,9 +85,9 @@ func (db *DB) UpdateBook(id string, data map[string]interface{}) error {
|
|||
return updateBook(booksColl, id, data)
|
||||
}
|
||||
|
||||
func (db *DB) FlagBadQuality(id string) error {
|
||||
func (db *DB) FlagBadQuality(id string, user string) error {
|
||||
booksColl := db.session.DB(db.name).C(books_coll)
|
||||
return flagBadQuality(booksColl, id)
|
||||
return flagBadQuality(booksColl, id, user)
|
||||
}
|
||||
|
||||
func (db *DB) ActiveBook(id string) error {
|
||||
|
|
Reference in a new issue