-
Notifications
You must be signed in to change notification settings - Fork 2
/
accounts.go
70 lines (60 loc) · 1.81 KB
/
accounts.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
package handler
import (
"context"
"fmt"
"github.com/labstack/echo/v4"
"github.com/spacemeshos/explorer-backend/internal/service"
"github.com/spacemeshos/explorer-backend/model"
"net/http"
)
func Accounts(c echo.Context) error {
cc := c.(*ApiContext)
pageNum, pageSize := GetPagination(c)
accounts, total, err := cc.Service.GetAccounts(context.TODO(), pageNum, pageSize)
if err != nil {
return fmt.Errorf("failed to get accounts list: %w", err)
}
return c.JSON(http.StatusOK, PaginatedDataResponse{
Data: accounts,
Pagination: GetPaginationMetadata(total, pageNum, pageSize),
})
}
func Account(c echo.Context) error {
cc := c.(*ApiContext)
account, err := cc.Service.GetAccount(context.TODO(), c.Param("id"))
if err != nil {
if err == service.ErrNotFound {
return echo.ErrNotFound
}
return fmt.Errorf("failed to get account `%s` info: %w", c.Param("id"), err)
}
return c.JSON(http.StatusOK, DataResponse{Data: []*model.Account{account}})
}
func AccountDetails(c echo.Context) error {
cc := c.(*ApiContext)
pageNum, pageSize := GetPagination(cc)
accountID := c.Param("id")
var (
response interface{}
err error
total int64
)
switch c.Param("entity") {
case txs:
response, total, err = cc.Service.GetAccountTransactions(context.TODO(), accountID, pageNum, pageSize)
case rewards:
response, total, err = cc.Service.GetAccountRewards(context.TODO(), accountID, pageNum, pageSize)
default:
return echo.NewHTTPError(http.StatusNotFound, "entity not found")
}
if err != nil {
if err == service.ErrNotFound {
return echo.ErrNotFound
}
return fmt.Errorf("failed to get account entity `%s` list: %w", c.Param("entity"), err)
}
return c.JSON(http.StatusOK, PaginatedDataResponse{
Data: response,
Pagination: GetPaginationMetadata(total, pageNum, pageSize),
})
}