-
Notifications
You must be signed in to change notification settings - Fork 0
/
order.go
52 lines (41 loc) · 1.3 KB
/
order.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package usergrp
import (
"errors"
"net/http"
"github.com/aleury/service/business/core/user"
"github.com/aleury/service/business/core/usersummary"
"github.com/aleury/service/business/data/order"
"github.com/aleury/service/business/sys/validate"
)
var orderByFields = map[string]struct{}{
user.OrderByID: {},
user.OrderByName: {},
user.OrderByEmail: {},
user.OrderByRoles: {},
user.OrderByEnabled: {},
}
func parseOrder(r *http.Request) (order.By, error) {
orderBy, err := order.Parse(r, user.DefaultOrderBy)
if err != nil {
return order.By{}, err
}
if _, exists := orderByFields[orderBy.Field]; !exists {
return order.By{}, validate.NewFieldsError(orderBy.Field, errors.New("order field does not exist"))
}
return orderBy, nil
}
// =============================================================================
var orderBySummaryFields = map[string]struct{}{
usersummary.OrderByUserID: {},
usersummary.OrderByUserName: {},
}
func parseSummaryOrder(r *http.Request) (order.By, error) {
orderBy, err := order.Parse(r, usersummary.DefaultOrderBy)
if err != nil {
return order.By{}, err
}
if _, exists := orderBySummaryFields[orderBy.Field]; !exists {
return order.By{}, validate.NewFieldsError(orderBy.Field, errors.New("order field does not exist"))
}
return orderBy, nil
}