2020-06-24 13:25:31 +00:00
|
|
|
package usecase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-05-05 17:32:45 +00:00
|
|
|
"github.com/tribalwarshelp/shared/tw/twmodel"
|
2020-06-24 13:25:31 +00:00
|
|
|
|
2020-08-09 12:32:46 +00:00
|
|
|
"github.com/tribalwarshelp/api/middleware"
|
2020-06-24 13:25:31 +00:00
|
|
|
"github.com/tribalwarshelp/api/tribechange"
|
|
|
|
)
|
|
|
|
|
|
|
|
type usecase struct {
|
|
|
|
repo tribechange.Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(repo tribechange.Repository) tribechange.Usecase {
|
|
|
|
return &usecase{repo}
|
|
|
|
}
|
|
|
|
|
2021-05-05 17:32:45 +00:00
|
|
|
func (ucase *usecase) Fetch(ctx context.Context, cfg tribechange.FetchConfig) ([]*twmodel.TribeChange, 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.TribeChangeFilter{}
|
2020-06-24 13:25:31 +00:00
|
|
|
}
|
2021-03-21 06:42:12 +00:00
|
|
|
if !middleware.CanExceedLimit(ctx) && (cfg.Limit > tribechange.FetchLimit || cfg.Limit <= 0) {
|
|
|
|
cfg.Limit = tribechange.FetchLimit
|
2020-11-21 11:01:16 +00:00
|
|
|
}
|
2021-05-05 18:15:51 +00:00
|
|
|
if len(cfg.Sort) > tribechange.MaxOrders {
|
|
|
|
cfg.Sort = cfg.Sort[0:tribechange.MaxOrders]
|
|
|
|
}
|
2020-11-21 11:01:16 +00:00
|
|
|
return ucase.repo.Fetch(ctx, cfg)
|
2020-06-24 13:25:31 +00:00
|
|
|
}
|