-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove_book.go
52 lines (42 loc) · 1.4 KB
/
remove_book.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
package lib
import (
"fmt"
)
// removeBookOptions are the optional arguments for RemoveBook.
type removeBookOptions struct {
db NullString
}
// DefaultRemoveBookOptions are the default options for RemoveBook.
func DefaultRemoveBookOptions() removeBookOptions {
return removeBookOptions{}
}
// WithDB sets the location of the bookmarks database.
func (o *removeBookOptions) WithDB(db string) {
o.db = NullStringFrom(db)
}
// RemoveBook removes a bookmark from the bookmarks database.
func RemoveBook(id string, options removeBookOptions) (err error) {
return execWithTransaction(options.db, connectDB, func(tx transaction) error {
if err := validateBookID(tx, id); err != nil {
return fmt.Errorf("bookmark ID validation failed while removing bookmark: %w", err)
}
books, err := getBooksDB(tx, getBooksDBArgs{
idFilter: id,
includeBooks: true,
})
if err != nil {
return fmt.Errorf("error getting bookmarks while removing bookmark: %w", err)
}
book := books[0]
if err = unlinkTagsDB(tx, book.ID, book.Tags); err != nil {
return fmt.Errorf("error unlinking tags while removing bookmark: %w", err)
}
if err = removeBookDB(tx, book.ID); err != nil {
return fmt.Errorf("error while removing bookmark: %w", err)
}
if err = cleanOrphanedTagsDB(tx, book.Tags); err != nil {
return fmt.Errorf("error cleaning orphaned tags while removing bookmark: %w", err)
}
return nil
})
}