refactor: ennoblements - sort only by createdAt
This commit is contained in:
parent
b669bbc2ff
commit
e9c0cb716e
|
@ -195,10 +195,9 @@ func (l listEnnoblementsParamsApplier) applyPagination(q *bun.SelectQuery) *bun.
|
||||||
|
|
||||||
func ennoblementSortByToColumn(sortBy domain.EnnoblementSortBy) (string, error) {
|
func ennoblementSortByToColumn(sortBy domain.EnnoblementSortBy) (string, error) {
|
||||||
switch sortBy {
|
switch sortBy {
|
||||||
case domain.EnnoblementSortByID:
|
|
||||||
return "ennoblement.id", nil
|
|
||||||
case domain.EnnoblementSortByCreatedAt:
|
case domain.EnnoblementSortByCreatedAt:
|
||||||
return "ennoblement.created_at", nil
|
return "ennoblement.created_at", nil
|
||||||
}
|
default:
|
||||||
return "", fmt.Errorf("%w: %d", domain.ErrUnsupportedSortBy, sortBy)
|
return "", fmt.Errorf("%w: %d", domain.ErrUnsupportedSortBy, sortBy)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -209,7 +209,7 @@ func TestEnnoblement_List(t *testing.T) {
|
||||||
expectedCount: 2,
|
expectedCount: 2,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "ServerKeys=[it70],Sort=[{By=ID,Direction=DESC}],Limit=1,Offset=1",
|
name: "ServerKeys=[it70],Sort=[{By=CreatedAt,Direction=DESC}],Limit=1,Offset=1",
|
||||||
params: domain.ListEnnoblementsParams{
|
params: domain.ListEnnoblementsParams{
|
||||||
Pagination: domain.Pagination{
|
Pagination: domain.Pagination{
|
||||||
Limit: 1,
|
Limit: 1,
|
||||||
|
@ -217,7 +217,7 @@ func TestEnnoblement_List(t *testing.T) {
|
||||||
},
|
},
|
||||||
ServerKeys: []string{"it70"},
|
ServerKeys: []string{"it70"},
|
||||||
Sort: []domain.EnnoblementSort{
|
Sort: []domain.EnnoblementSort{
|
||||||
{By: domain.EnnoblementSortByID, Direction: domain.SortDirectionDESC},
|
{By: domain.EnnoblementSortByCreatedAt, Direction: domain.SortDirectionDESC},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
expectedEnnoblements: []expectedEnnoblement{
|
expectedEnnoblements: []expectedEnnoblement{
|
||||||
|
|
|
@ -41,18 +41,16 @@ type CreateEnnoblementParams struct {
|
||||||
type EnnoblementSortBy uint8
|
type EnnoblementSortBy uint8
|
||||||
|
|
||||||
const (
|
const (
|
||||||
EnnoblementSortByID EnnoblementSortBy = iota
|
EnnoblementSortByCreatedAt EnnoblementSortBy = iota
|
||||||
EnnoblementSortByCreatedAt
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewEnnoblementSortBy(s string) (EnnoblementSortBy, error) {
|
func NewEnnoblementSortBy(s string) (EnnoblementSortBy, error) {
|
||||||
switch s {
|
switch s {
|
||||||
case "id":
|
|
||||||
return EnnoblementSortByID, nil
|
|
||||||
case "createdAt":
|
case "createdAt":
|
||||||
return EnnoblementSortByCreatedAt, nil
|
return EnnoblementSortByCreatedAt, nil
|
||||||
}
|
default:
|
||||||
return 0, fmt.Errorf("%w: \"%s\"", ErrUnsupportedSortBy, s)
|
return 0, fmt.Errorf("%w: \"%s\"", ErrUnsupportedSortBy, s)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
type EnnoblementSort struct {
|
type EnnoblementSort struct {
|
||||||
|
|
|
@ -17,10 +17,6 @@ func TestNewEnnoblementSortBy(t *testing.T) {
|
||||||
s string
|
s string
|
||||||
output domain.EnnoblementSortBy
|
output domain.EnnoblementSortBy
|
||||||
}{
|
}{
|
||||||
{
|
|
||||||
s: "id",
|
|
||||||
output: domain.EnnoblementSortByID,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
s: "createdAt",
|
s: "createdAt",
|
||||||
output: domain.EnnoblementSortByCreatedAt,
|
output: domain.EnnoblementSortByCreatedAt,
|
||||||
|
|
|
@ -40,7 +40,7 @@ type ennoblement struct {
|
||||||
// @Param before query string false "only show items created before the given time, this is a timestamp in RFC 3339 format" format(date-time)
|
// @Param before query string false "only show items created before the given time, this is a timestamp in RFC 3339 format" format(date-time)
|
||||||
// @Param offset query int false "specifies where to start a page" minimum(0) default(0)
|
// @Param offset query int false "specifies where to start a page" minimum(0) default(0)
|
||||||
// @Param limit query int false "page size" minimum(1) maximum(200) default(200)
|
// @Param limit query int false "page size" minimum(1) maximum(200) default(200)
|
||||||
// @Param sort query []string false "format: field:direction, default: [id:asc]" Enums(id:asc,id:desc,createdAt:asc,createdAt:desc)
|
// @Param sort query []string false "format: field:direction, default: [createdAt:asc]" Enums(createdAt:asc,createdAt:desc)
|
||||||
// @Router /versions/{versionCode}/servers/{serverKey}/ennoblements [get]
|
// @Router /versions/{versionCode}/servers/{serverKey}/ennoblements [get]
|
||||||
func (e *ennoblement) list(w http.ResponseWriter, r *http.Request) {
|
func (e *ennoblement) list(w http.ResponseWriter, r *http.Request) {
|
||||||
var err error
|
var err error
|
||||||
|
|
|
@ -469,7 +469,7 @@ func TestEnnoblement_list(t *testing.T) {
|
||||||
versionCode: version.Code,
|
versionCode: version.Code,
|
||||||
key: server.Key,
|
key: server.Key,
|
||||||
queryParams: url.Values{
|
queryParams: url.Values{
|
||||||
"sort": []string{"id:asc", "test"},
|
"sort": []string{"createdAt:asc", "test"},
|
||||||
},
|
},
|
||||||
expectedStatus: 400,
|
expectedStatus: 400,
|
||||||
target: &model.ErrorResp{},
|
target: &model.ErrorResp{},
|
||||||
|
@ -490,7 +490,7 @@ func TestEnnoblement_list(t *testing.T) {
|
||||||
versionCode: version.Code,
|
versionCode: version.Code,
|
||||||
key: server.Key,
|
key: server.Key,
|
||||||
queryParams: url.Values{
|
queryParams: url.Values{
|
||||||
"sort": []string{"id:asc", "test:asc"},
|
"sort": []string{"createdAt:asc", "test:asc"},
|
||||||
},
|
},
|
||||||
expectedStatus: 400,
|
expectedStatus: 400,
|
||||||
target: &model.ErrorResp{},
|
target: &model.ErrorResp{},
|
||||||
|
@ -511,7 +511,7 @@ func TestEnnoblement_list(t *testing.T) {
|
||||||
versionCode: version.Code,
|
versionCode: version.Code,
|
||||||
key: server.Key,
|
key: server.Key,
|
||||||
queryParams: url.Values{
|
queryParams: url.Values{
|
||||||
"sort": []string{"id:asc2"},
|
"sort": []string{"createdAt:asc2"},
|
||||||
},
|
},
|
||||||
expectedStatus: 400,
|
expectedStatus: 400,
|
||||||
target: &model.ErrorResp{},
|
target: &model.ErrorResp{},
|
||||||
|
|
|
@ -13,7 +13,7 @@ import (
|
||||||
const (
|
const (
|
||||||
ennoblementChunkSize = 1000
|
ennoblementChunkSize = 1000
|
||||||
ennoblementMaxLimit = 200
|
ennoblementMaxLimit = 200
|
||||||
ennoblementSortMaxLen = 2
|
ennoblementSortMaxLen = 1
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -97,7 +97,7 @@ func (e *Ennoblement) ListCountWithRelations(
|
||||||
if len(params.Sort) == 0 {
|
if len(params.Sort) == 0 {
|
||||||
params.Sort = []domain.EnnoblementSort{
|
params.Sort = []domain.EnnoblementSort{
|
||||||
{
|
{
|
||||||
By: domain.EnnoblementSortByID,
|
By: domain.EnnoblementSortByCreatedAt,
|
||||||
Direction: domain.SortDirectionASC,
|
Direction: domain.SortDirectionASC,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -185,7 +185,7 @@ func TestEnnoblement_ListCountWithRelations(t *testing.T) {
|
||||||
if len(sort) == 0 {
|
if len(sort) == 0 {
|
||||||
return []domain.EnnoblementSort{
|
return []domain.EnnoblementSort{
|
||||||
{
|
{
|
||||||
By: domain.EnnoblementSortByID,
|
By: domain.EnnoblementSortByCreatedAt,
|
||||||
Direction: domain.SortDirectionASC,
|
Direction: domain.SortDirectionASC,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -267,13 +267,9 @@ func TestEnnoblement_ListCountWithRelations(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "len(params.Sort) > 2",
|
name: "len(params.Sort) > 1",
|
||||||
params: domain.ListEnnoblementsParams{
|
params: domain.ListEnnoblementsParams{
|
||||||
Sort: []domain.EnnoblementSort{
|
Sort: []domain.EnnoblementSort{
|
||||||
{
|
|
||||||
By: domain.EnnoblementSortByID,
|
|
||||||
Direction: domain.SortDirectionASC,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
By: domain.EnnoblementSortByCreatedAt,
|
By: domain.EnnoblementSortByCreatedAt,
|
||||||
Direction: domain.SortDirectionASC,
|
Direction: domain.SortDirectionASC,
|
||||||
|
@ -287,7 +283,7 @@ func TestEnnoblement_ListCountWithRelations(t *testing.T) {
|
||||||
expectedErr: domain.ValidationError{
|
expectedErr: domain.ValidationError{
|
||||||
Field: "sort",
|
Field: "sort",
|
||||||
Err: domain.MaxLengthError{
|
Err: domain.MaxLengthError{
|
||||||
Max: 2,
|
Max: 1,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
@ -5,7 +5,7 @@ go 1.19
|
||||||
require (
|
require (
|
||||||
github.com/golangci/golangci-lint v1.50.1
|
github.com/golangci/golangci-lint v1.50.1
|
||||||
github.com/maxbrunsfeld/counterfeiter/v6 v6.5.0
|
github.com/maxbrunsfeld/counterfeiter/v6 v6.5.0
|
||||||
github.com/swaggo/swag v1.8.8
|
github.com/swaggo/swag v1.8.7
|
||||||
)
|
)
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
|
|
@ -542,8 +542,8 @@ github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PK
|
||||||
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
|
||||||
github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs=
|
github.com/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs=
|
||||||
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
|
github.com/subosito/gotenv v1.4.1/go.mod h1:ayKnFf/c6rvx/2iiLrJUk1e6plDbT3edrFNGqEflhK0=
|
||||||
github.com/swaggo/swag v1.8.8 h1:/GgJmrJ8/c0z4R4hoEPZ5UeEhVGdvsII4JbVDLbR7Xc=
|
github.com/swaggo/swag v1.8.7 h1:2K9ivTD3teEO+2fXV6zrZKDqk5IuU2aJtBDo8U7omWU=
|
||||||
github.com/swaggo/swag v1.8.8/go.mod h1:ezQVUUhly8dludpVk+/PuwJWvLLanB13ygV5Pr9enSk=
|
github.com/swaggo/swag v1.8.7/go.mod h1:ezQVUUhly8dludpVk+/PuwJWvLLanB13ygV5Pr9enSk=
|
||||||
github.com/tdakkota/asciicheck v0.1.1 h1:PKzG7JUTUmVspQTDqtkX9eSiLGossXTybutHwTXuO0A=
|
github.com/tdakkota/asciicheck v0.1.1 h1:PKzG7JUTUmVspQTDqtkX9eSiLGossXTybutHwTXuO0A=
|
||||||
github.com/tdakkota/asciicheck v0.1.1/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM=
|
github.com/tdakkota/asciicheck v0.1.1/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM=
|
||||||
github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA=
|
github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA=
|
||||||
|
|
Reference in New Issue
Block a user