@@ -12,6 +12,8 @@ import (
12
12
"github.com/bufbuild/protovalidate-go"
13
13
"github.com/sirupsen/logrus"
14
14
"github.com/volatiletech/sqlboiler/v4/boil"
15
+ "github.com/volatiletech/sqlboiler/v4/queries/qm"
16
+ "google.golang.org/protobuf/types/known/timestamppb"
15
17
16
18
"github.com/kevinmichaelchen/temporal-saga-grpc/cmd/svc/license/models"
17
19
)
@@ -74,3 +76,52 @@ func (s *Service) CreateLicense(
74
76
75
77
return out , nil
76
78
}
79
+
80
+ // GetLicense - Gets a License.
81
+ func (s * Service ) GetLicense (
82
+ ctx context.Context ,
83
+ req * connect.Request [licensev1beta1.GetLicenseRequest ],
84
+ ) (* connect.Response [licensev1beta1.GetLicenseResponse ], error ) {
85
+ record , err := models .FindLicense (ctx , s .db , req .Msg .GetId ())
86
+ if err != nil {
87
+ return nil , fmt .Errorf ("unable to retrieve item: %w" , err )
88
+ }
89
+
90
+ return connect .NewResponse (& licensev1beta1.GetLicenseResponse {
91
+ License : & licensev1beta1.License {
92
+ Id : record .ID ,
93
+ Start : timestamppb .New (record .StartTime ),
94
+ End : timestamppb .New (record .EndTime ),
95
+ UserId : record .UserID ,
96
+ },
97
+ }), nil
98
+ }
99
+
100
+ // ListLicenses - Lists licenses.
101
+ func (s * Service ) ListLicenses (
102
+ ctx context.Context ,
103
+ req * connect.Request [licensev1beta1.ListLicensesRequest ],
104
+ ) (* connect.Response [licensev1beta1.ListLicensesResponse ], error ) {
105
+ var mods []qm.QueryMod
106
+ // TODO account for parent and page_token fields
107
+ mods = append (mods , qm .Limit (max (1 , int (req .Msg .GetPageSize ()))))
108
+
109
+ items , err := models .Licenses (mods ... ).All (ctx , s .db )
110
+ if err != nil {
111
+ return nil , fmt .Errorf ("unable to retrieve items: %w" , err )
112
+ }
113
+
114
+ var licenses []* licensev1beta1.License
115
+ for _ , item := range items {
116
+ licenses = append (licenses , & licensev1beta1.License {
117
+ Id : item .ID ,
118
+ Start : timestamppb .New (item .StartTime ),
119
+ End : timestamppb .New (item .EndTime ),
120
+ UserId : item .UserID ,
121
+ })
122
+ }
123
+
124
+ return connect .NewResponse (& licensev1beta1.ListLicensesResponse {
125
+ Licenses : licenses ,
126
+ }), nil
127
+ }
0 commit comments