|
| 1 | +/* |
| 2 | + * Copyright (c) 2025. protobox |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package example |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "time" |
| 22 | + |
| 23 | + "github.com/google/uuid" |
| 24 | + "google.golang.org/protobuf/types/known/timestamppb" |
| 25 | + |
| 26 | + "github.com/notjustmoney/protobox" |
| 27 | + exampleeventv1 "github.com/notjustmoney/protobox/examples/event-dispatching/gen/proto/event/example/v1" |
| 28 | + examplerpcv1 "github.com/notjustmoney/protobox/examples/event-dispatching/gen/proto/rpc/example/v1" |
| 29 | +) |
| 30 | + |
| 31 | +type CreateExampleHandler struct { |
| 32 | + publisher protobox.Publisher |
| 33 | +} |
| 34 | + |
| 35 | +func NewCreateExampleHandler( |
| 36 | + publisher protobox.Publisher, |
| 37 | +) *CreateExampleHandler { |
| 38 | + return &CreateExampleHandler{ |
| 39 | + publisher: publisher, |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +func (h *CreateExampleHandler) Handle(ctx context.Context, req *examplerpcv1.CreateExampleRequest) (*examplerpcv1.CreateExampleResponse, error) { |
| 44 | + id := uuid.NewString() |
| 45 | + now := time.Now() |
| 46 | + if err := h.publisher.Publish(ctx, &exampleeventv1.ExampleCreated{ |
| 47 | + Id: id, |
| 48 | + Name: req.Name, |
| 49 | + Description: req.Description, |
| 50 | + CreatedAt: timestamppb.New(now), |
| 51 | + }); err != nil { |
| 52 | + return nil, err |
| 53 | + } |
| 54 | + return &examplerpcv1.CreateExampleResponse{ |
| 55 | + Id: id, |
| 56 | + Name: req.Name, |
| 57 | + Description: req.Description, |
| 58 | + CreatedAt: timestamppb.New(now), |
| 59 | + }, nil |
| 60 | +} |
0 commit comments