From 28990e2dbe8d08684cef3fdc581c3e21e7fd5492 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Mon, 13 Jan 2025 05:06:19 -0800 Subject: [PATCH] submissions: implement sort functionality for listSubmissions --- pkg/datastore/datastore.go | 3 ++- pkg/datastore/gormstore/submissions.go | 27 ++++++++++++++++++++++++-- pkg/model/submission.go | 10 ++++++++++ pkg/service/submissions.go | 10 ++++++---- 4 files changed, 43 insertions(+), 7 deletions(-) diff --git a/pkg/datastore/datastore.go b/pkg/datastore/datastore.go index fede681..eb7ca73 100644 --- a/pkg/datastore/datastore.go +++ b/pkg/datastore/datastore.go @@ -9,6 +9,7 @@ import ( var ( ErrNotExist = errors.New("resource does not exist") ErroNoRowsAffected = errors.New("query did not affect any rows") + ErrInvalidSubmissionListSort = errors.New("invalid submission list sort parameter [1,2,3,4]") ) type Datastore interface { @@ -25,7 +26,7 @@ type Submissions interface { IfStatusThenUpdate(ctx context.Context, id int64, statuses []model.Status, values OptionalMap) error IfStatusThenUpdateAndGet(ctx context.Context, id int64, statuses []model.Status, values OptionalMap) (model.Submission, error) Delete(ctx context.Context, id int64) error - List(ctx context.Context, filters OptionalMap, page model.Page) ([]model.Submission, error) + List(ctx context.Context, filters OptionalMap, page model.Page, sort model.SubmissionListSort) ([]model.Submission, error) } type Scripts interface { diff --git a/pkg/datastore/gormstore/submissions.go b/pkg/datastore/gormstore/submissions.go index 8066511..b7073ab 100644 --- a/pkg/datastore/gormstore/submissions.go +++ b/pkg/datastore/gormstore/submissions.go @@ -99,9 +99,32 @@ func (env *Submissions) Delete(ctx context.Context, id int64) error { return nil } -func (env *Submissions) List(ctx context.Context, filters datastore.OptionalMap, page model.Page) ([]model.Submission, error) { +func (env *Submissions) List(ctx context.Context, filters datastore.OptionalMap, page model.Page, sort model.SubmissionListSort) ([]model.Submission, error) { var maps []model.Submission - if err := env.db.Where(filters.Map()).Offset(int((page.Number - 1) * page.Size)).Limit(int(page.Size)).Find(&maps).Error; err != nil { + + db := env.db + + switch sort { + case model.SubmissionListSortDisabled: + // No sort + break + case model.SubmissionListSortDisplayNameAscending: + db=db.Order("display_name ASC") + break + case model.SubmissionListSortDisplayNameDescending: + db=db.Order("display_name DESC") + break + case model.SubmissionListSortDateAscending: + db=db.Order("created_at ASC") + break + case model.SubmissionListSortDateDescending: + db=db.Order("created_at DESC") + break + default: + return nil, datastore.ErrInvalidSubmissionListSort + } + + if err := db.Where(filters.Map()).Offset(int((page.Number - 1) * page.Size)).Limit(int(page.Size)).Find(&maps).Error; err != nil { return nil, err } diff --git a/pkg/model/submission.go b/pkg/model/submission.go index 3cc74b1..b144a87 100644 --- a/pkg/model/submission.go +++ b/pkg/model/submission.go @@ -22,6 +22,16 @@ const ( StatusUnderConstruction Status = 0 ) +type SubmissionListSort uint32 + +const ( + SubmissionListSortDisabled SubmissionListSort = 0 + SubmissionListSortDisplayNameAscending SubmissionListSort = 1 + SubmissionListSortDisplayNameDescending SubmissionListSort = 2 + SubmissionListSortDateAscending SubmissionListSort = 3 + SubmissionListSortDateDescending SubmissionListSort = 4 +) + type Submission struct { ID int64 `gorm:"primaryKey"` DisplayName string diff --git a/pkg/service/submissions.go b/pkg/service/submissions.go index 1824b6a..3051ac4 100644 --- a/pkg/service/submissions.go +++ b/pkg/service/submissions.go @@ -58,7 +58,7 @@ func (svc *Service) CreateSubmission(ctx context.Context, request *api.Submissio creation_submissions, err := svc.DB.Submissions().List(ctx, filter, model.Page{ Number: 1, Size: int32(CreationPhaseSubmissionsLimit), - }) + },model.SubmissionListSortDisabled) if err != nil { return nil, err } @@ -77,7 +77,7 @@ func (svc *Service) CreateSubmission(ctx context.Context, request *api.Submissio active_submissions, err := svc.DB.Submissions().List(ctx, filter, model.Page{ Number: 1, Size: 1, - }) + },model.SubmissionListSortDisabled) if err != nil { return nil, err } @@ -94,7 +94,7 @@ func (svc *Service) CreateSubmission(ctx context.Context, request *api.Submissio active_submissions, err := svc.DB.Submissions().List(ctx, filter, model.Page{ Number: 1, Size: 1, - }) + },model.SubmissionListSortDisabled) if err != nil { return nil, err } @@ -167,10 +167,12 @@ func (svc *Service) ListSubmissions(ctx context.Context, params api.ListSubmissi filter.Add("game_id", params.GameID.Value) } + sort := model.SubmissionListSort(params.Sort.Or(int32(model.SubmissionListSortDisabled))) + items, err := svc.DB.Submissions().List(ctx, filter, model.Page{ Number: params.Page, Size: params.Limit, - }) + },sort) if err != nil { return nil, err }