/
rpc_invite_player_to_game.go
69 lines (56 loc) · 2 KB
/
rpc_invite_player_to_game.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package gapi
import (
"context"
"log"
db "github.com/PlatosCodes/desserted/backend/db/sqlc"
"github.com/PlatosCodes/desserted/backend/pb"
"github.com/lib/pq"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/emptypb"
)
func (server *Server) InvitePlayersToGame(ctx context.Context, req *pb.InvitePlayersToGameRequest) (*emptypb.Empty, error) {
authPayload, err := server.authorizeUser(ctx)
if err != nil {
return nil, unauthenticatedError(err)
}
log.Println("yooohooo:", req)
inviter := req.GetInviterPlayerId()
if authPayload.UserID != inviter {
return nil, status.Errorf(codes.PermissionDenied, "you are not authorized to add a game for this user")
}
creator, err := server.Store.IsUserGameCreator(ctx, db.IsUserGameCreatorParams{
CreatedBy: inviter,
GameID: req.GetGameId(),
})
if err != nil {
if pqErr, ok := err.(*pq.Error); ok {
return nil, status.Errorf(codes.Internal, "database error in checking game creator: %s, code: %s", err, pqErr.Code)
}
return nil, status.Errorf(codes.Internal, "internal server error: %s", err)
}
if !creator {
return nil, status.Errorf(codes.PermissionDenied, "you are not authorized to invite players to this game")
}
invitees := req.GetInviteeUsernames()
log.Println(invitees)
for _, username := range invitees {
arg := db.CreateGameInvitationWithUsernameParams{
InviterPlayerID: inviter,
Username: username,
GameID: req.GetGameId(),
}
if err := server.Store.CreateGameInvitationWithUsername(ctx, arg); err != nil {
if pqErr, ok := err.(*pq.Error); ok {
switch pqErr.Code.Name() {
case "unique_violation":
return nil, status.Errorf(codes.AlreadyExists, "player has already been invited to game: %s", err)
default:
return nil, status.Errorf(codes.Internal, "database error in inviting player to game: %s", err)
}
}
return nil, status.Errorf(codes.Internal, "internal server error: %s", err)
}
}
return &emptypb.Empty{}, nil
}