/
list_assets.go
81 lines (67 loc) · 1.59 KB
/
list_assets.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
package endpoint
import (
"context"
"fmt"
"net/http"
"github.com/spolu/settle/lib/db"
"github.com/spolu/settle/lib/errors"
"github.com/spolu/settle/lib/format"
"github.com/spolu/settle/lib/ptr"
"github.com/spolu/settle/lib/svc"
"github.com/spolu/settle/mint"
"github.com/spolu/settle/mint/lib/authentication"
"github.com/spolu/settle/mint/model"
)
const (
// EndPtListAssets creates a new assset.
EndPtListAssets EndPtName = "ListAssets"
)
func init() {
registrar[EndPtListAssets] = NewListAssets
}
// ListAssets returns a list of assets.
type ListAssets struct {
ListEndpoint
Owner string
}
// NewListAssets constructs and initialiezes the endpoint.
func NewListAssets(
r *http.Request,
) (Endpoint, error) {
return &ListAssets{
ListEndpoint: ListEndpoint{},
}, nil
}
// Validate validates the input parameters.
func (e *ListAssets) Validate(
r *http.Request,
) error {
ctx := r.Context()
e.Owner = fmt.Sprintf("%s@%s",
authentication.Get(ctx).User.Username, mint.GetHost(ctx))
return e.ListEndpoint.Validate(r)
}
// Execute executes the endpoint.
func (e *ListAssets) Execute(
ctx context.Context,
) (*int, *svc.Resp, error) {
ctx = db.Begin(ctx, "mint")
defer db.LoggedRollback(ctx)
assets, err := model.LoadAssetListByOwner(ctx,
e.ListEndpoint.CreatedBefore,
e.ListEndpoint.Limit,
e.Owner,
)
if err != nil {
return nil, nil, errors.Trace(err) // 500
}
db.Commit(ctx)
l := []mint.AssetResource{}
for _, a := range assets {
a := a
l = append(l, model.NewAssetResource(ctx, &a))
}
return ptr.Int(http.StatusOK), &svc.Resp{
"assets": format.JSONPtr(l),
}, nil
}