-
Notifications
You must be signed in to change notification settings - Fork 2
/
layers.go
82 lines (71 loc) · 2.23 KB
/
layers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package handler
import (
"context"
"fmt"
"github.com/labstack/echo/v4"
"github.com/spacemeshos/explorer-backend/internal/service"
"net/http"
"strconv"
"github.com/gofiber/fiber/v2"
"github.com/spacemeshos/explorer-backend/model"
)
func Layers(c echo.Context) error {
cc := c.(*ApiContext)
pageNum, pageSize := GetPagination(c)
layersList, total, err := cc.Service.GetLayers(context.TODO(), pageNum, pageSize)
if err != nil {
return fmt.Errorf("failed to get epoch list: %w", err)
}
return c.JSON(http.StatusOK, PaginatedDataResponse{
Data: layersList,
Pagination: GetPaginationMetadata(total, pageNum, pageSize),
})
}
func Layer(c echo.Context) error {
cc := c.(*ApiContext)
layerID, err := strconv.Atoi(c.Param("id"))
if err != nil {
return c.NoContent(http.StatusBadRequest)
}
layer, err := cc.Service.GetLayer(context.TODO(), layerID)
if err != nil {
if err == service.ErrNotFound {
return echo.ErrNotFound
}
return fmt.Errorf("failed to get layer info: %w", err)
}
return c.JSON(http.StatusOK, DataResponse{Data: []*model.Layer{layer}})
}
func LayerDetails(c echo.Context) error {
cc := c.(*ApiContext)
pageNum, pageSize := GetPagination(c)
layerID, err := strconv.Atoi(c.Param("id"))
if err != nil {
return fmt.Errorf("wrong layer id: %w", err)
}
var (
response interface{}
total int64
)
switch c.Param("entity") {
case blocks:
response, total, err = cc.Service.GetLayerBlocks(context.TODO(), layerID, pageNum, pageSize)
case txs:
response, total, err = cc.Service.GetLayerTransactions(context.TODO(), layerID, pageNum, pageSize)
case smeshers:
response, total, err = cc.Service.GetLayerSmeshers(context.TODO(), layerID, pageNum, pageSize)
case rewards:
response, total, err = cc.Service.GetLayerRewards(context.TODO(), layerID, pageNum, pageSize)
case atxs:
response, total, err = cc.Service.GetLayerActivations(context.TODO(), layerID, pageNum, pageSize)
default:
return fiber.NewError(fiber.StatusNotFound, "entity not found")
}
if err != nil {
return fmt.Errorf("failed to get layer entity `%s` list: %w", c.Param("entity"), err)
}
return c.JSON(http.StatusOK, PaginatedDataResponse{
Data: response,
Pagination: GetPaginationMetadata(total, pageNum, pageSize),
})
}