Skip to content

Commit

Permalink
Change ticket updates API (#973)
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky authored Apr 16, 2023
1 parent 7f10779 commit a4330ba
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 11 deletions.
9 changes: 8 additions & 1 deletion cmd/api/handlers/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ func contractWithStatsPostprocessing(ctxs config.Contexts, ctx *config.Context,
if err != nil {
return ContractWithStats{}, err
}
res := ContractWithStats{c, 0, 0}
res := ContractWithStats{c, 0, 0, false}

eventsCount, err := ctx.Operations.EventsCount(contractModel.AccountID)
if err != nil {
Expand All @@ -157,5 +157,12 @@ func contractWithStatsPostprocessing(ctxs config.Contexts, ctx *config.Context,
return res, err
}
res.SameCount += int64(stats)

hasTicketUpdates, err := ctx.TicketUpdates.Has(contractModel.AccountID)
if err != nil {
return res, err
}
res.HasTicketUpdates = hasTicketUpdates

return res, nil
}
22 changes: 12 additions & 10 deletions cmd/api/handlers/responses.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,9 @@ func (c *Contract) FromModel(contract contract.Contract) {
type ContractWithStats struct {
Contract

SameCount int64 `json:"same_count"`
EventsCount int `json:"events_count"`
SameCount int64 `json:"same_count"`
EventsCount int `json:"events_count"`
HasTicketUpdates bool `json:"has_ticket_updates"`
}

// RecentlyCalledContract -
Expand Down Expand Up @@ -660,14 +661,15 @@ func NewEvent(o operation.Operation) (*Event, error) {

// TicketUpdate -
type TicketUpdate struct {
ID int64 `json:"id"`
Level int64 `json:"level"`
Timestamp time.Time `json:"timestamp"`
Ticketer string `json:"ticketer"`
Address string `json:"address"`
Amount string `json:"amount"`
ContentType []ast.Typedef `json:"content_type"`
Content *ast.MiguelNode `json:"content,omitempty"`
ID int64 `json:"id"`
Level int64 `json:"level"`
Timestamp time.Time `json:"timestamp"`
Ticketer string `json:"ticketer"`
Address string `json:"address"`
Amount string `json:"amount"`
OperationHash string `json:"operation_hash"`
ContentType []ast.Typedef `json:"content_type"`
Content *ast.MiguelNode `json:"content,omitempty"`
}

// NewTicketUpdateFromModel -
Expand Down
6 changes: 6 additions & 0 deletions cmd/api/handlers/tickets.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ func GetContractTicketUpdates() gin.HandlerFunc {
update.Content = contentMiguel[0]
}

operation, err := ctx.Operations.GetByID(updates[i].OperationID)
if handleError(c, ctx.Storage, err, 0) {
return
}
update.OperationHash = operation.Hash

response = append(response, update)
}

Expand Down
1 change: 1 addition & 0 deletions internal/models/ticket/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package ticket
// Repository -
type Repository interface {
Get(ticketer string, limit, offset int64) ([]TicketUpdate, error)
Has(contractID int64) (bool, error)
}
20 changes: 20 additions & 0 deletions internal/postgres/ticket/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,23 @@ func (storage *Storage) Get(ticketer string, limit, offset int64) ([]ticket.Tick
err := query.Order("id desc").Select(&response)
return response, err
}

// Has -
func (storage *Storage) Has(contractID int64) (bool, error) {
var id int64
err := storage.DB.
Model((*ticket.TicketUpdate)(nil)).
Column("id").
Where("ticketer_id = ?", contractID).
OrderExpr("id ASC").
Limit(1).
Select(&id)

if err != nil {
if storage.IsRecordNotFound(err) {
return false, nil
}
return false, err
}
return true, nil
}

0 comments on commit a4330ba

Please sign in to comment.