2024-01-04 17:55:26 +00:00
|
|
|
// Package services provides the core business logic for the application.
|
2022-10-30 04:05:38 +00:00
|
|
|
package services
|
|
|
|
|
2023-02-13 19:00:29 +00:00
|
|
|
import (
|
2024-01-18 19:45:42 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/core/currencies"
|
2023-02-13 19:00:29 +00:00
|
|
|
"github.com/hay-kot/homebox/backend/internal/data/repo"
|
|
|
|
)
|
2022-10-30 04:05:38 +00:00
|
|
|
|
|
|
|
type AllServices struct {
|
2023-03-21 19:32:48 +00:00
|
|
|
User *UserService
|
|
|
|
Group *GroupService
|
|
|
|
Items *ItemService
|
|
|
|
BackgroundService *BackgroundService
|
2024-01-18 19:45:42 +00:00
|
|
|
Currencies *currencies.CurrencyRegistry
|
2022-10-30 04:05:38 +00:00
|
|
|
}
|
|
|
|
|
2022-11-13 23:17:55 +00:00
|
|
|
type OptionsFunc func(*options)
|
|
|
|
|
|
|
|
type options struct {
|
|
|
|
autoIncrementAssetID bool
|
2024-01-18 19:45:42 +00:00
|
|
|
currencies []currencies.Currency
|
2022-11-13 23:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func WithAutoIncrementAssetID(v bool) func(*options) {
|
|
|
|
return func(o *options) {
|
|
|
|
o.autoIncrementAssetID = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-18 19:45:42 +00:00
|
|
|
func WithCurrencies(v []currencies.Currency) func(*options) {
|
|
|
|
return func(o *options) {
|
|
|
|
o.currencies = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-13 23:17:55 +00:00
|
|
|
func New(repos *repo.AllRepos, opts ...OptionsFunc) *AllServices {
|
2022-10-30 04:05:38 +00:00
|
|
|
if repos == nil {
|
|
|
|
panic("repos cannot be nil")
|
|
|
|
}
|
|
|
|
|
2024-01-18 19:45:42 +00:00
|
|
|
defaultCurrencies, err := currencies.CollectionCurrencies(
|
|
|
|
currencies.CollectDefaults(),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic("failed to collect default currencies")
|
|
|
|
}
|
|
|
|
|
2022-11-13 23:17:55 +00:00
|
|
|
options := &options{
|
|
|
|
autoIncrementAssetID: true,
|
2024-01-18 19:45:42 +00:00
|
|
|
currencies: defaultCurrencies,
|
2022-11-13 23:17:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
opt(options)
|
|
|
|
}
|
|
|
|
|
2022-10-30 04:05:38 +00:00
|
|
|
return &AllServices{
|
|
|
|
User: &UserService{repos},
|
|
|
|
Group: &GroupService{repos},
|
|
|
|
Items: &ItemService{
|
2022-11-13 23:17:55 +00:00
|
|
|
repo: repos,
|
|
|
|
autoIncrementAssetID: options.autoIncrementAssetID,
|
2022-10-30 04:05:38 +00:00
|
|
|
},
|
2023-03-21 19:32:48 +00:00
|
|
|
BackgroundService: &BackgroundService{repos},
|
2024-01-18 19:45:42 +00:00
|
|
|
Currencies: currencies.NewCurrencyService(options.currencies),
|
2022-10-30 04:05:38 +00:00
|
|
|
}
|
|
|
|
}
|