/
ecosystem_getter.go
75 lines (59 loc) · 2.07 KB
/
ecosystem_getter.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
package modes
import (
"github.com/IBAX-io/go-ibax/packages/api"
"github.com/IBAX-io/go-ibax/packages/conf"
"github.com/IBAX-io/go-ibax/packages/consts"
"github.com/IBAX-io/go-ibax/packages/storage/sqldb"
"github.com/IBAX-io/go-ibax/packages/types"
log "github.com/sirupsen/logrus"
)
func GetEcosystemGetter() types.EcosystemGetter {
if conf.Config.IsSupportingCLB() {
return CLBEcosystemGetter{}
}
return BCEcosystemGetter{}
}
type BCEcosystemGetter struct {
logger *log.Entry
}
func (ng BCEcosystemGetter) GetEcosystemName(id int64) (string, error) {
ecosystem := &sqldb.Ecosystem{}
found, err := ecosystem.Get(nil, id)
if err != nil {
log.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("on getting ecosystem from db")
return "", err
}
if !found {
log.WithFields(log.Fields{"type": consts.NotFound, "id": id, "error": api.ErrEcosystemNotFound}).Error("ecosystem not found")
return "", err
}
return ecosystem.Name, nil
}
func (g BCEcosystemGetter) GetEcosystemLookup() ([]int64, []string, error) {
return sqldb.GetAllSystemStatesIDs()
}
func (v BCEcosystemGetter) ValidateId(formEcosysID, clientEcosysID int64, le *log.Entry) (int64, error) {
if formEcosysID <= 0 {
return clientEcosysID, nil
}
count, err := sqldb.NewDbTransaction(nil).GetNextID("1_ecosystems")
if err != nil {
le.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("getting next id of ecosystems")
return 0, err
}
if formEcosysID >= count {
le.WithFields(log.Fields{"state_id": formEcosysID, "count": count, "type": consts.ParameterExceeded}).Error("ecosystem is larger then max count")
return 0, api.ErrEcosystemNotFound
}
return formEcosysID, nil
}
type CLBEcosystemGetter struct{}
func (g CLBEcosystemGetter) GetEcosystemLookup() ([]int64, []string, error) {
return []int64{1}, []string{"Platform ecosystem"}, nil
}
func (CLBEcosystemGetter) ValidateId(id, clientID int64, le *log.Entry) (int64, error) {
return consts.DefaultCLB, nil
}
func (ng CLBEcosystemGetter) GetEcosystemName(id int64) (string, error) {
return "Platform ecosystem", nil
}