arnaucube
arnaucube/hermez-nodePublic Self-hosted repository
hermez-node/api/bids.go
api/bids.goRaw
 1package api
 2
 3import (
 4	"errors"
 5	"net/http"
 6
 7	"github.com/gin-gonic/gin"
 8	"github.com/hermeznetwork/hermez-node/db"
 9	"github.com/hermeznetwork/hermez-node/db/historydb"
10)
11
12func (a *API) getBids(c *gin.Context) {
13	slotNum, bidderAddr, err := parseBidFilters(c)
14	if err != nil {
15		retBadReq(err, c)
16		return
17	}
18	if slotNum == nil && bidderAddr == nil {
19		retBadReq(errors.New("It is necessary to add at least one filter: slotNum or/and bidderAddr"), c)
20		return
21	}
22	// Pagination
23	fromItem, order, limit, err := parsePagination(c)
24	if err != nil {
25		retBadReq(err, c)
26		return
27	}
28
29	bids, pagination, err := a.h.GetBidsAPI(
30		slotNum, bidderAddr, fromItem, limit, order,
31	)
32
33	if err != nil {
34		retSQLErr(err, c)
35		return
36	}
37
38	// Build succesfull response
39	type bidsResponse struct {
40		Bids       []historydb.BidAPI `json:"bids"`
41		Pagination *db.Pagination     `json:"pagination"`
42	}
43	c.JSON(http.StatusOK, &bidsResponse{
44		Bids:       bids,
45		Pagination: pagination,
46	})
47}