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. " + 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). " + "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 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 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" + "- 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).", "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 bookmarks[i].Tags = newTags
} }
err = DB.UpdateBookmarks(bookmarks) result, err := DB.UpdateBookmarks(bookmarks)
if err != nil { if err != nil {
return []model.Bookmark{}, fmt.Errorf("Failed to update bookmarks: %v", err) 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) SearchBookmarks(orderLatest bool, keyword string, tags ...string) ([]model.Bookmark, error)
// UpdateBookmarks updates the saved bookmark in database. // 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 creates new account in database
CreateAccount(username, password string) error 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. // 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 // Prepare transaction
tx, err := db.Beginx() tx, err := db.Beginx()
if err != nil { if err != nil {
return err return []model.Bookmark{}, err
} }
// Make sure to rollback if panic ever happened // 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 { if r := recover(); r != nil {
panicErr, _ := r.(error) panicErr, _ := r.(error)
tx.Rollback() tx.Rollback()
result = []model.Bookmark{}
err = panicErr 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 = ?`) stmtDeleteBookmarkTag, err := tx.Preparex(`DELETE FROM bookmark_tag WHERE bookmark_id = ? AND tag_id = ?`)
checkError(err) checkError(err)
result = []model.Bookmark{}
for _, book := range bookmarks { for _, book := range bookmarks {
stmtUpdateBookmark.MustExec( stmtUpdateBookmark.MustExec(
book.URL, book.URL,
@ -516,6 +519,7 @@ func (db *SQLiteDatabase) UpdateBookmarks(bookmarks []model.Bookmark) (err error
book.HTML, book.HTML,
book.ID) book.ID)
newTags := []model.Tag{}
for _, tag := range book.Tags { for _, tag := range book.Tags {
if tag.Deleted { if tag.Deleted {
stmtDeleteBookmarkTag.MustExec(book.ID, tag.ID) stmtDeleteBookmarkTag.MustExec(book.ID, tag.ID)
@ -535,14 +539,19 @@ func (db *SQLiteDatabase) UpdateBookmarks(bookmarks []model.Bookmark) (err error
stmtInsertBookmarkTag.Exec(tagID, book.ID) stmtInsertBookmarkTag.Exec(tagID, book.ID)
} }
newTags = append(newTags, tag)
} }
book.Tags = newTags
result = append(result, book)
} }
// Commit transaction // Commit transaction
err = tx.Commit() err = tx.Commit()
checkError(err) checkError(err)
return err return result, err
} }
// CreateAccount saves new account to database. Returns new ID and error if any happened. // 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; if (this.inputBookmark.url === "") return;
var tags = this.inputBookmark.tags.replace(/\s+/g, " "), var idx = this.inputBookmark.index,
listTag = tags === "" ? [] : listTag = tags.split(/\s+/g), tags = this.inputBookmark.tags.replace(/\s+/g, " "),
finalTag = []; newTags = tags === "" ? [] : listTag = tags.split(/\s+/g),
finalTags = [];
for (var i = 0; i < listTag.length; i++) { if (idx !== -1) {
finalTag.push({ 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] name: listTag[i]
}); });
} }
@ -271,12 +283,10 @@
url: this.inputBookmark.url, url: this.inputBookmark.url,
title: this.inputBookmark.title, title: this.inputBookmark.title,
excerpt: this.inputBookmark.excerpt, excerpt: this.inputBookmark.excerpt,
tags: finalTag tags: finalTags
} }
}) })
.then(function (response) { .then(function (response) {
var idx = app.inputBookmark.index;
if (idx === -1) app.bookmarks.unshift(response.data); if (idx === -1) app.bookmarks.unshift(response.data);
else { else {
app.bookmarks.splice(idx, 1, response.data); app.bookmarks.splice(idx, 1, response.data);