-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_tags.go
65 lines (52 loc) · 1.6 KB
/
remove_tags.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package lib
import (
"fmt"
"github.com/samber/lo"
)
// removeTagsOptions are the optional arguments for RemoveTags.
type removeTagsOptions struct {
db NullString
}
// DefaultRemoveTagsOptions are the default options for RemoveTags.
func DefaultRemoveTagsOptions() removeTagsOptions {
return removeTagsOptions{}
}
// WithDB sets the location of the bookmarks database.
func (o *removeTagsOptions) WithDB(db string) {
o.db = NullStringFrom(db)
}
// RemoveTags removes tags from a bookmark in the bookmarks database.
func RemoveTags(id string, tags []string, options removeTagsOptions) (Book, error) {
return queryWithTransaction(options.db, connectDB, func(tx transaction) (Book, error) {
var book Book
books, err := getBooksDB(tx, getBooksDBArgs{
idFilter: id,
includeBooks: true,
})
if err != nil {
return book, fmt.Errorf("error getting bookmarks while removing tags: %w", err)
}
if len(books) != 1 || books[0].IsFolder {
return book, ErrBookNotFound
}
for _, tag := range tags {
if !lo.Contains(books[0].Tags, tag) {
return book, ErrTagNotFound
}
}
if err = unlinkTagsDB(tx, books[0].ID, tags); err != nil {
return book, fmt.Errorf("error unlinking tags while removing tags: %w", err)
}
if err = cleanOrphanedTagsDB(tx, tags); err != nil {
return book, fmt.Errorf("error cleaning orphaned tags while removing tags: %w", err)
}
books, err = getBooksDB(tx, getBooksDBArgs{
idFilter: id,
includeBooks: true,
})
if err != nil {
return book, fmt.Errorf("error getting bookmarks while removing tags: %w", err)
}
return books[0], nil
})
}