submissions: implement sort functionality for listSubmissions
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
a39e2892ef
commit
28990e2dbe
@ -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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user