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

Commit be1225e

Browse files
committed
Impl InvitationsServiceServer
1 parent 6df9e27 commit be1225e

File tree

3 files changed

+60
-10
lines changed

3 files changed

+60
-10
lines changed

app/server/invitations_server.go

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,34 @@ type invitationServiceServerImpl struct {
4343
}
4444

4545
func (s *invitationServiceServerImpl) ListInvitations(ctx context.Context, req *api_pb.ListInvitationsRequest) (*api_pb.ListInvitationsResponse, error) {
46-
// TODO: Not yet implemented.
47-
return nil, status.Error(codes.Unimplemented, "TODO: You should implement it!")
46+
_, err := s.getAdmin(ctx)
47+
if err != nil {
48+
return nil, err
49+
}
50+
51+
is := s.InvitationStore(ctx)
52+
invs, err := is.ListInvitations()
53+
if err != nil {
54+
return nil, errors.WithStack(err)
55+
}
56+
57+
resp := &api_pb.ListInvitationsResponse{
58+
Invitations: invitationsToResponse(invs),
59+
}
60+
return resp, nil
4861
}
4962

5063
func (s *invitationServiceServerImpl) GetInvitation(ctx context.Context, req *api_pb.GetInvitationRequest) (*api_pb.Invitation, error) {
51-
// TODO: Not yet implemented.
52-
return nil, status.Error(codes.Unimplemented, "TODO: You should implement it!")
64+
is := s.InvitationStore(ctx)
65+
inv, err := is.GetInvitation(req.GetToken())
66+
if err != nil {
67+
if errors.Cause(err) == sql.ErrNoRows {
68+
return nil, util.ErrNotFound
69+
}
70+
return nil, err
71+
}
72+
73+
return invitationToResponse(inv), nil
5374
}
5475

5576
func (s *invitationServiceServerImpl) CreateInvitation(ctx context.Context, req *api_pb.CreateInvitationRequest) (*api_pb.Invitation, error) {
@@ -83,8 +104,21 @@ func (s *invitationServiceServerImpl) CreateInvitation(ctx context.Context, req
83104
}
84105

85106
func (s *invitationServiceServerImpl) DeleteInvitation(ctx context.Context, req *api_pb.DeleteInvitationRequest) (*empty.Empty, error) {
86-
// TODO: Not yet implemented.
87-
return nil, status.Error(codes.Unimplemented, "TODO: You should implement it!")
107+
_, err := s.getAdmin(ctx)
108+
if err != nil {
109+
return nil, err
110+
}
111+
112+
is := s.InvitationStore(ctx)
113+
err = is.DeleteInvitation(int64(req.GetInvitationId()))
114+
if err != nil {
115+
if errors.Cause(err) == sql.ErrNoRows {
116+
return nil, util.ErrNotFound
117+
}
118+
return nil, err
119+
}
120+
121+
return &empty.Empty{}, nil
88122
}
89123

90124
func (s *invitationServiceServerImpl) getAdmin(ctx context.Context) (*record.User, error) {
@@ -105,6 +139,15 @@ func (s *invitationServiceServerImpl) getAdmin(ctx context.Context) (*record.Use
105139
return u, nil
106140
}
107141

142+
func invitationsToResponse(invs []*record.Invitation) []*api_pb.Invitation {
143+
resp := make([]*api_pb.Invitation, 0, len(invs))
144+
for _, i := range invs {
145+
resp = append(resp, invitationToResponse(i))
146+
}
147+
148+
return resp
149+
}
150+
108151
func invitationToResponse(inv *record.Invitation) *api_pb.Invitation {
109152
return &api_pb.Invitation{
110153
InvitationId: uint32(inv.ID),

infra/store/invitation/invitation_store.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,12 @@ func (s *invitationStoreImpl) ListInvitations() ([]*record.Invitation, error) {
4646
return invs, nil
4747
}
4848

49-
func (s *invitationStoreImpl) GetInvitation(id int64) (*record.Invitation, error) {
50-
inv, err := record.FindInvitation(s.ctx, s.db, id)
49+
func (s *invitationStoreImpl) GetInvitation(code string) (*record.Invitation, error) {
50+
mods := []qm.QueryMod{
51+
record.InvitationWhere.Code.EQ(code),
52+
}
53+
54+
inv, err := record.Invitations(mods...).One(s.ctx, s.db)
5155
if err != nil {
5256
return nil, errors.WithStack(err)
5357
}
@@ -79,7 +83,10 @@ func (s *invitationStoreImpl) CreateInvitation(inviter model.UserID, email strin
7983
}
8084

8185
func (s *invitationStoreImpl) DeleteInvitation(id int64) error {
82-
_, err := record.Invitations(record.InvitationWhere.ID.EQ(id)).DeleteAll(s.ctx, s.db)
86+
n, err := record.Invitations(record.InvitationWhere.ID.EQ(id)).DeleteAll(s.ctx, s.db)
87+
if n == 0 {
88+
return errors.WithStack(sql.ErrNoRows)
89+
}
8390
return errors.WithStack(err)
8491
}
8592

infra/store/invitation_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
// InvitationStore provides invitations
99
type InvitationStore interface {
1010
ListInvitations() ([]*record.Invitation, error)
11-
GetInvitation(id int64) (*record.Invitation, error)
11+
GetInvitation(code string) (*record.Invitation, error)
1212
CreateInvitation(inviter model.UserID, email string) (*record.Invitation, error)
1313
DeleteInvitation(id int64) error
1414
}

0 commit comments

Comments
 (0)