Skip to content
This repository was archived by the owner on Feb 4, 2021. It is now read-only.

Commit b8dd6d6

Browse files
committed
Impl roles service server
1 parent 072ac20 commit b8dd6d6

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

app/di/store_component.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import (
1515
feedstore "github.com/ProgrammingLab/prolab-accounts/infra/store/feed"
1616
heartbeatstore "github.com/ProgrammingLab/prolab-accounts/infra/store/heartbeat"
1717
profilestore "github.com/ProgrammingLab/prolab-accounts/infra/store/profile"
18+
rolestore "github.com/ProgrammingLab/prolab-accounts/infra/store/role"
1819
sessionstore "github.com/ProgrammingLab/prolab-accounts/infra/store/session"
1920
userstore "github.com/ProgrammingLab/prolab-accounts/infra/store/user"
2021
userblogstore "github.com/ProgrammingLab/prolab-accounts/infra/store/user_blog"
@@ -28,6 +29,7 @@ type StoreComponent interface {
2829
UserBlogStore(ctx context.Context) store.UserBlogStore
2930
FeedStore(ctx context.Context) store.FeedStore
3031
EntryStore(ctx context.Context) store.EntryStore
32+
RoleStore(ctx context.Context) store.RoleStore
3133
HeartbeatStore(ctx context.Context) store.HeartbeatStore
3234
}
3335

@@ -154,6 +156,10 @@ func (s *storeComponentImpl) EntryStore(ctx context.Context) store.EntryStore {
154156
return entrystore.NewEntryStore(ctx, s.db)
155157
}
156158

159+
func (s *storeComponentImpl) RoleStore(ctx context.Context) store.RoleStore {
160+
return rolestore.NewRoleStore(ctx, s.db)
161+
}
162+
157163
func (s *storeComponentImpl) HeartbeatStore(ctx context.Context) store.HeartbeatStore {
158164
return heartbeatstore.NewHeartbeatStore(ctx, s.client, s.cfg)
159165
}

app/run.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ func Run() error {
5757
server.NewUserBlogServiceServer(store),
5858
server.NewEntryServiceServer(store, cfg),
5959
server.NewPingServiceServer(store),
60+
server.NewRoleServiceServer(store),
6061
),
6162
)
6263

app/server/roles_server.go

Lines changed: 47 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@ package server
22

33
import (
44
"context"
5+
"database/sql"
56

6-
"github.com/golang/protobuf/ptypes/empty"
7+
"github.com/ProgrammingLab/prolab-accounts/app/util"
78
"github.com/izumin5210/grapi/pkg/grapiserver"
8-
"google.golang.org/grpc/codes"
9-
"google.golang.org/grpc/status"
9+
"github.com/pkg/errors"
1010

1111
api_pb "github.com/ProgrammingLab/prolab-accounts/api"
12+
"github.com/ProgrammingLab/prolab-accounts/app/di"
13+
"github.com/ProgrammingLab/prolab-accounts/infra/record"
1214
)
1315

1416
// RoleServiceServer is a composite interface of api_pb.RoleServiceServer and grapiserver.Server.
@@ -18,34 +20,60 @@ type RoleServiceServer interface {
1820
}
1921

2022
// 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+
}
2327
}
2428

2529
type roleServiceServerImpl struct {
30+
di.StoreComponent
2631
}
2732

2833
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
3149
}
3250

3351
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+
}
3758

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
4163
}
4264

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
4672
}
4773

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+
}
5179
}

app/server/users_server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func userToResponse(user *record.User, includePrivate bool, cfg *config.Config)
220220

221221
if r := p.R; p.R != nil {
222222
if role := r.Role; role != nil {
223-
u.Role = role.Name.String
223+
u.Role = roleToResponse(role)
224224
}
225225
if dep := r.Department; dep != nil {
226226
u.Department = &type_pb.Department{

0 commit comments

Comments
 (0)