parent
15dd6b4178
commit
69438a9d0c
pkg/datastore
@ -38,6 +38,7 @@ type Mapfixes interface {
|
|||||||
IfStatusThenUpdateAndGet(ctx context.Context, id int64, statuses []model.MapfixStatus, values OptionalMap) (model.Mapfix, error)
|
IfStatusThenUpdateAndGet(ctx context.Context, id int64, statuses []model.MapfixStatus, values OptionalMap) (model.Mapfix, error)
|
||||||
Delete(ctx context.Context, id int64) error
|
Delete(ctx context.Context, id int64) error
|
||||||
List(ctx context.Context, filters OptionalMap, page model.Page, sort ListSort) ([]model.Mapfix, error)
|
List(ctx context.Context, filters OptionalMap, page model.Page, sort ListSort) ([]model.Mapfix, error)
|
||||||
|
ListRestricted(ctx context.Context, filters OptionalMap, page model.Page, sort ListSort, submitter int64) ([]model.Mapfix, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Operations interface {
|
type Operations interface {
|
||||||
@ -56,6 +57,7 @@ type Submissions interface {
|
|||||||
IfStatusThenUpdateAndGet(ctx context.Context, id int64, statuses []model.SubmissionStatus, values OptionalMap) (model.Submission, error)
|
IfStatusThenUpdateAndGet(ctx context.Context, id int64, statuses []model.SubmissionStatus, 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, sort ListSort) ([]model.Submission, error)
|
List(ctx context.Context, filters OptionalMap, page model.Page, sort ListSort) ([]model.Submission, error)
|
||||||
|
ListRestricted(ctx context.Context, filters OptionalMap, page model.Page, sort ListSort, submitter int64) ([]model.Submission, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
type Scripts interface {
|
type Scripts interface {
|
||||||
|
@ -130,3 +130,40 @@ func (env *Mapfixes) List(ctx context.Context, filters datastore.OptionalMap, pa
|
|||||||
|
|
||||||
return maps, nil
|
return maps, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (env *Mapfixes) ListRestricted(ctx context.Context, filters datastore.OptionalMap, page model.Page, sort datastore.ListSort, submitter int64) ([]model.Mapfix, error) {
|
||||||
|
var maps []model.Mapfix
|
||||||
|
|
||||||
|
db := env.db
|
||||||
|
|
||||||
|
switch sort {
|
||||||
|
case datastore.ListSortDisabled:
|
||||||
|
// No sort
|
||||||
|
break
|
||||||
|
case datastore.ListSortDisplayNameAscending:
|
||||||
|
db=db.Order("display_name ASC")
|
||||||
|
break
|
||||||
|
case datastore.ListSortDisplayNameDescending:
|
||||||
|
db=db.Order("display_name DESC")
|
||||||
|
break
|
||||||
|
case datastore.ListSortDateAscending:
|
||||||
|
db=db.Order("created_at ASC")
|
||||||
|
break
|
||||||
|
case datastore.ListSortDateDescending:
|
||||||
|
db=db.Order("created_at DESC")
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
return nil, datastore.ErrInvalidListSort
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.
|
||||||
|
Where(filters.Map()).
|
||||||
|
Where("status_id NOT IN ? OR submitter = ? AND status_id IN ?",PrivateSubmissions,submitter,PrivateSubmissions).
|
||||||
|
Offset(int((page.Number - 1) * page.Size)).
|
||||||
|
Limit(int(page.Size)).
|
||||||
|
Find(&maps).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return maps, nil
|
||||||
|
}
|
||||||
|
@ -10,6 +10,13 @@ import (
|
|||||||
"gorm.io/gorm/clause"
|
"gorm.io/gorm/clause"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var(
|
||||||
|
PrivateSubmissions = []model.SubmissionStatus{
|
||||||
|
model.SubmissionStatusUnderConstruction,
|
||||||
|
model.SubmissionStatusChangesRequested,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
type Submissions struct {
|
type Submissions struct {
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
}
|
}
|
||||||
@ -130,3 +137,44 @@ func (env *Submissions) List(ctx context.Context, filters datastore.OptionalMap,
|
|||||||
|
|
||||||
return maps, nil
|
return maps, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (env *Submissions) ListRestricted(ctx context.Context, filters datastore.OptionalMap, page model.Page, sort datastore.ListSort, submitter int64) ([]model.Submission, error) {
|
||||||
|
var maps []model.Submission
|
||||||
|
|
||||||
|
db := env.db
|
||||||
|
|
||||||
|
switch sort {
|
||||||
|
case datastore.ListSortDisabled:
|
||||||
|
// No sort
|
||||||
|
break
|
||||||
|
case datastore.ListSortDisplayNameAscending:
|
||||||
|
db=db.Order("display_name ASC")
|
||||||
|
break
|
||||||
|
case datastore.ListSortDisplayNameDescending:
|
||||||
|
db=db.Order("display_name DESC")
|
||||||
|
break
|
||||||
|
case datastore.ListSortDateAscending:
|
||||||
|
db=db.Order("created_at ASC")
|
||||||
|
break
|
||||||
|
case datastore.ListSortDateDescending:
|
||||||
|
db=db.Order("created_at DESC")
|
||||||
|
break
|
||||||
|
default:
|
||||||
|
return nil, datastore.ErrInvalidListSort
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := db.
|
||||||
|
Where(filters.Map()).
|
||||||
|
// In order to see submissions,
|
||||||
|
// at least one of two criteria must be met:
|
||||||
|
// - You are the submitter
|
||||||
|
// - The submission is not under construction / changes requested
|
||||||
|
Where("status_id NOT IN ? OR submitter = ? AND status_id IN ?",PrivateSubmissions,submitter,PrivateSubmissions).
|
||||||
|
Offset(int((page.Number - 1) * page.Size)).
|
||||||
|
Limit(int(page.Size)).
|
||||||
|
Find(&maps).Error; err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return maps, nil
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user