flamingo.me/flamingo-commerce/v3@v3.11.0/product/interfaces/templatefunctions/getProductUrl.go (about)

     1  package templatefunctions
     2  
     3  import (
     4  	"context"
     5  
     6  	"flamingo.me/flamingo-commerce/v3/product/application"
     7  	"flamingo.me/flamingo-commerce/v3/product/domain"
     8  	"flamingo.me/flamingo/v3/framework/flamingo"
     9  )
    10  
    11  type (
    12  	// GetProductURL is exported as a template function
    13  	GetProductURL struct {
    14  		URLService *application.URLService `inject:""`
    15  		Logger     flamingo.Logger         `inject:""`
    16  	}
    17  )
    18  
    19  // Func returns the JSON object
    20  func (tf *GetProductURL) Func(ctx context.Context) interface{} {
    21  	return func(p domain.BasicProduct) string {
    22  		if p == nil {
    23  			tf.Logger.WithField("category", "product").Warn("Called getPrpductUrl templatefunc without a product")
    24  			return ""
    25  		}
    26  		url, err := tf.URLService.Get(p, "")
    27  		if err != nil {
    28  			tf.Logger.WithContext(ctx).WithField("category", "product").Error(err)
    29  			return ""
    30  		}
    31  		return url
    32  	}
    33  }