don't remove deleted from post store, just mark it to keep tree

This commit is contained in:
Umputun
2018-02-04 17:58:52 -06:00
parent fdfcf202b0
commit 472d6d7d81
4 changed files with 30 additions and 6 deletions
+1
View File
@@ -133,6 +133,7 @@ type Comment struct {
Votes map[string]bool `json:"votes"` // comment votes, read only
Timestamp time.Time `json:"time"` // time stamp, read only
Pin bool `json:"pin"` // pinned status, read only
Delete bool `json:"delete"` // delete status, read only
}
type Locator struct {
+20 -3
View File
@@ -130,14 +130,31 @@ func (b *BoltDB) Delete(locator Locator, commentID string) error {
}
return bdb.Update(func(tx *bolt.Tx) error {
// delete from post bucket
// mark deleted from post bucket, but don't remove. Needed to keep tree
bucket := tx.Bucket([]byte(locator.URL))
if bucket == nil {
return errors.Errorf("no bucket %s in store", locator.URL)
}
if err := bucket.Delete([]byte(commentID)); err != nil {
return errors.Wrapf(err, "can't delete key %s from bucket %s", commentID, locator.URL)
comment, err := b.load(bucket, []byte(commentID))
if err != nil {
return errors.Wrapf(err, "can't load key %s from bucket %s", commentID, locator.URL)
}
// set deleted status and clear fields
comment.Deleted = true
comment.Text = "this comment was deleted"
comment.Score = 0
comment.Votes = map[string]bool{}
comment.Edit = nil
if err := b.save(bucket, []byte(commentID), comment); err != nil {
return errors.Wrapf(err, "can't save deleted comment for key %s from bucket %s", commentID, locator.URL)
}
// if err := bucket.Delete([]byte(commentID)); err != nil {
// return errors.Wrapf(err, "can't delete key %s from bucket %s", commentID, locator.URL)
// }
// delete from "last" bucket
bucket = tx.Bucket([]byte(lastBucketName))
+8 -3
View File
@@ -40,12 +40,17 @@ func TestBoltDB_Delete(t *testing.T) {
res, err = b.Find(loc, "time")
assert.Nil(t, err)
assert.Equal(t, 1, len(res))
assert.Equal(t, "some text2", res[0].Text)
assert.Equal(t, 2, len(res))
assert.Equal(t, "this comment was deleted", res[0].Text)
assert.True(t, res[0].Deleted, "marked deleted")
assert.Equal(t, "some text2", res[1].Text)
assert.False(t, res[1].Deleted)
comments, err := b.Last("radio-t", 10)
assert.Nil(t, err)
assert.Equal(t, 1, len(comments), "only 1 left in last")
assert.Equal(t, 2, len(comments), "2 in last, nothing removed")
assert.Equal(t, "this comment was deleted", comments[1].Text)
assert.True(t, comments[1].Deleted, "marked deleted")
}
func TestBoltDB_Get(t *testing.T) {
+1
View File
@@ -27,6 +27,7 @@ type Comment struct {
Timestamp time.Time `json:"time"`
Pin bool `json:"pin,omitempty"`
Edit *Edit `json:"edit,omitempty"`
Deleted bool `json:"delete,omitempty"`
}
// Locator keeps site and url of the post