mirror of
https://github.com/arnaucube/hermez-node.git
synced 2026-02-07 11:26:44 +01:00
API Coordinator Refactor
This commit is contained in:
57
api/coordinator.go
Normal file
57
api/coordinator.go
Normal file
@@ -0,0 +1,57 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/hermeznetwork/hermez-node/db"
|
||||
"github.com/hermeznetwork/hermez-node/db/historydb"
|
||||
)
|
||||
|
||||
func getCoordinator(c *gin.Context) {
|
||||
// Get bidderAddr
|
||||
const name = "bidderAddr"
|
||||
bidderAddr, err := parseParamEthAddr(name, c)
|
||||
|
||||
if err != nil {
|
||||
retBadReq(err, c)
|
||||
return
|
||||
} else if bidderAddr == nil {
|
||||
retBadReq(ErrNillBidderAddr, c)
|
||||
return
|
||||
}
|
||||
|
||||
coordinator, err := h.GetCoordinatorAPI(*bidderAddr)
|
||||
if err != nil {
|
||||
retSQLErr(err, c)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, coordinator)
|
||||
}
|
||||
|
||||
func getCoordinators(c *gin.Context) {
|
||||
// Pagination
|
||||
fromItem, order, limit, err := parsePagination(c)
|
||||
if err != nil {
|
||||
retBadReq(err, c)
|
||||
return
|
||||
}
|
||||
|
||||
// Fetch coordinators from historyDB
|
||||
coordinators, pagination, err := h.GetCoordinatorsAPI(fromItem, limit, order)
|
||||
if err != nil {
|
||||
retSQLErr(err, c)
|
||||
return
|
||||
}
|
||||
|
||||
// Build succesfull response
|
||||
type coordinatorsResponse struct {
|
||||
Coordinators []historydb.CoordinatorAPI `json:"coordinators"`
|
||||
Pagination *db.Pagination `json:"pagination"`
|
||||
}
|
||||
c.JSON(http.StatusOK, &coordinatorsResponse{
|
||||
Coordinators: coordinators,
|
||||
Pagination: pagination,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user