-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
updateResource.go
31 lines (27 loc) · 1 KB
/
updateResource.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
package service
import (
"context"
"github.com/plgd-dev/hub/v2/grpc-gateway/pb"
"github.com/plgd-dev/hub/v2/pkg/log"
kitNetGrpc "github.com/plgd-dev/hub/v2/pkg/net/grpc"
"github.com/plgd-dev/hub/v2/resource-aggregate/commands"
"google.golang.org/grpc/codes"
)
func logAndReturnUpdateResourceError(err error) error {
return log.LogAndReturnError(kitNetGrpc.ForwardErrorf(codes.Internal, "cannot update resource: %v", err))
}
func (r *RequestHandler) UpdateResource(ctx context.Context, req *pb.UpdateResourceRequest) (*pb.UpdateResourceResponse, error) {
updateCommand, err := req.ToRACommand(ctx)
if err != nil {
return nil, logAndReturnUpdateResourceError(err)
}
updatedEvent, err := r.resourceAggregateClient.SyncUpdateResource(ctx, "*", updateCommand)
if err != nil {
return nil, logAndReturnUpdateResourceError(err)
}
err = commands.CheckEventContent(updatedEvent)
if err != nil {
return nil, logAndReturnUpdateResourceError(err)
}
return &pb.UpdateResourceResponse{Data: updatedEvent}, nil
}