Make task sort by string
Some checks reported errors
continuous-integration/drone/pr Build was killed

This commit is contained in:
kolaente 2020-04-21 17:31:52 +02:00
parent f4d1042eb9
commit 70bcef390f
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 8 additions and 14 deletions

View File

@ -573,7 +573,7 @@ func (err ErrTaskAttachmentIsTooLarge) HTTPError() web.HTTPError {
// ErrInvalidSortParam represents an error where the provided sort param is invalid // ErrInvalidSortParam represents an error where the provided sort param is invalid
type ErrInvalidSortParam struct { type ErrInvalidSortParam struct {
SortBy sortProperty SortBy string
} }
// IsErrInvalidSortParam checks if an error is ErrInvalidSortParam. // IsErrInvalidSortParam checks if an error is ErrInvalidSortParam.

View File

@ -107,7 +107,7 @@ func (tf *TaskCollection) ReadAll(a web.Auth, search string, page int, perPage i
var sort = make([]*sortParam, 0, len(tf.SortBy)) var sort = make([]*sortParam, 0, len(tf.SortBy))
for i, s := range tf.SortBy { for i, s := range tf.SortBy {
param := &sortParam{ param := &sortParam{
sortBy: sortProperty(s), sortBy: s,
orderBy: orderAscending, orderBy: orderAscending,
} }
// This checks if tf.OrderBy has an entry with the same index as the current entry from tf.SortBy // This checks if tf.OrderBy has an entry with the same index as the current entry from tf.SortBy

View File

@ -18,12 +18,10 @@ package models
type ( type (
sortParam struct { sortParam struct {
sortBy sortProperty sortBy string
orderBy sortOrder // asc or desc orderBy sortOrder // asc or desc
} }
sortProperty string
sortOrder string sortOrder string
) )
@ -48,10 +46,6 @@ const (
taskPropertyPosition string = "position" taskPropertyPosition string = "position"
) )
func (p sortProperty) String() string {
return string(p)
}
const ( const (
orderInvalid sortOrder = "invalid" orderInvalid sortOrder = "invalid"
orderAscending sortOrder = "asc" orderAscending sortOrder = "asc"
@ -76,5 +70,5 @@ func (sp *sortParam) validate() error {
if sp.orderBy != orderDescending && sp.orderBy != orderAscending { if sp.orderBy != orderDescending && sp.orderBy != orderAscending {
return ErrInvalidSortOrder{OrderBy: sp.orderBy} return ErrInvalidSortOrder{OrderBy: sp.orderBy}
} }
return validateTaskField(string(sp.sortBy)) return validateTaskField(sp.sortBy)
} }

View File

@ -64,7 +64,7 @@ func TestSortParamValidation(t *testing.T) {
t.Run(test, func(t *testing.T) { t.Run(test, func(t *testing.T) {
s := &sortParam{ s := &sortParam{
orderBy: orderAscending, orderBy: orderAscending,
sortBy: sortProperty(test), sortBy: test,
} }
err := s.validate() err := s.validate()
assert.NoError(t, err) assert.NoError(t, err)

View File

@ -161,9 +161,9 @@ func getRawTasksForLists(lists []*List, opts *taskOptions) (tasks []*Task, resul
// Add the id parameter as the last parameter to sorty by default, but only if it is not already passed as the last parameter. // Add the id parameter as the last parameter to sorty by default, but only if it is not already passed as the last parameter.
if len(opts.sortby) == 0 || if len(opts.sortby) == 0 ||
len(opts.sortby) > 0 && opts.sortby[len(opts.sortby)-1].sortBy != sortProperty(taskPropertyID) { len(opts.sortby) > 0 && opts.sortby[len(opts.sortby)-1].sortBy != taskPropertyID {
opts.sortby = append(opts.sortby, &sortParam{ opts.sortby = append(opts.sortby, &sortParam{
sortBy: sortProperty(taskPropertyID), sortBy: taskPropertyID,
orderBy: orderAscending, orderBy: orderAscending,
}) })
} }
@ -177,7 +177,7 @@ func getRawTasksForLists(lists []*List, opts *taskOptions) (tasks []*Task, resul
if err := param.validate(); err != nil { if err := param.validate(); err != nil {
return nil, 0, 0, err return nil, 0, 0, err
} }
orderby += param.sortBy.String() + " " + param.orderBy.String() orderby += param.sortBy + " " + param.orderBy.String()
if (i + 1) < len(opts.sortby) { if (i + 1) < len(opts.sortby) {
orderby += ", " orderby += ", "
} }