submissions: implement sort functionality for listSubmissions
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
Quaternions 2025-01-13 05:06:19 -08:00
parent a39e2892ef
commit 28990e2dbe
4 changed files with 43 additions and 7 deletions

View File

@ -9,6 +9,7 @@ import (
var ( var (
ErrNotExist = errors.New("resource does not exist") ErrNotExist = errors.New("resource does not exist")
ErroNoRowsAffected = errors.New("query did not affect any rows") ErroNoRowsAffected = errors.New("query did not affect any rows")
ErrInvalidSubmissionListSort = errors.New("invalid submission list sort parameter [1,2,3,4]")
) )
type Datastore interface { type Datastore interface {
@ -25,7 +26,7 @@ type Submissions interface {
IfStatusThenUpdate(ctx context.Context, id int64, statuses []model.Status, values OptionalMap) error 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) IfStatusThenUpdateAndGet(ctx context.Context, id int64, statuses []model.Status, values OptionalMap) (model.Submission, error)
Delete(ctx context.Context, id int64) 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 { type Scripts interface {

View File

@ -99,9 +99,32 @@ func (env *Submissions) Delete(ctx context.Context, id int64) error {
return nil 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 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 return nil, err
} }

View File

@ -22,6 +22,16 @@ const (
StatusUnderConstruction Status = 0 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 { type Submission struct {
ID int64 `gorm:"primaryKey"` ID int64 `gorm:"primaryKey"`
DisplayName string DisplayName string

View File

@ -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{ creation_submissions, err := svc.DB.Submissions().List(ctx, filter, model.Page{
Number: 1, Number: 1,
Size: int32(CreationPhaseSubmissionsLimit), Size: int32(CreationPhaseSubmissionsLimit),
}) },model.SubmissionListSortDisabled)
if err != nil { if err != nil {
return nil, err 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{ active_submissions, err := svc.DB.Submissions().List(ctx, filter, model.Page{
Number: 1, Number: 1,
Size: 1, Size: 1,
}) },model.SubmissionListSortDisabled)
if err != nil { if err != nil {
return nil, err 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{ active_submissions, err := svc.DB.Submissions().List(ctx, filter, model.Page{
Number: 1, Number: 1,
Size: 1, Size: 1,
}) },model.SubmissionListSortDisabled)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -167,10 +167,12 @@ func (svc *Service) ListSubmissions(ctx context.Context, params api.ListSubmissi
filter.Add("game_id", params.GameID.Value) 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{ items, err := svc.DB.Submissions().List(ctx, filter, model.Page{
Number: params.Page, Number: params.Page,
Size: params.Limit, Size: params.Limit,
}) },sort)
if err != nil { if err != nil {
return nil, err return nil, err
} }