-
Notifications
You must be signed in to change notification settings - Fork 0
/
location.go
65 lines (61 loc) · 1.5 KB
/
location.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
package server
import (
"golang.org/x/net/context"
"github.com/motki/core/model"
"github.com/motki/core/proto"
"github.com/pkg/errors"
)
func (srv *grpcServer) GetLocation(ctx context.Context, req *proto.GetLocationRequest) (resp *proto.LocationResponse, err error) {
defer func() {
if err != nil {
resp = &proto.LocationResponse{
Result: errorResult(err),
}
err = nil
}
}()
if req.Token == nil {
return nil, errors.New("token cannot be empty")
}
ctx, _, err = srv.getAuthorizedContext(req.Token, model.RoleLogistics)
if err != nil {
return nil, err
}
loc, err := srv.model.GetLocation(ctx, int(req.LocationId))
if err != nil {
return nil, err
}
return &proto.LocationResponse{
Result: successResult,
Location: proto.LocationToProto(loc),
}, nil
}
func (srv *grpcServer) QueryLocations(ctx context.Context, req *proto.QueryLocationsRequest) (resp *proto.LocationsResponse, err error) {
defer func() {
if err != nil {
resp = &proto.LocationsResponse{
Result: errorResult(err),
}
err = nil
}
}()
if req.Token == nil {
return nil, errors.New("token cannot be empty")
}
ctx, _, err = srv.getAuthorizedContext(req.Token, model.RoleLogistics)
if err != nil {
return nil, err
}
locs, err := srv.model.QueryLocations(ctx, req.Query)
if err != nil {
return nil, err
}
var res []*proto.Location
for _, l := range locs {
res = append(res, proto.LocationToProto(l))
}
return &proto.LocationsResponse{
Result: successResult,
Location: res,
}, nil
}