-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,84 @@ | ||
package services | ||
|
||
import ( | ||
overseerr_go "github.com/devopsarr/overseerr-go/overseerr" | ||
"context" | ||
"fmt" | ||
|
||
"github.com/h3mmy/bloopyboi/bot/internal/database" | ||
"github.com/h3mmy/bloopyboi/bot/internal/log" | ||
"github.com/h3mmy/bloopyboi/bot/internal/models" | ||
"github.com/h3mmy/bloopyboi/ent" | ||
"github.com/h3mmy/bloopyboi/ent/discorduser" | ||
"github.com/h3mmy/bloopyboi/ent/mediarequest" | ||
"go.uber.org/zap" | ||
"go.uber.org/zap/zapcore" | ||
) | ||
|
||
type MediaService struct { | ||
bloopyMeta models.BloopyMeta | ||
logger *zap.Logger | ||
overseerrClient *overseerr_go.APIClient | ||
bookService *BookService | ||
bloopyMeta models.BloopyMeta | ||
logger *zap.Logger | ||
overseerrService *OverseerrService | ||
bookService *BookService | ||
db *ent.Client | ||
dbEnabled bool | ||
} | ||
|
||
func NewMediaService(overseerrGen *OverseerrClientGenerator, bookService *BookService) *MediaService { | ||
func NewMediaService() *MediaService { | ||
lgr := log.NewZapLogger().With( | ||
zapcore.Field{Type: zapcore.StringType, Key: ServiceLoggerFieldKey, String: "media_service"}, | ||
) | ||
return &MediaService{ | ||
bloopyMeta: models.NewBloopyMeta(), | ||
logger: lgr, | ||
overseerrClient: overseerrGen.generateClient(), | ||
bookService: bookService, | ||
bloopyMeta: models.NewBloopyMeta(), | ||
logger: lgr, | ||
overseerrService: nil, | ||
bookService: nil, | ||
dbEnabled: false, | ||
db: nil, | ||
} | ||
} | ||
|
||
func (s *MediaService) WithBookService(bsvc *BookService) { | ||
s.bookService = bsvc | ||
} | ||
|
||
func (s *MediaService) WithOverseerrService(osvc *OverseerrService) { | ||
s.overseerrService = osvc | ||
} | ||
|
||
func (s *MediaService) RefreshDBConnection() error { | ||
if s.dbEnabled { | ||
s.db.Close() | ||
} | ||
dbEnabled := true | ||
dbClient, err := database.Open() | ||
if err != nil { | ||
s.logger.Error("failed to open database", zap.Error(err)) | ||
dbEnabled = false | ||
} | ||
s.db = dbClient | ||
s.dbEnabled = dbEnabled | ||
|
||
return err | ||
} | ||
|
||
func (s *MediaService) GetMediaRequestsForUser(ctx context.Context, discordUserId int) ([]*ent.MediaRequest, error) { | ||
// TODO: Create composite request response?? | ||
|
||
if s.dbEnabled { | ||
requests, err := s.db.MediaRequest. | ||
Query(). | ||
WithBook(). | ||
Where( | ||
mediarequest.HasDiscordUsersWith( | ||
discorduser.DiscordidEQ(string(discordUserId)), | ||
Check failure on line 73 in bot/services/media_service.go GitHub Actions / lint
Check failure on line 73 in bot/services/media_service.go GitHub Actions / tests
Check failure on line 73 in bot/services/media_service.go GitHub Actions / lint
|
||
), | ||
). | ||
All(ctx) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to get book requests for discord user %s: %w", discordUserId, err) | ||
Check failure on line 78 in bot/services/media_service.go GitHub Actions / lint
Check failure on line 78 in bot/services/media_service.go GitHub Actions / tests
Check failure on line 78 in bot/services/media_service.go GitHub Actions / lint
|
||
} | ||
return requests, nil | ||
} | ||
s.logger.Warn("database not enabled") | ||
return nil, nil | ||
} |
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.
This file was deleted.