-
Notifications
You must be signed in to change notification settings - Fork 0
/
character.go
96 lines (91 loc) · 2.26 KB
/
character.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package server
import (
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/motki/core/model"
"github.com/motki/core/proto"
)
func (srv *grpcServer) GetCharacter(ctx context.Context, req *proto.GetCharacterRequest) (resp *proto.CharacterResponse, err error) {
defer func() {
if err != nil {
resp = &proto.CharacterResponse{
Result: errorResult(err),
}
err = nil
}
}()
var char *model.Character
if charID := req.GetCharacterId(); charID != 0 {
char, err = srv.model.GetCharacter(int(charID))
if err != nil {
return nil, err
}
} else {
if req.Token == nil {
return nil, errors.New("token cannot be empty")
}
var role model.Role = model.RoleLogistics
switch req.GetRole() {
case proto.Role_LOGISTICS:
role = model.RoleLogistics
case proto.Role_USER:
role = model.RoleUser
default:
// no op
}
user, err := srv.model.GetUserBySessionKey(req.Token.Identifier)
if err != nil {
return nil, err
}
a, err := srv.model.GetAuthorization(user, role)
if err != nil {
return nil, err
}
char, err = srv.model.GetCharacter(a.CharacterID)
if err != nil {
return nil, err
}
}
return &proto.CharacterResponse{
Result: successResult,
Character: proto.CharacterToProto(char),
}, nil
}
func (srv *grpcServer) GetCorporation(ctx context.Context, req *proto.GetCorporationRequest) (resp *proto.CorporationResponse, err error) {
defer func() {
if err != nil {
resp = &proto.CorporationResponse{
Result: errorResult(err),
}
err = nil
}
}()
var corp *model.Corporation
corp, err = srv.model.GetCorporation(int(req.GetCorporationId()))
if err != nil {
return nil, err
}
return &proto.CorporationResponse{
Result: successResult,
Corporation: proto.CorporationToProto(corp),
}, nil
}
func (srv *grpcServer) GetAlliance(ctx context.Context, req *proto.GetAllianceRequest) (resp *proto.AllianceResponse, err error) {
defer func() {
if err != nil {
resp = &proto.AllianceResponse{
Result: errorResult(err),
}
err = nil
}
}()
var alliance *model.Alliance
alliance, err = srv.model.GetAlliance(int(req.GetAllianceId()))
if err != nil {
return nil, err
}
return &proto.AllianceResponse{
Result: successResult,
Alliance: proto.AllianceToProto(alliance),
}, nil
}