Skip to content

Commit

Permalink
Add helper function (#945)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Sep 10, 2022
1 parent b87974a commit b68e126
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 16 deletions.
41 changes: 41 additions & 0 deletions cmd/api/handlers/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package handlers

import (
"net/http"
"strings"

"github.com/baking-bad/bcdhub/internal/config"
"github.com/baking-bad/bcdhub/internal/models/types"
"github.com/gin-gonic/gin"
)

// ContractsHelpers -
func ContractsHelpers() gin.HandlerFunc {
return func(c *gin.Context) {
ctx := c.MustGet("context").(*config.Context)

var req getByNetwork
if err := c.BindUri(&req); handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}

var args findContract
if err := c.BindQuery(&args); handleError(c, ctx.Storage, err, http.StatusBadRequest) {
return
}

splitted := strings.Split(args.Tags, ",")
tags := types.NewTags(splitted)
contract, err := ctx.Contracts.FindOne(tags)
if handleError(c, ctx.Storage, err, http.StatusNotFound) {
return
}

response, err := contractPostprocessing(ctx, contract)
if handleError(c, ctx.Storage, err, 0) {
return
}

c.SecureJSON(http.StatusOK, response)
}
}
4 changes: 4 additions & 0 deletions cmd/api/handlers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,3 +249,7 @@ type globalConstantsContractsRequest struct {
type getViewsArgs struct {
Kind ViewSchemaKind `form:"kind" binding:"omitempty,oneof=off-chain on-chain"`
}

type findContract struct {
Tags string `form:"tags" binding:"omitempty"`
}
5 changes: 5 additions & 0 deletions cmd/api/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ func (api *app) makeRouter() {
}
}

helpers := v1.Group("helpers")
{
helpers.GET("contracts/:network", handlers.NetworkMiddleware(api.Contexts), cache.CachePage(store, time.Hour, handlers.ContractsHelpers()))
}

bigmap := v1.Group("bigmap/:network/:ptr")
bigmap.Use(handlers.NetworkMiddleware(api.Contexts))
{
Expand Down
7 changes: 6 additions & 1 deletion internal/models/contract/repository.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package contract

import "time"
import (
"time"

"github.com/baking-bad/bcdhub/internal/models/types"
)

// Repository -
type Repository interface {
Expand All @@ -15,6 +19,7 @@ type Repository interface {

// ScriptPart - returns part of script type. Part can be `storage`, `parameter` or `code`.
ScriptPart(address string, symLink, part string) ([]byte, error)
FindOne(tags types.Tags) (Contract, error)
}

// ScriptRepository -
Expand Down
31 changes: 16 additions & 15 deletions internal/models/mock/contract/mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions internal/postgres/contract/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,14 @@ func (storage *Storage) RecentlyCalled(offset, size int64) (contracts []contract
func (storage *Storage) Count() (int, error) {
return storage.DB.Model((*contract.Contract)(nil)).CountEstimate(1000000)
}

// FindOne -
func (storage *Storage) FindOne(tags types.Tags) (result contract.Contract, err error) {
err = storage.DB.Model(&result).
Where("tags&? > 0", tags).
ColumnExpr("contract.id, contract.tx_count, contract.last_action, contract.account_id, contract.timestamp, contract.level").
ColumnExpr("account.address as account__address, account.alias as account__alias").
Join(`LEFT JOIN "accounts" AS "account" ON "account"."id" = "contract"."account_id"`).
First()
return
}

0 comments on commit b68e126

Please sign in to comment.