-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathbooks.go
More file actions
117 lines (106 loc) · 3.23 KB
/
Copy pathbooks.go
File metadata and controls
117 lines (106 loc) · 3.23 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package book
import (
"errors"
"log/slog"
"github.com/sbondCo/Watcharr/database/entity"
"github.com/sbondCo/Watcharr/domain"
"github.com/sbondCo/Watcharr/image"
"github.com/sbondCo/Watcharr/media/openlibrary"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)
type Service struct {
db *gorm.DB
openLibrary *openlibrary.OpenLibrary
activityProvider domain.ActivityAddProvider
}
func NewService(db *gorm.DB, openLibrary *openlibrary.OpenLibrary, activityProvider domain.ActivityAddProvider) *Service {
return &Service{
db,
openLibrary,
activityProvider,
}
}
// Cache(save) book to our table
func (s *Service) saveBook(c *entity.Book, onlyUpdate bool) error {
slog.Info("Saving book to db", "olid", c.OLID, "title", c.Title)
if c.OLID == "" || c.Title == "" {
slog.Error("savebook: content missing id or title!", "olid", c.OLID, "title", c.Title)
return errors.New("book missing id or title")
}
if c.CoverUrl != "" {
p, err := image.DownloadAndInsertImage(s.db, c.CoverUrl, "books")
if err != nil {
slog.Error("savebook: Failed to cache book cover.", "error", err)
} else {
slog.Debug("savebook: Cached book cover", "p", p)
c.CoverID = &p.ID
}
}
var res *gorm.DB
if onlyUpdate {
// We only want to update an existing row, if it exists.
res = s.db.Model(&entity.Book{}).Where("ol_id = ?", c.OLID).Updates(c)
if res.Error != nil {
slog.Error("savebook: Error updating book in database", "error", res.Error.Error())
return errors.New("failed to update cached book in database")
}
} else {
// On conflict, update existing row with details incase any were updated/missing.
res = s.db.Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "ol_id"}},
DoUpdates: clause.AssignmentColumns([]string{
"isbn",
"title",
"storyline",
"cover_url",
"cover_id",
"release_date",
"rating_average",
"rating_count",
"genres",
"author_names",
"author_ids",
"author_photo_urls",
}),
}).Create(&c)
if res.Error != nil {
// Error if anything but unique contraint error
if res.Error != gorm.ErrDuplicatedKey {
slog.Error("saveBook: Error creating book in database", "error", res.Error.Error())
return errors.New("failed to cache book in database")
}
}
}
return nil
}
func (s *Service) cacheBook(b entity.Book, onlyUpdate bool) (entity.Book, error) {
slog.Debug("cacheBook", "book_details", s)
err := s.saveBook(&b, onlyUpdate)
if err != nil {
slog.Error("cacheBook: Failed to save book!", "error", err)
return entity.Book{}, errors.New("failed to save book")
}
return b, nil
}
func (s *Service) GetOrCache(olid string) (entity.Book, error) {
var book entity.Book
s.db.Where("ol_id = ?", olid).Find(&book)
// Create book if not found from our db
if book == (entity.Book{}) {
slog.Debug("GetOrCache: book not in db, fetching...")
resp, err := s.openLibrary.GetBookDetails(olid)
if err != nil {
slog.Error("GetOrCache: content api request failed", "error", err)
return book, errors.New("failed to find requested books")
}
book, err = s.cacheBook(resp, false)
if err != nil {
slog.Error("GetOrCache: failed to cache book",
"olid", olid,
"err", err)
return book, errors.New("failed to cache content")
}
}
return book, nil
}