@@ -2,12 +2,10 @@ package ports
22
33import (
44 "context"
5- "errors"
65 "time"
76
87 "github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/genproto/trainer"
98 "github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/trainer/app"
10- "github.com/golang/protobuf/ptypes"
119 "github.com/golang/protobuf/ptypes/empty"
1210 "github.com/golang/protobuf/ptypes/timestamp"
1311 "google.golang.org/grpc/codes"
@@ -23,10 +21,7 @@ func NewGrpcServer(application app.Application) GrpcServer {
2321}
2422
2523func (g GrpcServer ) MakeHourAvailable (ctx context.Context , request * trainer.UpdateHourRequest ) (* empty.Empty , error ) {
26- trainingTime , err := protoTimestampToTime (request .Time )
27- if err != nil {
28- return nil , status .Error (codes .InvalidArgument , "unable to parse time" )
29- }
24+ trainingTime := protoTimestampToTime (request .Time )
3025
3126 if err := g .app .Commands .MakeHoursAvailable .Handle (ctx , []time.Time {trainingTime }); err != nil {
3227 return nil , status .Error (codes .Internal , err .Error ())
@@ -36,10 +31,7 @@ func (g GrpcServer) MakeHourAvailable(ctx context.Context, request *trainer.Upda
3631}
3732
3833func (g GrpcServer ) ScheduleTraining (ctx context.Context , request * trainer.UpdateHourRequest ) (* empty.Empty , error ) {
39- trainingTime , err := protoTimestampToTime (request .Time )
40- if err != nil {
41- return nil , status .Error (codes .InvalidArgument , "unable to parse time" )
42- }
34+ trainingTime := protoTimestampToTime (request .Time )
4335
4436 if err := g .app .Commands .ScheduleTraining .Handle (ctx , trainingTime ); err != nil {
4537 return nil , status .Error (codes .Internal , err .Error ())
@@ -49,10 +41,7 @@ func (g GrpcServer) ScheduleTraining(ctx context.Context, request *trainer.Updat
4941}
5042
5143func (g GrpcServer ) CancelTraining (ctx context.Context , request * trainer.UpdateHourRequest ) (* empty.Empty , error ) {
52- trainingTime , err := protoTimestampToTime (request .Time )
53- if err != nil {
54- return nil , status .Error (codes .InvalidArgument , "unable to parse time" )
55- }
44+ trainingTime := protoTimestampToTime (request .Time )
5645
5746 if err := g .app .Commands .CancelTraining .Handle (ctx , trainingTime ); err != nil {
5847 return nil , status .Error (codes .Internal , err .Error ())
@@ -62,10 +51,7 @@ func (g GrpcServer) CancelTraining(ctx context.Context, request *trainer.UpdateH
6251}
6352
6453func (g GrpcServer ) IsHourAvailable (ctx context.Context , request * trainer.IsHourAvailableRequest ) (* trainer.IsHourAvailableResponse , error ) {
65- trainingTime , err := protoTimestampToTime (request .Time )
66- if err != nil {
67- return nil , status .Error (codes .InvalidArgument , "unable to parse time" )
68- }
54+ trainingTime := protoTimestampToTime (request .Time )
6955
7056 isAvailable , err := g .app .Queries .HourAvailability .Handle (ctx , trainingTime )
7157 if err != nil {
@@ -75,13 +61,6 @@ func (g GrpcServer) IsHourAvailable(ctx context.Context, request *trainer.IsHour
7561 return & trainer.IsHourAvailableResponse {IsAvailable : isAvailable }, nil
7662}
7763
78- func protoTimestampToTime (timestamp * timestamp.Timestamp ) (time.Time , error ) {
79- t , err := ptypes .Timestamp (timestamp )
80- if err != nil {
81- return time.Time {}, errors .New ("unable to parse time" )
82- }
83-
84- t = t .UTC ().Truncate (time .Hour )
85-
86- return t , nil
64+ func protoTimestampToTime (timestamp * timestamp.Timestamp ) time.Time {
65+ return timestamp .AsTime ().UTC ().Truncate (time .Hour )
8766}
0 commit comments