2020-06-02 15:45:21 +00:00
|
|
|
package repository
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/go-pg/pg/v10"
|
2020-12-25 11:07:54 +00:00
|
|
|
"github.com/go-pg/pg/v10/orm"
|
2020-06-02 15:45:21 +00:00
|
|
|
"github.com/tribalwarshelp/api/tribe"
|
|
|
|
"github.com/tribalwarshelp/shared/models"
|
|
|
|
)
|
|
|
|
|
|
|
|
type pgRepository struct {
|
|
|
|
*pg.DB
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPGRepository(db *pg.DB) tribe.Repository {
|
|
|
|
return &pgRepository{db}
|
|
|
|
}
|
|
|
|
|
2020-06-24 13:05:31 +00:00
|
|
|
func (repo *pgRepository) Fetch(ctx context.Context, cfg tribe.FetchConfig) ([]*models.Tribe, int, error) {
|
2020-06-02 15:45:21 +00:00
|
|
|
var err error
|
|
|
|
data := []*models.Tribe{}
|
2020-06-24 13:05:31 +00:00
|
|
|
total := 0
|
2020-11-21 11:01:16 +00:00
|
|
|
query := repo.
|
|
|
|
WithParam("SERVER", pg.Safe(cfg.Server)).
|
|
|
|
Model(&data).
|
|
|
|
Context(ctx).
|
|
|
|
Order(cfg.Sort...).
|
|
|
|
Limit(cfg.Limit).
|
|
|
|
Offset(cfg.Offset)
|
2020-06-24 13:05:31 +00:00
|
|
|
if cfg.Filter != nil {
|
2021-01-06 16:43:02 +00:00
|
|
|
query = query.Apply(cfg.Filter.Where)
|
2020-06-02 15:45:21 +00:00
|
|
|
}
|
|
|
|
|
2020-06-24 13:05:31 +00:00
|
|
|
if cfg.Count {
|
|
|
|
total, err = query.SelectAndCount()
|
|
|
|
} else {
|
|
|
|
err = query.Select()
|
|
|
|
}
|
2020-06-02 15:45:21 +00:00
|
|
|
if err != nil && err != pg.ErrNoRows {
|
2020-06-24 13:05:31 +00:00
|
|
|
if strings.Contains(err.Error(), `relation "`+cfg.Server) {
|
2020-06-02 15:45:21 +00:00
|
|
|
return nil, 0, fmt.Errorf("Server not found")
|
|
|
|
}
|
2021-01-13 19:46:24 +00:00
|
|
|
return nil, 0, fmt.Errorf("Internal server error")
|
2020-06-02 15:45:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data, total, nil
|
|
|
|
}
|
2020-12-25 11:07:54 +00:00
|
|
|
|
|
|
|
func (repo *pgRepository) SearchTribe(ctx context.Context, cfg tribe.SearchTribeConfig) ([]*models.FoundTribe, int, error) {
|
|
|
|
servers := []*models.Server{}
|
|
|
|
if err := repo.
|
|
|
|
Model(&servers).
|
|
|
|
Context(ctx).
|
|
|
|
Column("key").
|
|
|
|
Where("version_code = ?", cfg.Version).
|
|
|
|
Select(); err != nil {
|
2021-01-13 19:46:24 +00:00
|
|
|
return nil, 0, fmt.Errorf("Internal server error")
|
2020-12-25 11:07:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var query *orm.Query
|
|
|
|
res := []*models.FoundTribe{}
|
|
|
|
for _, server := range servers {
|
|
|
|
safeKey := pg.Safe(server.Key)
|
|
|
|
otherQuery := repo.
|
|
|
|
Model().
|
|
|
|
Context(ctx).
|
|
|
|
ColumnExpr("? AS server", server.Key).
|
2020-12-26 15:36:38 +00:00
|
|
|
Column("tribe.id", "tribe.name", "tribe.tag", "tribe.most_points", "tribe.best_rank", "tribe.most_villages").
|
2020-12-25 11:07:54 +00:00
|
|
|
TableExpr("?0.tribes as tribe", safeKey).
|
|
|
|
Where("tribe.tag ILIKE ?0 OR tribe.name ILIKE ?0", cfg.Query)
|
|
|
|
if query == nil {
|
|
|
|
query = otherQuery
|
|
|
|
} else {
|
|
|
|
query = query.UnionAll(otherQuery)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
count := 0
|
|
|
|
if query != nil {
|
|
|
|
base := repo.
|
|
|
|
Model().
|
|
|
|
With("union_q", query).
|
|
|
|
Table("union_q").
|
|
|
|
Limit(cfg.Limit).
|
|
|
|
Offset(cfg.Offset).
|
|
|
|
Order(cfg.Sort...)
|
|
|
|
if cfg.Count {
|
|
|
|
count, err = base.SelectAndCount(&res)
|
|
|
|
} else {
|
|
|
|
err = base.Select(&res)
|
|
|
|
}
|
|
|
|
if err != nil && err != pg.ErrNoRows {
|
2021-01-13 19:46:24 +00:00
|
|
|
return nil, 0, fmt.Errorf("Internal server error")
|
2020-12-25 11:07:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, count, nil
|
|
|
|
}
|