2020-11-06 19:23:12 +00:00
|
|
|
package usecase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-05-05 17:32:45 +00:00
|
|
|
"github.com/tribalwarshelp/shared/tw/twmodel"
|
2020-11-06 19:23:12 +00:00
|
|
|
|
|
|
|
"github.com/tribalwarshelp/api/middleware"
|
|
|
|
"github.com/tribalwarshelp/api/version"
|
|
|
|
)
|
|
|
|
|
|
|
|
type usecase struct {
|
|
|
|
repo version.Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(repo version.Repository) version.Usecase {
|
|
|
|
return &usecase{
|
|
|
|
repo,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:32:45 +00:00
|
|
|
func (ucase *usecase) Fetch(ctx context.Context, cfg version.FetchConfig) ([]*twmodel.Version, int, error) {
|
2020-11-21 11:34:25 +00:00
|
|
|
if cfg.Filter == nil {
|
2021-05-05 17:32:45 +00:00
|
|
|
cfg.Filter = &twmodel.VersionFilter{}
|
2020-11-06 19:23:12 +00:00
|
|
|
}
|
2021-03-21 06:42:12 +00:00
|
|
|
if !middleware.CanExceedLimit(ctx) && (cfg.Limit > version.FetchLimit || cfg.Limit <= 0) {
|
|
|
|
cfg.Limit = version.FetchLimit
|
2020-11-21 11:34:25 +00:00
|
|
|
}
|
|
|
|
return ucase.repo.Fetch(ctx, cfg)
|
2020-11-06 19:23:12 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 17:32:45 +00:00
|
|
|
func (ucase *usecase) GetByCode(ctx context.Context, code twmodel.VersionCode) (*twmodel.Version, error) {
|
2020-11-06 19:23:12 +00:00
|
|
|
versions, _, err := ucase.repo.Fetch(ctx, version.FetchConfig{
|
2021-05-05 17:32:45 +00:00
|
|
|
Filter: &twmodel.VersionFilter{
|
|
|
|
Code: []twmodel.VersionCode{code},
|
2020-11-06 19:23:12 +00:00
|
|
|
},
|
2021-03-21 12:03:39 +00:00
|
|
|
Limit: 1,
|
|
|
|
Select: true,
|
|
|
|
Count: false,
|
2020-11-06 19:23:12 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if len(versions) == 0 {
|
|
|
|
return nil, fmt.Errorf("There is no version with code: %s.", code)
|
|
|
|
}
|
|
|
|
return versions[0], nil
|
|
|
|
}
|