forked from jsteenb2/mess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
grpc.go
68 lines (51 loc) · 2.36 KB
/
grpc.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
package ports
import (
"context"
"time"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/genproto/trainer"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/trainer/app"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/trainer/app/command"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/trainer/app/query"
"github.com/golang/protobuf/ptypes/empty"
"github.com/golang/protobuf/ptypes/timestamp"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type GrpcServer struct {
app app.Application
}
func NewGrpcServer(application app.Application) GrpcServer {
return GrpcServer{app: application}
}
func (g GrpcServer) MakeHourAvailable(ctx context.Context, request *trainer.UpdateHourRequest) (*empty.Empty, error) {
trainingTime := protoTimestampToTime(request.Time)
if err := g.app.Commands.MakeHoursAvailable.Handle(ctx, command.MakeHoursAvailable{Hours: []time.Time{trainingTime}}); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &empty.Empty{}, nil
}
func (g GrpcServer) ScheduleTraining(ctx context.Context, request *trainer.UpdateHourRequest) (*empty.Empty, error) {
trainingTime := protoTimestampToTime(request.Time)
if err := g.app.Commands.ScheduleTraining.Handle(ctx, command.ScheduleTraining{Hour: trainingTime}); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &empty.Empty{}, nil
}
func (g GrpcServer) CancelTraining(ctx context.Context, request *trainer.UpdateHourRequest) (*empty.Empty, error) {
trainingTime := protoTimestampToTime(request.Time)
if err := g.app.Commands.CancelTraining.Handle(ctx, command.CancelTraining{Hour: trainingTime}); err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &empty.Empty{}, nil
}
func (g GrpcServer) IsHourAvailable(ctx context.Context, request *trainer.IsHourAvailableRequest) (*trainer.IsHourAvailableResponse, error) {
trainingTime := protoTimestampToTime(request.Time)
isAvailable, err := g.app.Queries.HourAvailability.Handle(ctx, query.HourAvailability{Hour: trainingTime})
if err != nil {
return nil, status.Error(codes.Internal, err.Error())
}
return &trainer.IsHourAvailableResponse{IsAvailable: isAvailable}, nil
}
func protoTimestampToTime(timestamp *timestamp.Timestamp) time.Time {
return timestamp.AsTime().UTC().Truncate(time.Hour)
}