refactor: ennoblements - sort only by createdAt
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/tag Build is passing

This commit is contained in:
Dawid Wysokiński 2022-12-30 09:53:28 +01:00
parent b669bbc2ff
commit e9c0cb716e
Signed by: Kichiyaki
GPG Key ID: B5445E357FB8B892
10 changed files with 19 additions and 30 deletions

View File

@ -195,10 +195,9 @@ func (l listEnnoblementsParamsApplier) applyPagination(q *bun.SelectQuery) *bun.
func ennoblementSortByToColumn(sortBy domain.EnnoblementSortBy) (string, error) {
switch sortBy {
case domain.EnnoblementSortByID:
return "ennoblement.id", nil
case domain.EnnoblementSortByCreatedAt:
return "ennoblement.created_at", nil
default:
return "", fmt.Errorf("%w: %d", domain.ErrUnsupportedSortBy, sortBy)
}
return "", fmt.Errorf("%w: %d", domain.ErrUnsupportedSortBy, sortBy)
}

View File

@ -209,7 +209,7 @@ func TestEnnoblement_List(t *testing.T) {
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{
Pagination: domain.Pagination{
Limit: 1,
@ -217,7 +217,7 @@ func TestEnnoblement_List(t *testing.T) {
},
ServerKeys: []string{"it70"},
Sort: []domain.EnnoblementSort{
{By: domain.EnnoblementSortByID, Direction: domain.SortDirectionDESC},
{By: domain.EnnoblementSortByCreatedAt, Direction: domain.SortDirectionDESC},
},
},
expectedEnnoblements: []expectedEnnoblement{

View File

@ -41,18 +41,16 @@ type CreateEnnoblementParams struct {
type EnnoblementSortBy uint8
const (
EnnoblementSortByID EnnoblementSortBy = iota
EnnoblementSortByCreatedAt
EnnoblementSortByCreatedAt EnnoblementSortBy = iota
)
func NewEnnoblementSortBy(s string) (EnnoblementSortBy, error) {
switch s {
case "id":
return EnnoblementSortByID, nil
case "createdAt":
return EnnoblementSortByCreatedAt, nil
default:
return 0, fmt.Errorf("%w: \"%s\"", ErrUnsupportedSortBy, s)
}
return 0, fmt.Errorf("%w: \"%s\"", ErrUnsupportedSortBy, s)
}
type EnnoblementSort struct {

View File

@ -17,10 +17,6 @@ func TestNewEnnoblementSortBy(t *testing.T) {
s string
output domain.EnnoblementSortBy
}{
{
s: "id",
output: domain.EnnoblementSortByID,
},
{
s: "createdAt",
output: domain.EnnoblementSortByCreatedAt,

View File

@ -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 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 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]
func (e *ennoblement) list(w http.ResponseWriter, r *http.Request) {
var err error

View File

@ -469,7 +469,7 @@ func TestEnnoblement_list(t *testing.T) {
versionCode: version.Code,
key: server.Key,
queryParams: url.Values{
"sort": []string{"id:asc", "test"},
"sort": []string{"createdAt:asc", "test"},
},
expectedStatus: 400,
target: &model.ErrorResp{},
@ -490,7 +490,7 @@ func TestEnnoblement_list(t *testing.T) {
versionCode: version.Code,
key: server.Key,
queryParams: url.Values{
"sort": []string{"id:asc", "test:asc"},
"sort": []string{"createdAt:asc", "test:asc"},
},
expectedStatus: 400,
target: &model.ErrorResp{},
@ -511,7 +511,7 @@ func TestEnnoblement_list(t *testing.T) {
versionCode: version.Code,
key: server.Key,
queryParams: url.Values{
"sort": []string{"id:asc2"},
"sort": []string{"createdAt:asc2"},
},
expectedStatus: 400,
target: &model.ErrorResp{},

View File

@ -13,7 +13,7 @@ import (
const (
ennoblementChunkSize = 1000
ennoblementMaxLimit = 200
ennoblementSortMaxLen = 2
ennoblementSortMaxLen = 1
)
var (
@ -97,7 +97,7 @@ func (e *Ennoblement) ListCountWithRelations(
if len(params.Sort) == 0 {
params.Sort = []domain.EnnoblementSort{
{
By: domain.EnnoblementSortByID,
By: domain.EnnoblementSortByCreatedAt,
Direction: domain.SortDirectionASC,
},
}

View File

@ -185,7 +185,7 @@ func TestEnnoblement_ListCountWithRelations(t *testing.T) {
if len(sort) == 0 {
return []domain.EnnoblementSort{
{
By: domain.EnnoblementSortByID,
By: domain.EnnoblementSortByCreatedAt,
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{
Sort: []domain.EnnoblementSort{
{
By: domain.EnnoblementSortByID,
Direction: domain.SortDirectionASC,
},
{
By: domain.EnnoblementSortByCreatedAt,
Direction: domain.SortDirectionASC,
@ -287,7 +283,7 @@ func TestEnnoblement_ListCountWithRelations(t *testing.T) {
expectedErr: domain.ValidationError{
Field: "sort",
Err: domain.MaxLengthError{
Max: 2,
Max: 1,
},
},
},

View File

@ -5,7 +5,7 @@ go 1.19
require (
github.com/golangci/golangci-lint v1.50.1
github.com/maxbrunsfeld/counterfeiter/v6 v6.5.0
github.com/swaggo/swag v1.8.8
github.com/swaggo/swag v1.8.7
)
require (

View File

@ -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/subosito/gotenv v1.4.1 h1:jyEFiXpy21Wm81FBN71l9VoMMV8H8jG+qIK3GCpY6Qs=
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.8/go.mod h1:ezQVUUhly8dludpVk+/PuwJWvLLanB13ygV5Pr9enSk=
github.com/swaggo/swag v1.8.7 h1:2K9ivTD3teEO+2fXV6zrZKDqk5IuU2aJtBDo8U7omWU=
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/go.mod h1:yHp0ai0Z9gUljN3o0xMhYJnH/IcvkdTBOX2fmJ93JEM=
github.com/tenntenn/modver v1.0.1 h1:2klLppGhDgzJrScMpkj9Ujy3rXPUspSjAcev9tSEBgA=