flamingo.me/flamingo-commerce/v3@v3.11.0/cart/interfaces/templatefunctions/getCart.go (about)

     1  package templatefunctions
     2  
     3  import (
     4  	"context"
     5  
     6  	"go.opencensus.io/trace"
     7  
     8  	"flamingo.me/flamingo-commerce/v3/cart/domain/decorator"
     9  	"flamingo.me/flamingo/v3/framework/web"
    10  
    11  	"flamingo.me/flamingo-commerce/v3/cart/application"
    12  	cartDomain "flamingo.me/flamingo-commerce/v3/cart/domain/cart"
    13  	"flamingo.me/flamingo/v3/framework/flamingo"
    14  )
    15  
    16  type (
    17  	// GetCart is exported as a template function
    18  	GetCart struct {
    19  		cartReceiverService *application.CartReceiverService
    20  		logger              flamingo.Logger
    21  	}
    22  	// GetDecoratedCart is exported as a template function
    23  	GetDecoratedCart struct {
    24  		cartReceiverService *application.CartReceiverService
    25  		logger              flamingo.Logger
    26  	}
    27  )
    28  
    29  // Inject dependencies
    30  func (tf *GetCart) Inject(
    31  	applicationCartReceiverService *application.CartReceiverService,
    32  	logger flamingo.Logger,
    33  
    34  ) {
    35  	tf.cartReceiverService = applicationCartReceiverService
    36  	tf.logger = logger.WithField(flamingo.LogKeyModule, "cart").WithField(flamingo.LogKeyCategory, "getCart")
    37  }
    38  
    39  // Func defines the GetCart template function
    40  func (tf *GetCart) Func(ctx context.Context) interface{} {
    41  	return func() cartDomain.Cart {
    42  		ctx, span := trace.StartSpan(ctx, "cart/GetCart/Func")
    43  		defer span.End()
    44  
    45  		session := web.SessionFromContext(ctx)
    46  		cart, e := tf.cartReceiverService.ViewCart(ctx, session)
    47  		if e != nil {
    48  			tf.logger.WithContext(ctx).Error("Error: cart.interfaces.templatefunc %v", e)
    49  		}
    50  		if cart == nil {
    51  			cart = &cartDomain.Cart{}
    52  		}
    53  		//dereference (should lower risk of undesired sideeffects if missused in template)
    54  		return *cart
    55  	}
    56  }
    57  
    58  // Inject dependencies
    59  func (tf *GetDecoratedCart) Inject(
    60  	cartReceiverService *application.CartReceiverService,
    61  	logger flamingo.Logger,
    62  ) {
    63  	tf.cartReceiverService = cartReceiverService
    64  	tf.logger = logger
    65  }
    66  
    67  // Func defines the GetDecoratedCart template function
    68  func (tf *GetDecoratedCart) Func(ctx context.Context) interface{} {
    69  	return func() decorator.DecoratedCart {
    70  		ctx, span := trace.StartSpan(ctx, "cart/GetDecoratedCart/Func")
    71  		defer span.End()
    72  
    73  		session := web.SessionFromContext(ctx)
    74  		cart, e := tf.cartReceiverService.ViewDecoratedCart(ctx, session)
    75  		if e != nil {
    76  			if e == application.ErrTemporaryCartService {
    77  				tf.logger.WithContext(ctx).Warn("Error: cart.interfaces.templatefunc %v", e)
    78  			} else {
    79  				tf.logger.WithContext(ctx).Error("Error: cart.interfaces.templatefunc %v", e)
    80  			}
    81  		}
    82  		if cart == nil {
    83  			cart = &decorator.DecoratedCart{}
    84  		}
    85  		//dereference (should lower risk of undesired sideeffects if missused in template)
    86  		return *cart
    87  	}
    88  }