Skip to content

Commit

Permalink
- The API order parameter is used after verification. (#2149)
Browse files Browse the repository at this point in the history
- JSON-RPC Add ibax.getOpenEcosystem API.
  • Loading branch information
group-monkey authored Mar 14, 2024
1 parent 7e098f2 commit 771519c
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 23 deletions.
24 changes: 20 additions & 4 deletions packages/api/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
package api

import (
"encoding/json"
"errors"
"fmt"
"github.com/IBAX-io/go-ibax/packages/script"

"github.com/IBAX-io/go-ibax/packages/conf"
Expand Down Expand Up @@ -142,14 +144,28 @@ func getListWhereHandler(w http.ResponseWriter, r *http.Request) {
logger := getLogger(r)

var (
err error
table, where string
err error
table, where, order string
)
table, form.Columns, err = checkAccess(params["name"], form.Columns, client)
if err != nil {
errorResponse(w, err)
return
}
if form.Order != "" {
var orderParam any
err = json.Unmarshal([]byte(form.Order), &orderParam)
if err != nil {
errorResponse(w, fmt.Errorf("order unamrshal:%v", err))
return
}
order, err = qb.GetOrder(table, orderParam, true)
if err != nil {
errorResponse(w, err)
return
}
}

//q := sqldb.GetTableQuery(params["name"], client.EcosystemID)
q := sqldb.GetTableListQuery(params["name"], client.EcosystemID)
if len(form.Columns) > 0 {
Expand Down Expand Up @@ -199,8 +215,8 @@ func getListWhereHandler(w http.ResponseWriter, r *http.Request) {
return
}

if len(form.Order) > 0 {
rows, err := q.Order(form.Order).Offset(form.Offset).Limit(form.Limit).Rows()
if len(order) > 0 {
rows, err := q.Order(order).Offset(form.Offset).Limit(form.Limit).Rows()
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err, "table": table}).Error("Getting rows from table")
errorResponse(w, err)
Expand Down
2 changes: 1 addition & 1 deletion packages/service/jsonrpc/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type accountsApi struct {
Mode
}

func NewAccountsApi(m Mode) *accountsApi {
func newAccountsApi(m Mode) *accountsApi {
return &accountsApi{m}
}

Expand Down
2 changes: 1 addition & 1 deletion packages/service/jsonrpc/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type authApi struct {
mode Mode
}

func NewAuthApi(mode Mode) *authApi {
func newAuthApi(mode Mode) *authApi {
a := &authApi{
mode: mode,
}
Expand Down
74 changes: 71 additions & 3 deletions packages/service/jsonrpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/IBAX-io/go-ibax/packages/types"
"github.com/shopspring/decimal"
log "github.com/sirupsen/logrus"
"gorm.io/gorm"
"net/http"
"strconv"
"strings"
Expand All @@ -28,7 +29,7 @@ import (
type blockChainApi struct {
}

func NewBlockChainApi() *blockChainApi {
func newBlockChainApi() *blockChainApi {
return &blockChainApi{}
}

Expand Down Expand Up @@ -924,8 +925,8 @@ func (b *blockChainApi) GetMember(ctx RequestContext, account string, ecosystemI

type ListWhereForm struct {
ListForm
Order string `json:"order"`
Where any `json:"where"`
Order any `json:"order"`
Where any `json:"where"`
}

func (f *ListWhereForm) Validate(r *http.Request) error {
Expand Down Expand Up @@ -970,3 +971,70 @@ func (b *blockChainApi) GetBlocksCountByNode(ctx RequestContext, nodePosition in
return &bm, nil

}

type openSystemInfo struct {
Ecosystem int64 `json:"ecosystem"`
Info *EcosystemInfo `json:"info,omitempty"`
}

type openSystemList struct {
Count int64 `json:"count"`
List []openSystemInfo `json:"list"`
}

// GetOpenEcosystem
// verbosity Type: numeric, optional, default=1
// 1 for a ecosystem id list, and 2 for json object with ecosystem info
func (b *blockChainApi) GetOpenEcosystem(ctx RequestContext, verbosity, offset, limit *int) (*openSystemList, *Error) {
r := ctx.HTTPRequest()
logger := getLogger(r)
form := &paginatorForm{}
if offset != nil {
form.Offset = *offset
}
if limit != nil {
form.Limit = *limit
}
if err := parameterValidator(r, form); err != nil {
return nil, InvalidParamsError(err.Error())
}

var bosity = 1
if verbosity != nil {
bosity = *verbosity
if bosity != 1 && bosity != 2 {
return nil, DefaultError("Not Supported verbosity")
}
}
var (
idList []int64
spfm sqldb.StateParameter
info openSystemList
sqlQuery *gorm.DB
)
sqlQuery = sqldb.GetDB(nil).Table(spfm.TableName()).Where("name = 'free_membership' AND value = '1'").
Select("ecosystem")
err := sqlQuery.Count(&info.Count).Error
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("get open ecosystem count")
return nil, DefaultError(err.Error())
}
err = sqlQuery.Offset(form.Offset).Limit(form.Limit).Order("ecosystem asc").Find(&idList).Error
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err}).Error("get open ecosystem")
return nil, DefaultError(err.Error())
}
info.List = make([]openSystemInfo, len(idList))
for i, ecosystem := range idList {
if bosity == 2 {
var er *Error
info.List[i].Info, er = b.EcosystemInfo(ctx, ecosystem)
if er != nil {
return nil, er
}
}
info.List[i].Ecosystem = ecosystem
}

return &info, nil
}
16 changes: 11 additions & 5 deletions packages/service/jsonrpc/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type commonApi struct {
mode Mode
}

func NewCommonApi(m Mode) *commonApi {
func newCommonApi(m Mode) *commonApi {
return &commonApi{
mode: m,
}
Expand Down Expand Up @@ -194,6 +194,7 @@ type notifyInfo struct {

type KeyInfoResult struct {
Account string `json:"account"`
KeyId string `json:"key_id"`
Ecosystems []*keyEcosystemInfo `json:"ecosystems"`
}

Expand Down Expand Up @@ -300,6 +301,7 @@ func (c *commonApi) GetKeyInfo(ctx RequestContext, accountAddress string) (*KeyI

return &KeyInfoResult{
Account: account,
KeyId: strconv.FormatInt(keyID, 10),
Ecosystems: keysList,
}, nil
}
Expand Down Expand Up @@ -366,13 +368,17 @@ func (c *commonApi) GetList(ctx RequestContext, auth Auth, form *ListWhereForm)
logger := getLogger(r)

var (
err error
table, where string
err error
table, where, order string
)
table, form.Columns, err = checkAccess(form.Name, form.Columns, client)
if err != nil {
return nil, DefaultError(err.Error())
}
order, err = qb.GetOrder(table, form.Order, true)
if err != nil {
return nil, DefaultError(err.Error())
}
var q *gorm.DB
q = sqldb.GetTableListQuery(form.Name, client.EcosystemID)

Expand Down Expand Up @@ -426,8 +432,8 @@ func (c *commonApi) GetList(ctx RequestContext, auth Auth, form *ListWhereForm)
return nil, DefaultError(fmt.Sprintf("Table %s has not been found", table))
}

if len(form.Order) > 0 {
rows, err := q.Order(form.Order).Offset(form.Offset).Limit(form.Limit).Rows()
if len(order) > 0 {
rows, err := q.Order(order).Offset(form.Offset).Limit(form.Limit).Rows()
if err != nil {
logger.WithFields(log.Fields{"type": consts.DBError, "error": err, "table": table}).Error("Getting rows from table")
return nil, DefaultError(err.Error())
Expand Down
2 changes: 1 addition & 1 deletion packages/service/jsonrpc/common_forms.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (f *ecosystemForm) Validate(r *http.Request) error {

ecosysID, err := f.Validator.ValidateId(f.EcosystemID, client.EcosystemID, logger)
if err != nil {
if err == errors.New("Ecosystem not found") {
if err.Error() == "Ecosystem not found" {
err = fmt.Errorf("ecosystem %d doesn't exist", f.EcosystemID)
}
return err
Expand Down
2 changes: 1 addition & 1 deletion packages/service/jsonrpc/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type NotSingle struct {
type dataApi struct {
}

func NewDataApi() *dataApi {
func newDataApi() *dataApi {
return &dataApi{}
}

Expand Down
12 changes: 6 additions & 6 deletions packages/service/jsonrpc/namespace_ibax.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func (p *IbaxApi) GetApis() []any {

func NewIbaxApi(m Mode) *IbaxApi {
return &IbaxApi{
auth: NewAuthApi(m),
bk: NewBlockChainApi(),
common: NewCommonApi(m),
tx: NewTransactionApi(),
account: NewAccountsApi(m),
data: NewDataApi(),
auth: newAuthApi(m),
bk: newBlockChainApi(),
common: newCommonApi(m),
tx: newTransactionApi(),
account: newAccountsApi(m),
data: newDataApi(),
}
}
2 changes: 1 addition & 1 deletion packages/service/jsonrpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
type transactionApi struct {
}

func NewTransactionApi() *transactionApi {
func newTransactionApi() *transactionApi {
return &transactionApi{}
}

Expand Down

0 comments on commit 771519c

Please sign in to comment.