@@ -2,13 +2,15 @@ package server
2
2
3
3
import (
4
4
"context"
5
+ "database/sql"
5
6
6
- "github.com/golang/protobuf/ptypes/empty "
7
+ "github.com/ProgrammingLab/prolab-accounts/app/util "
7
8
"github.com/izumin5210/grapi/pkg/grapiserver"
8
- "google.golang.org/grpc/codes"
9
- "google.golang.org/grpc/status"
9
+ "github.com/pkg/errors"
10
10
11
11
api_pb "github.com/ProgrammingLab/prolab-accounts/api"
12
+ "github.com/ProgrammingLab/prolab-accounts/app/di"
13
+ "github.com/ProgrammingLab/prolab-accounts/infra/record"
12
14
)
13
15
14
16
// RoleServiceServer is a composite interface of api_pb.RoleServiceServer and grapiserver.Server.
@@ -18,34 +20,60 @@ type RoleServiceServer interface {
18
20
}
19
21
20
22
// NewRoleServiceServer creates a new RoleServiceServer instance.
21
- func NewRoleServiceServer () RoleServiceServer {
22
- return & roleServiceServerImpl {}
23
+ func NewRoleServiceServer (store di.StoreComponent ) RoleServiceServer {
24
+ return & roleServiceServerImpl {
25
+ StoreComponent : store ,
26
+ }
23
27
}
24
28
25
29
type roleServiceServerImpl struct {
30
+ di.StoreComponent
26
31
}
27
32
28
33
func (s * roleServiceServerImpl ) ListRoles (ctx context.Context , req * api_pb.ListRolesRequest ) (* api_pb.ListRolesResponse , error ) {
29
- // TODO: Not yet implemented.
30
- return nil , status .Error (codes .Unimplemented , "TODO: You should implement it!" )
34
+ rs := s .RoleStore (ctx )
35
+ roles , err := rs .ListRoles ()
36
+ if err != nil {
37
+ if errors .Cause (err ) == sql .ErrNoRows {
38
+ return & api_pb.ListRolesResponse {
39
+ Roles : rolesToResponse (nil ),
40
+ }, nil
41
+ }
42
+
43
+ return nil , err
44
+ }
45
+
46
+ return & api_pb.ListRolesResponse {
47
+ Roles : rolesToResponse (roles ),
48
+ }, nil
31
49
}
32
50
33
51
func (s * roleServiceServerImpl ) GetRole (ctx context.Context , req * api_pb.GetRoleRequest ) (* api_pb.Role , error ) {
34
- // TODO: Not yet implemented.
35
- return nil , status .Error (codes .Unimplemented , "TODO: You should implement it!" )
36
- }
52
+ rs := s .RoleStore (ctx )
53
+ r , err := rs .GetRole (int64 (req .GetRoleId ()))
54
+ if err != nil {
55
+ if errors .Cause (err ) == sql .ErrNoRows {
56
+ return nil , util .ErrNotFound
57
+ }
37
58
38
- func (s * roleServiceServerImpl ) CreateRole (ctx context.Context , req * api_pb.CreateRoleRequest ) (* api_pb.Role , error ) {
39
- // TODO: Not yet implemented.
40
- return nil , status .Error (codes .Unimplemented , "TODO: You should implement it!" )
59
+ return nil , err
60
+ }
61
+
62
+ return roleToResponse (r ), nil
41
63
}
42
64
43
- func (s * roleServiceServerImpl ) UpdateRole (ctx context.Context , req * api_pb.UpdateRoleRequest ) (* api_pb.Role , error ) {
44
- // TODO: Not yet implemented.
45
- return nil , status .Error (codes .Unimplemented , "TODO: You should implement it!" )
65
+ func rolesToResponse (roles []* record.Role ) []* api_pb.Role {
66
+ resp := make ([]* api_pb.Role , 0 , len (roles ))
67
+ for _ , r := range roles {
68
+ resp = append (resp , roleToResponse (r ))
69
+ }
70
+
71
+ return resp
46
72
}
47
73
48
- func (s * roleServiceServerImpl ) DeleteRole (ctx context.Context , req * api_pb.DeleteRoleRequest ) (* empty.Empty , error ) {
49
- // TODO: Not yet implemented.
50
- return nil , status .Error (codes .Unimplemented , "TODO: You should implement it!" )
74
+ func roleToResponse (role * record.Role ) * api_pb.Role {
75
+ return & api_pb.Role {
76
+ RoleId : uint32 (role .ID ),
77
+ Name : role .Name .String ,
78
+ }
51
79
}
0 commit comments