2020-06-02 15:45:21 +00:00
|
|
|
package usecase
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
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/player"
|
2020-06-04 13:06:32 +00:00
|
|
|
"github.com/tribalwarshelp/api/utils"
|
2020-06-02 15:45:21 +00:00
|
|
|
"github.com/tribalwarshelp/shared/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type usecase struct {
|
|
|
|
repo player.Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(repo player.Repository) player.Usecase {
|
|
|
|
return &usecase{repo}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ucase *usecase) Fetch(ctx context.Context, server string, filter *models.PlayerFilter) ([]*models.Player, int, error) {
|
|
|
|
if filter == nil {
|
|
|
|
filter = &models.PlayerFilter{}
|
|
|
|
}
|
2020-08-09 12:32:46 +00:00
|
|
|
if !middleware.MayExceedLimit(ctx) && (filter.Limit > player.PaginationLimit || filter.Limit <= 0) {
|
2020-06-02 15:45:21 +00:00
|
|
|
filter.Limit = player.PaginationLimit
|
|
|
|
}
|
2020-06-04 13:06:32 +00:00
|
|
|
filter.Sort = utils.SanitizeSort(filter.Sort)
|
2020-07-18 09:02:30 +00:00
|
|
|
if filter.TribeFilter != nil {
|
|
|
|
filter.TribeFilter.Sort = utils.SanitizeSort(filter.TribeFilter.Sort)
|
|
|
|
}
|
2020-06-24 13:05:31 +00:00
|
|
|
return ucase.repo.Fetch(ctx, player.FetchConfig{
|
|
|
|
Server: server,
|
|
|
|
Filter: filter,
|
|
|
|
Count: true,
|
|
|
|
})
|
2020-06-02 15:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ucase *usecase) GetByID(ctx context.Context, server string, id int) (*models.Player, error) {
|
2020-06-24 13:05:31 +00:00
|
|
|
players, _, err := ucase.repo.Fetch(ctx, player.FetchConfig{
|
|
|
|
Server: server,
|
|
|
|
Filter: &models.PlayerFilter{
|
|
|
|
ID: []int{id},
|
|
|
|
Limit: 1,
|
|
|
|
},
|
2020-06-02 15:45:21 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2020-06-24 13:05:31 +00:00
|
|
|
if len(players) == 0 {
|
2020-06-18 11:06:55 +00:00
|
|
|
return nil, fmt.Errorf("Player (ID: %d) not found.", id)
|
2020-06-02 15:45:21 +00:00
|
|
|
}
|
|
|
|
return players[0], nil
|
|
|
|
}
|