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