github.com/machinefi/w3bstream@v1.6.5-rc9.0.20240426031326-b8c7c4876e72/cmd/srv-applet-mgr/apis/strategy/get.go (about)

     1  package strategy
     2  
     3  import (
     4  	"context"
     5  
     6  	"github.com/machinefi/w3bstream/cmd/srv-applet-mgr/apis/middleware"
     7  	"github.com/machinefi/w3bstream/pkg/depends/kit/httptransport/httpx"
     8  	"github.com/machinefi/w3bstream/pkg/modules/strategy"
     9  	"github.com/machinefi/w3bstream/pkg/types"
    10  )
    11  
    12  type GetStrategy struct {
    13  	httpx.MethodGet
    14  	StrategyID types.SFID `in:"path" name:"strategyID"`
    15  }
    16  
    17  func (r *GetStrategy) Path() string { return "/data/:strategyID" }
    18  
    19  func (r *GetStrategy) Output(ctx context.Context) (interface{}, error) {
    20  	ctx, err := middleware.MustCurrentAccountFromContext(ctx).
    21  		WithStrategyBySFID(ctx, r.StrategyID)
    22  	if err != nil {
    23  		return nil, err
    24  	}
    25  
    26  	return types.MustStrategyFromContext(ctx), nil
    27  }
    28  
    29  type ListStrategy struct {
    30  	httpx.MethodGet
    31  	strategy.ListReq
    32  }
    33  
    34  func (r *ListStrategy) Path() string { return "/datalist" }
    35  
    36  func (r *ListStrategy) Output(ctx context.Context) (interface{}, error) {
    37  	ctx, err := middleware.MustCurrentAccountFromContext(ctx).
    38  		WithProjectContextByName(ctx, middleware.MustProjectName(ctx))
    39  	if err != nil {
    40  		return nil, err
    41  	}
    42  
    43  	r.ListReq.ProjectID = types.MustProjectFromContext(ctx).ProjectID
    44  	return strategy.List(ctx, &r.ListReq)
    45  }
    46  
    47  type ListStrategyDetail struct {
    48  	httpx.MethodGet
    49  	strategy.ListReq
    50  }
    51  
    52  func (r *ListStrategyDetail) Path() string { return "/details" }
    53  
    54  func (r *ListStrategyDetail) Output(ctx context.Context) (interface{}, error) {
    55  	ctx, err := middleware.MustCurrentAccountFromContext(ctx).
    56  		WithProjectContextByName(ctx, middleware.MustProjectName(ctx))
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	return strategy.ListDetail(ctx, &r.ListReq)
    62  }