Skip to content

Commit

Permalink
Add filters to ticket updates endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Oct 15, 2023
1 parent 374e718 commit 133355e
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 31 deletions.
7 changes: 7 additions & 0 deletions cmd/api/handlers/requests.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,10 @@ type ticketBalancesRequest struct {
pageableRequest
WithoutZeroBalances bool `binding:"omitempty" form:"skip_empty"`
}

type ticketUpdatesRequest struct {
pageableRequest

Account string `binding:"omitempty,address" form:"account"`
TicketId *uint64 `binding:"omitempty" form:"ticket_id"`
}
23 changes: 17 additions & 6 deletions cmd/api/handlers/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ import (
// @Description Get ticket updates for contract
// @Tags contract
// @ID get-contract-ticket-updates
// @Param network path string true "network"
// @Param address path string true "KT address" minlength(36) maxlength(36)
// @Param size query integer false "Updates count" mininum(1) maximum(10)
// @Param offset query integer false "Offset" mininum(1)
// @Param network path string true "network"
// @Param address path string true "KT address" minlength(36) maxlength(36)
// @Param account query string false "Address" minlength(36) maxlength(36)
// @Param ticket_id query integer false "Ticket id" mininum(0)
// @Param size query integer false "Updates count" mininum(1) maximum(10)
// @Param offset query integer false "Offset" mininum(1)
// @Accept json
// @Produce json
// @Success 200 {array} TicketUpdate
Expand All @@ -35,12 +37,21 @@ func GetContractTicketUpdates() gin.HandlerFunc {
return
}

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

updates, err := ctx.Tickets.Updates(c.Request.Context(), req.Address, args.Size, args.Offset)
updates, err := ctx.Tickets.Updates(c.Request.Context(),
ticket.UpdatesRequest{
Ticketer: req.Address,
Account: args.Account,
TicketId: args.TicketId,
Limit: args.Size,
Offset: args.Offset,
},
)

if handleError(c, ctx.Storage, err, 0) {
return
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/indexer/indexer/indices.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,18 @@ func (bi *BlockchainIndexer) createIndices(ctx context.Context) error {
if err := bi.Storage.CreateIndex(ctx, "ticket_updates_ticket_id_idx", "ticket_id", ticketUpdate); err != nil {
return err
}
if err := bi.Storage.CreateIndex(ctx, "ticket_updates_account_id_idx", "account_id", ticketUpdate); err != nil {
return err
}

// Tickets
tickets := (*ticket.Ticket)(nil)
if err := bi.Storage.CreateIndex(ctx, "ticket_level_idx", "level", tickets); err != nil {
return err
}
if err := bi.Storage.CreateIndex(ctx, "ticket_ticketer_id_idx", "ticketer_id", tickets); err != nil {
return err
}

log.Info().Str("network", bi.Network.String()).Msg("database indices was created")

Expand Down
12 changes: 6 additions & 6 deletions internal/models/mock/ticket/mock.go

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

10 changes: 9 additions & 1 deletion internal/models/ticket/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,18 @@ type BalanceRequest struct {
WithoutZeroBalances bool
}

type UpdatesRequest struct {
Account string
Ticketer string
TicketId *uint64
Limit int64
Offset int64
}

//go:generate mockgen -source=$GOFILE -destination=../mock/ticket/mock.go -package=ticket -typed
type Repository interface {
List(ctx context.Context, ticketer string, limit, offset int64) ([]Ticket, error)
Updates(ctx context.Context, ticketer string, limit, offset int64) ([]TicketUpdate, error)
Updates(ctx context.Context, req UpdatesRequest) ([]TicketUpdate, error)
UpdatesForOperation(ctx context.Context, operationId int64) ([]TicketUpdate, error)
BalancesForAccount(ctx context.Context, accountId int64, req BalanceRequest) ([]Balance, error)
}
9 changes: 9 additions & 0 deletions internal/postgres/tests/fixtures/accounts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -736,6 +736,15 @@
events_count: 0
migrations_count: 0
ticket_updates_count: 0
- address: tz2GGf91VB3gK7MaaP8Xua6ohhHAhH4hTC11
id: 131
last_action: 2022-01-25T16:59:07Z
level: 39
operations_count: 2
type: 1
events_count: 0
migrations_count: 0
ticket_updates_count: 0
- address: KT1BrG24EYvtYGeGgd615Yzhvoa14BNPLZzu
id: 132
last_action: 2022-01-25T16:59:07Z
Expand Down
53 changes: 51 additions & 2 deletions internal/postgres/tests/ticket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,62 @@ import (
"time"

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

func (s *StorageTestSuite) TestTicketGet() {
func (s *StorageTestSuite) TestTicketUpdates() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

updates, err := s.ticketUpdates.Updates(ctx, "KT1SM849krq9FFxGWCZyc7X5GvAz8XnRmXnf", 10, 0)
updates, err := s.ticketUpdates.Updates(ctx, ticket.UpdatesRequest{
Ticketer: "KT1SM849krq9FFxGWCZyc7X5GvAz8XnRmXnf",
Limit: 10,
Offset: 0,
})
s.Require().NoError(err)
s.Require().Len(updates, 2)

update := updates[0]
s.Require().EqualValues(2, update.ID)
s.Require().EqualValues(104, update.OperationId)
s.Require().EqualValues(40, update.Level)
s.Require().EqualValues(1, update.TicketId)
s.Require().EqualValues(131, update.AccountId)
s.Require().EqualValues("43", update.Amount.String())
s.Require().EqualValues("KT1SM849krq9FFxGWCZyc7X5GvAz8XnRmXnf", update.Ticket.Ticketer.Address)
}

func (s *StorageTestSuite) TestTicketUpdatesForAccount() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

updates, err := s.ticketUpdates.Updates(ctx, ticket.UpdatesRequest{
Account: "tz2GGf91VB3gK7MaaP8Xua6ohhHAhH4hTC11",
Limit: 10,
Offset: 0,
})
s.Require().NoError(err)
s.Require().Len(updates, 2)

update := updates[1]
s.Require().EqualValues(2, update.ID)
s.Require().EqualValues(104, update.OperationId)
s.Require().EqualValues(40, update.Level)
s.Require().EqualValues(1, update.TicketId)
s.Require().EqualValues(131, update.AccountId)
s.Require().EqualValues("43", update.Amount.String())
s.Require().EqualValues("KT1SM849krq9FFxGWCZyc7X5GvAz8XnRmXnf", update.Ticket.Ticketer.Address)
}

func (s *StorageTestSuite) TestTicketUpdatesByTicketId() {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()

updates, err := s.ticketUpdates.Updates(ctx, ticket.UpdatesRequest{
TicketId: testsuite.Ptr(uint64(1)),
Limit: 10,
Offset: 0,
})
s.Require().NoError(err)
s.Require().Len(updates, 2)

Expand Down
50 changes: 34 additions & 16 deletions internal/postgres/ticket/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,8 @@ func NewStorage(pg *core.Postgres) *Storage {
return &Storage{pg}
}

// Get -
func (storage *Storage) Updates(ctx context.Context, ticketer string, limit, offset int64) (response []ticket.TicketUpdate, err error) {
var ticketerId uint64
if err := storage.DB.NewSelect().
Model((*account.Account)(nil)).
Column("id").
Where("address = ?", ticketer).
Limit(1).
Scan(ctx, &ticketerId); err != nil {
return nil, err
}
// Updates -
func (storage *Storage) Updates(ctx context.Context, req ticket.UpdatesRequest) (response []ticket.TicketUpdate, err error) {
query := storage.DB.
NewSelect().
Model(&response).
Expand All @@ -38,13 +29,40 @@ func (storage *Storage) Updates(ctx context.Context, ticketer string, limit, off
Relation("Account", func(sq *bun.SelectQuery) *bun.SelectQuery {
return sq.Column("address")
}).
Where("ticket.ticketer_id = ?", ticketerId)
Limit(storage.GetPageSize(req.Limit))

if req.Ticketer != "" {
var ticketerId uint64
if err := storage.DB.NewSelect().
Model((*account.Account)(nil)).
Column("id").
Where("address = ?", req.Ticketer).
Limit(1).
Scan(ctx, &ticketerId); err != nil {
return nil, err
}
query.Where("ticket.ticketer_id = ?", ticketerId)
}

if offset > 0 {
query.Offset(int(offset))
if req.Account != "" {
var accountId uint64
if err := storage.DB.NewSelect().
Model((*account.Account)(nil)).
Column("id").
Where("address = ?", req.Account).
Limit(1).
Scan(ctx, &accountId); err != nil {
return nil, err
}
query.Where("account_id = ?", accountId)
}

if req.TicketId != nil {
query.Where("ticket_id = ?", *req.TicketId)
}
if limit > 0 {
query.Limit(storage.GetPageSize(limit))

if req.Offset > 0 {
query.Offset(int(req.Offset))
}

err = query.Order("id desc").Scan(ctx)
Expand Down

0 comments on commit 133355e

Please sign in to comment.