Fix problem editing tags

This commit is contained in:
Radhi Fadlillah 2018-02-26 17:00:14 +07:00
parent c0d350def8
commit 81de2951b9
4 changed files with 33 additions and 15 deletions

View file

@ -19,7 +19,6 @@ var (
Long: "Update fields of an existing bookmark. " +
"Accepts space-separated list of indices (e.g. 5 6 23 4 110 45), hyphenated range (e.g. 100-200) or both (e.g. 1-3 7 9). " +
"If no arguments, ALL bookmarks will be updated. Update works differently depending on the flags:\n" +
"- If --title, --tag or --comment is passed without any value, clear the corresponding field from DB.\n" +
"- If indices are passed without any flags (--url, --title, --tag and --excerpt), read the URLs from DB and update titles from web.\n" +
"- If --url is passed (and --title is omitted), update the title from web using the URL. While using this flag, update only accept EXACTLY one index.\n" +
"While updating bookmark's tags, you can use - to remove tag (e.g. -nature to remove nature tag from this bookmark).",
@ -176,10 +175,10 @@ func updateBookmarks(indices []string, url, title, excerpt string, tags []string
bookmarks[i].Tags = newTags
}
err = DB.UpdateBookmarks(bookmarks)
result, err := DB.UpdateBookmarks(bookmarks)
if err != nil {
return []model.Bookmark{}, fmt.Errorf("Failed to update bookmarks: %v", err)
}
return bookmarks, nil
return result, nil
}

View file

@ -20,7 +20,7 @@ type Database interface {
SearchBookmarks(orderLatest bool, keyword string, tags ...string) ([]model.Bookmark, error)
// UpdateBookmarks updates the saved bookmark in database.
UpdateBookmarks(bookmarks []model.Bookmark) error
UpdateBookmarks(bookmarks []model.Bookmark) ([]model.Bookmark, error)
// CreateAccount creates new account in database
CreateAccount(username, password string) error

View file

@ -461,11 +461,11 @@ func (db *SQLiteDatabase) SearchBookmarks(orderLatest bool, keyword string, tags
}
// UpdateBookmarks updates the saved bookmark in database.
func (db *SQLiteDatabase) UpdateBookmarks(bookmarks []model.Bookmark) (err error) {
func (db *SQLiteDatabase) UpdateBookmarks(bookmarks []model.Bookmark) (result []model.Bookmark, err error) {
// Prepare transaction
tx, err := db.Beginx()
if err != nil {
return err
return []model.Bookmark{}, err
}
// Make sure to rollback if panic ever happened
@ -473,6 +473,8 @@ func (db *SQLiteDatabase) UpdateBookmarks(bookmarks []model.Bookmark) (err error
if r := recover(); r != nil {
panicErr, _ := r.(error)
tx.Rollback()
result = []model.Bookmark{}
err = panicErr
}
}()
@ -499,6 +501,7 @@ func (db *SQLiteDatabase) UpdateBookmarks(bookmarks []model.Bookmark) (err error
stmtDeleteBookmarkTag, err := tx.Preparex(`DELETE FROM bookmark_tag WHERE bookmark_id = ? AND tag_id = ?`)
checkError(err)
result = []model.Bookmark{}
for _, book := range bookmarks {
stmtUpdateBookmark.MustExec(
book.URL,
@ -516,6 +519,7 @@ func (db *SQLiteDatabase) UpdateBookmarks(bookmarks []model.Bookmark) (err error
book.HTML,
book.ID)
newTags := []model.Tag{}
for _, tag := range book.Tags {
if tag.Deleted {
stmtDeleteBookmarkTag.MustExec(book.ID, tag.ID)
@ -535,14 +539,19 @@ func (db *SQLiteDatabase) UpdateBookmarks(bookmarks []model.Bookmark) (err error
stmtInsertBookmarkTag.Exec(tagID, book.ID)
}
newTags = append(newTags, tag)
}
book.Tags = newTags
result = append(result, book)
}
// Commit transaction
err = tx.Commit()
checkError(err)
return err
return result, err
}
// CreateAccount saves new account to database. Returns new ID and error if any happened.

View file

@ -252,12 +252,24 @@
if (this.inputBookmark.url === "") return;
var tags = this.inputBookmark.tags.replace(/\s+/g, " "),
listTag = tags === "" ? [] : listTag = tags.split(/\s+/g),
finalTag = [];
var idx = this.inputBookmark.index,
tags = this.inputBookmark.tags.replace(/\s+/g, " "),
newTags = tags === "" ? [] : listTag = tags.split(/\s+/g),
finalTags = [];
for (var i = 0; i < listTag.length; i++) {
finalTag.push({
if (idx !== -1) {
var oldTags = this.bookmarks[idx].tags;
for (var i = 0; i < oldTags.length; i++) {
if (newTags.indexOf(oldTags[i].name) === -1) {
finalTags.push({
name: '-' + oldTags[i].name
})
}
}
}
for (var i = 0; i < newTags.length; i++) {
finalTags.push({
name: listTag[i]
});
}
@ -271,12 +283,10 @@
url: this.inputBookmark.url,
title: this.inputBookmark.title,
excerpt: this.inputBookmark.excerpt,
tags: finalTag
tags: finalTags
}
})
.then(function (response) {
var idx = app.inputBookmark.index;
if (idx === -1) app.bookmarks.unshift(response.data);
else {
app.bookmarks.splice(idx, 1, response.data);