Skip to content

Commit

Permalink
Add optional token_id parameter to the contract tokens endpoint (#570)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kus authored Mar 27, 2021
1 parent c6be63f commit e45cd1e
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cmd/api/handlers/dapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (ctx *Context) appendDAppInfo(dapp *tzip.DApp, withDetails bool) (DApp, err
ReleaseDate: contract.Timestamp.UTC(),
})

tokens, err := ctx.getTokens(consts.Mainnet, address.Address, 0, 0, 0, 0, "")
tokens, err := ctx.getTokens(consts.Mainnet, address.Address, 0, 0, 0, 0, "", -1)
if err != nil {
return result, err
}
Expand Down
1 change: 1 addition & 0 deletions cmd/api/handlers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ type minMaxLevel struct {
type tokenRequest struct {
pageableRequest
minMaxLevel
TokenID *int64 `form:"token_id" binding:"omitempty"`
}

type tokenBalanceRequest struct {
Expand Down
12 changes: 8 additions & 4 deletions cmd/api/handlers/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,26 @@ func (ctx *Context) GetContractTokens(c *gin.Context) {
if err := c.BindQuery(&pageReq); ctx.handleError(c, err, http.StatusBadRequest) {
return
}
if pageReq.TokenID == nil {
pageReq.TokenID = new(int64)
*pageReq.TokenID = -1
}
if pageReq.Size == 0 {
pageReq.Size = 10
}
tokens, err := ctx.getTokens(req.Network, req.Address, pageReq.Size, pageReq.Offset, pageReq.MinLevel, pageReq.MaxLevel, "")
tokens, err := ctx.getTokens(req.Network, req.Address, pageReq.Size, pageReq.Offset, pageReq.MinLevel, pageReq.MaxLevel, "", *pageReq.TokenID)
if ctx.handleError(c, err, 0) {
return
}
c.JSON(http.StatusOK, tokens)
}

func (ctx *Context) getTokens(network, contract string, size, offset, minLevel, maxLevel int64, creator string) ([]Token, error) {
func (ctx *Context) getTokens(network, contract string, size, offset, minLevel, maxLevel int64, creator string, tokenId int64) ([]Token, error) {
metadata, err := ctx.TokenMetadata.Get([]tokenmetadata.GetContext{
{
Contract: contract,
Network: network,
TokenID: -1,
TokenID: tokenId,
MinLevel: minLevel,
MaxLevel: maxLevel,
Creator: creator,
Expand Down Expand Up @@ -407,7 +411,7 @@ func (ctx *Context) GetTokenMetadata(c *gin.Context) {
if queryParams.Size == 0 {
queryParams.Size = 10
}
tokens, err := ctx.getTokens(req.Network, queryParams.Contract, queryParams.Size, queryParams.Offset, queryParams.MinLevel, queryParams.MaxLevel, queryParams.Creator)
tokens, err := ctx.getTokens(req.Network, queryParams.Contract, queryParams.Size, queryParams.Offset, queryParams.MinLevel, queryParams.MaxLevel, queryParams.Creator, -1)
if ctx.handleError(c, err, 0) {
return
}
Expand Down

0 comments on commit e45cd1e

Please sign in to comment.