2020-06-02 15:45:21 +00:00
|
|
|
package usecase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-05 17:49:00 +00:00
|
|
|
"github.com/pkg/errors"
|
2021-05-05 17:32:45 +00:00
|
|
|
"github.com/tribalwarshelp/shared/tw/twmodel"
|
2020-12-25 11:07:54 +00:00
|
|
|
"strings"
|
2020-06-02 15:45:21 +00:00
|
|
|
|
2020-08-09 12:32:46 +00:00
|
|
|
"github.com/tribalwarshelp/api/middleware"
|
2020-06-02 15:45:21 +00:00
|
|
|
"github.com/tribalwarshelp/api/tribe"
|
|
|
|
)
|
|
|
|
|
|
|
|
type usecase struct {
|
|
|
|
repo tribe.Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(repo tribe.Repository) tribe.Usecase {
|
|
|
|
return &usecase{repo}
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:32:45 +00:00
|
|
|
func (ucase *usecase) Fetch(ctx context.Context, cfg tribe.FetchConfig) ([]*twmodel.Tribe, int, error) {
|
2020-11-21 11:01:16 +00:00
|
|
|
if cfg.Filter == nil {
|
2021-05-05 17:32:45 +00:00
|
|
|
cfg.Filter = &twmodel.TribeFilter{}
|
2020-06-02 15:45:21 +00:00
|
|
|
}
|
2021-03-21 06:42:12 +00:00
|
|
|
if !middleware.CanExceedLimit(ctx) && (cfg.Limit > tribe.FetchLimit || cfg.Limit <= 0) {
|
|
|
|
cfg.Limit = tribe.FetchLimit
|
2020-11-21 11:01:16 +00:00
|
|
|
}
|
2021-05-05 18:15:51 +00:00
|
|
|
if len(cfg.Sort) > tribe.MaxOrders {
|
|
|
|
cfg.Sort = cfg.Sort[0:tribe.MaxOrders]
|
|
|
|
}
|
2020-11-21 11:01:16 +00:00
|
|
|
return ucase.repo.Fetch(ctx, cfg)
|
2020-06-02 15:45:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-05 17:32:45 +00:00
|
|
|
func (ucase *usecase) GetByID(ctx context.Context, server string, id int) (*twmodel.Tribe, error) {
|
2020-06-24 13:05:31 +00:00
|
|
|
tribes, _, err := ucase.repo.Fetch(ctx, tribe.FetchConfig{
|
2021-05-05 17:32:45 +00:00
|
|
|
Filter: &twmodel.TribeFilter{
|
2020-11-21 11:01:16 +00:00
|
|
|
ID: []int{id},
|
2020-06-24 13:05:31 +00:00
|
|
|
},
|
2020-11-21 11:01:16 +00:00
|
|
|
Limit: 1,
|
2020-06-24 13:05:31 +00:00
|
|
|
Server: server,
|
2020-11-21 11:01:16 +00:00
|
|
|
Count: false,
|
2021-03-21 12:03:39 +00:00
|
|
|
Select: true,
|
2020-06-02 15:45:21 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-24 13:05:31 +00:00
|
|
|
if len(tribes) == 0 {
|
2021-05-05 17:49:00 +00:00
|
|
|
return nil, errors.Errorf("Tribe (ID: %d) not found.", id)
|
2020-06-02 15:45:21 +00:00
|
|
|
}
|
|
|
|
return tribes[0], nil
|
|
|
|
}
|
2020-12-25 11:07:54 +00:00
|
|
|
|
2021-05-05 17:32:45 +00:00
|
|
|
func (ucase *usecase) SearchTribe(ctx context.Context, cfg tribe.SearchTribeConfig) ([]*twmodel.FoundTribe, int, error) {
|
2020-12-25 11:07:54 +00:00
|
|
|
if "" == strings.TrimSpace(cfg.Version) {
|
2021-05-05 17:49:00 +00:00
|
|
|
return nil, 0, errors.New("Version is required.")
|
2020-12-25 11:07:54 +00:00
|
|
|
}
|
|
|
|
if "" == strings.TrimSpace(cfg.Query) {
|
2021-05-05 17:49:00 +00:00
|
|
|
return nil, 0, errors.New("Query is too ambiguous. You must specify the variable 'query'.")
|
2020-12-25 11:07:54 +00:00
|
|
|
}
|
2021-03-21 06:42:12 +00:00
|
|
|
if !middleware.CanExceedLimit(ctx) && (cfg.Limit > tribe.FetchLimit || cfg.Limit <= 0) {
|
|
|
|
cfg.Limit = tribe.FetchLimit
|
2020-12-25 11:07:54 +00:00
|
|
|
}
|
2021-05-05 18:15:51 +00:00
|
|
|
if len(cfg.Sort) > tribe.MaxOrders {
|
|
|
|
cfg.Sort = cfg.Sort[0:tribe.MaxOrders]
|
|
|
|
}
|
2020-12-25 11:07:54 +00:00
|
|
|
return ucase.repo.SearchTribe(ctx, cfg)
|
|
|
|
}
|