-
Notifications
You must be signed in to change notification settings - Fork 38
/
offices.go
73 lines (59 loc) · 2.66 KB
/
offices.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package adminapi
import (
"fmt"
"github.com/go-openapi/runtime/middleware"
"github.com/transcom/mymove/pkg/appcontext"
transportation_officesop "github.com/transcom/mymove/pkg/gen/adminapi/adminoperations/transportation_offices"
"github.com/transcom/mymove/pkg/gen/adminmessages"
"github.com/transcom/mymove/pkg/handlers"
"github.com/transcom/mymove/pkg/models"
"github.com/transcom/mymove/pkg/services"
"github.com/transcom/mymove/pkg/services/query"
)
func payloadForOfficeModel(o models.TransportationOffice) *adminmessages.TransportationOffice {
return &adminmessages.TransportationOffice{
ID: handlers.FmtUUID(o.ID),
Name: handlers.FmtString(o.Name),
Address: payloadForAddressModel(&o.Address),
Gbloc: o.Gbloc,
PhoneLines: payloadForPhoneLines(o.PhoneLines),
Latitude: o.Latitude,
Longitude: o.Longitude,
}
}
// IndexOfficesHandler returns a list of office users via GET /office_users
type IndexOfficesHandler struct {
handlers.HandlerConfig
services.OfficeListFetcher
services.NewQueryFilter
services.NewPagination
}
var officesFilterConverters = map[string]func(string) []services.QueryFilter{
"q": func(content string) []services.QueryFilter {
return []services.QueryFilter{query.NewQueryFilter("name", "ILIKE", fmt.Sprintf("%%%s%%", content))}
},
}
// Handle retrieves a list of office users
func (h IndexOfficesHandler) Handle(params transportation_officesop.IndexOfficesParams) middleware.Responder {
return h.AuditableAppContextFromRequestWithErrors(params.HTTPRequest,
func(appCtx appcontext.AppContext) (middleware.Responder, error) {
// Here is where NewQueryFilter will be used to create Filters from the 'filter' query param
queryFilters := generateQueryFilters(appCtx.Logger(), params.Filter, officesFilterConverters)
pagination := h.NewPagination(params.Page, params.PerPage)
ordering := query.NewQueryOrder(params.Sort, params.Order)
offices, err := h.OfficeListFetcher.FetchOfficeList(appCtx, queryFilters, nil, pagination, ordering)
if err != nil {
return handlers.ResponseForError(appCtx.Logger(), err), err
}
totalOfficesCount, err := h.OfficeListFetcher.FetchOfficeCount(appCtx, queryFilters)
if err != nil {
return handlers.ResponseForError(appCtx.Logger(), err), err
}
queriedOfficesCount := len(offices)
payload := make(adminmessages.TransportationOffices, queriedOfficesCount)
for i, s := range offices {
payload[i] = payloadForOfficeModel(s)
}
return transportation_officesop.NewIndexOfficesOK().WithContentRange(fmt.Sprintf("offices %d-%d/%d", pagination.Offset(), pagination.Offset()+queriedOfficesCount, totalOfficesCount)).WithPayload(payload), nil
})
}