-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathservice.go
More file actions
260 lines (229 loc) · 8.31 KB
/
service.go
File metadata and controls
260 lines (229 loc) · 8.31 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
package main
//go:generate mockgen -destination mock_service_test.go -package main -source=service.go
import (
"context"
"errors"
"fmt"
"time"
"github.com/rl404/verniy"
)
// MediaService abstracts operations with a media service (MAL/AniList).
type MediaService interface {
GetByID(ctx context.Context, id TargetID, prefix string) (Target, error)
GetByName(ctx context.Context, name string, prefix string) ([]Target, error)
Update(ctx context.Context, id TargetID, src Source, prefix string) error
}
// MediaServiceWithMALID extends MediaService with MAL ID lookup capability.
type MediaServiceWithMALID interface {
MediaService
GetByMALID(ctx context.Context, malID int, prefix string) (Target, error)
}
// MALAnimeService implements MediaService for MAL anime operations.
type MALAnimeService struct {
client *MyAnimeListClient
}
// NewMALAnimeService creates a new MAL anime service.
func NewMALAnimeService(client *MyAnimeListClient) *MALAnimeService {
return &MALAnimeService{client: client}
}
func (s *MALAnimeService) GetByID(ctx context.Context, id TargetID, _ string) (Target, error) {
resp, err := s.client.GetAnimeByID(ctx, int(id))
if err != nil {
return nil, fmt.Errorf("error getting anime by id: %w", err)
}
// false: MALAnimeService is only used in forward sync (AniList→MAL)
ani, err := newAnimeFromMalAnime(*resp, false)
if err != nil {
return nil, fmt.Errorf("error creating anime from mal anime: %w", err)
}
return ani, nil
}
func (s *MALAnimeService) GetByName(ctx context.Context, name string, _ string) ([]Target, error) {
resp, err := s.client.GetAnimesByName(ctx, name)
if err != nil {
return nil, fmt.Errorf("error getting anime by name: %w", err)
}
// false: search results are forward-sync targets, not reverse-sync sources
return newTargetsFromAnimes(newAnimesFromMalAnimes(resp, false)), nil
}
func (s *MALAnimeService) Update(ctx context.Context, id TargetID, src Source, _ string) error {
a, ok := src.(Anime)
if !ok {
return errors.New("source is not an anime")
}
err := s.client.UpdateAnimeByIDAndOptions(ctx, int(id), a.GetUpdateOptions())
if err != nil {
return fmt.Errorf("error updating anime by id and options: %w", err)
}
return nil
}
// MALMangaService implements MediaService for MAL manga operations.
type MALMangaService struct {
client *MyAnimeListClient
}
// NewMALMangaService creates a new MAL manga service.
func NewMALMangaService(client *MyAnimeListClient) *MALMangaService {
return &MALMangaService{client: client}
}
func (s *MALMangaService) GetByID(ctx context.Context, id TargetID, _ string) (Target, error) {
resp, err := s.client.GetMangaByID(ctx, int(id))
if err != nil {
return nil, fmt.Errorf("error getting manga by id: %w", err)
}
// false: MALMangaService is only used in forward sync (AniList→MAL)
manga, err := newMangaFromMalManga(*resp, false)
if err != nil {
return nil, fmt.Errorf("error creating manga from mal manga: %w", err)
}
return manga, nil
}
func (s *MALMangaService) GetByName(ctx context.Context, name string, _ string) ([]Target, error) {
resp, err := s.client.GetMangasByName(ctx, name)
if err != nil {
return nil, fmt.Errorf("error getting manga by name: %w", err)
}
// false: search results are forward-sync targets, not reverse-sync sources
return newTargetsFromMangas(newMangasFromMalMangas(resp, false)), nil
}
func (s *MALMangaService) Update(ctx context.Context, id TargetID, src Source, _ string) error {
m, ok := src.(Manga)
if !ok {
return errors.New("source is not a manga")
}
err := s.client.UpdateMangaByIDAndOptions(ctx, int(id), m.GetUpdateOptions())
if err != nil {
return fmt.Errorf("error updating manga by id and options: %w", err)
}
return nil
}
// AniListAnimeService implements MediaServiceWithMALID for AniList anime operations.
// Reverse=true marks returned entries as reverse-sync targets (IDAnilist as target ID).
type AniListAnimeService struct {
client *AnilistClient
scoreFormat verniy.ScoreFormat
Reverse bool
}
// NewAniListAnimeService creates a new AniList anime service.
// reverse=true for MAL→AniList direction.
func NewAniListAnimeService(client *AnilistClient, scoreFormat verniy.ScoreFormat, reverse bool) *AniListAnimeService {
return &AniListAnimeService{client: client, scoreFormat: scoreFormat, Reverse: reverse}
}
func (s *AniListAnimeService) GetByID(ctx context.Context, id TargetID, _ string) (Target, error) {
resp, err := s.client.GetAnimeByID(ctx, int(id))
if err != nil {
return nil, fmt.Errorf("error getting anilist anime by id: %w", err)
}
ani, err := newAnimeFromVerniyMedia(*resp, s.Reverse)
if err != nil {
return nil, fmt.Errorf("error creating anime from anilist media: %w", err)
}
return ani, nil
}
func (s *AniListAnimeService) GetByName(ctx context.Context, name string, _ string) ([]Target, error) {
resp, err := s.client.GetAnimesByName(ctx, name)
if err != nil {
return nil, fmt.Errorf("error getting anilist anime by name: %w", err)
}
return newTargetsFromAnimes(newAnimesFromVerniyMedias(resp, s.Reverse)), nil
}
func (s *AniListAnimeService) GetByMALID(ctx context.Context, malID int, _ string) (Target, error) {
resp, err := s.client.GetAnimeByMALID(ctx, malID)
if err != nil {
return nil, fmt.Errorf("error getting anilist anime by MAL ID: %w", err)
}
ani, err := newAnimeFromVerniyMedia(*resp, s.Reverse)
if err != nil {
return nil, fmt.Errorf("error creating anime from anilist media: %w", err)
}
return ani, nil
}
func (s *AniListAnimeService) Update(ctx context.Context, id TargetID, src Source, _ string) error {
a, ok := src.(Anime)
if !ok {
return errors.New("source is not an anime")
}
anilistScore := denormalizeScoreForAniList(ctx, a.Score, s.scoreFormat)
var completedAt *time.Time
if a.Status == StatusCompleted {
completedAt = a.FinishedAt
}
err := s.client.UpdateAnimeEntry(
ctx,
int(id),
a.Status.GetAnilistStatus(),
a.Progress,
anilistScore,
a.StartedAt,
completedAt,
)
if err != nil {
return fmt.Errorf("error updating anilist anime: %w", err)
}
return nil
}
// AniListMangaService implements MediaServiceWithMALID for AniList manga operations.
// Reverse=true marks returned entries as reverse-sync targets (IDAnilist as target ID).
type AniListMangaService struct {
client *AnilistClient
scoreFormat verniy.ScoreFormat
Reverse bool
}
// NewAniListMangaService creates a new AniList manga service.
// reverse=true for MAL→AniList direction.
func NewAniListMangaService(client *AnilistClient, scoreFormat verniy.ScoreFormat, reverse bool) *AniListMangaService {
return &AniListMangaService{client: client, scoreFormat: scoreFormat, Reverse: reverse}
}
func (s *AniListMangaService) GetByID(ctx context.Context, id TargetID, _ string) (Target, error) {
resp, err := s.client.GetMangaByID(ctx, int(id))
if err != nil {
return nil, fmt.Errorf("error getting anilist manga by id: %w", err)
}
manga, err := newMangaFromVerniyMedia(*resp, s.Reverse)
if err != nil {
return nil, fmt.Errorf("error creating manga from anilist media: %w", err)
}
return manga, nil
}
func (s *AniListMangaService) GetByName(ctx context.Context, name string, _ string) ([]Target, error) {
resp, err := s.client.GetMangasByName(ctx, name)
if err != nil {
return nil, fmt.Errorf("error getting anilist manga by name: %w", err)
}
return newTargetsFromMangas(newMangasFromVerniyMedias(resp, s.Reverse)), nil
}
func (s *AniListMangaService) GetByMALID(ctx context.Context, malID int, _ string) (Target, error) {
resp, err := s.client.GetMangaByMALID(ctx, malID)
if err != nil {
return nil, fmt.Errorf("error getting anilist manga by MAL ID: %w", err)
}
manga, err := newMangaFromVerniyMedia(*resp, s.Reverse)
if err != nil {
return nil, fmt.Errorf("error creating manga from anilist media: %w", err)
}
return manga, nil
}
func (s *AniListMangaService) Update(ctx context.Context, id TargetID, src Source, _ string) error {
m, ok := src.(Manga)
if !ok {
return errors.New("source is not a manga")
}
anilistScore := denormalizeMangaScoreForAniList(ctx, m.Score, s.scoreFormat)
var completedAt *time.Time
if m.Status == MangaStatusCompleted {
completedAt = m.FinishedAt
}
err := s.client.UpdateMangaEntry(
ctx,
int(id),
m.Status.GetAnilistStatus(),
m.Progress,
m.ProgressVolumes,
anilistScore,
m.StartedAt,
completedAt,
)
if err != nil {
return fmt.Errorf("error updating anilist manga: %w", err)
}
return nil
}