flamingo.me/flamingo-commerce/v3@v3.11.0/cart/interfaces/controller/forms/billingform.go (about)

     1  package forms
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  
     7  	"flamingo.me/flamingo/v3/framework/flamingo"
     8  	"flamingo.me/flamingo/v3/framework/web"
     9  	"flamingo.me/form/application"
    10  	"flamingo.me/form/domain"
    11  
    12  	cartApplication "flamingo.me/flamingo-commerce/v3/cart/application"
    13  	customerApplication "flamingo.me/flamingo-commerce/v3/customer/application"
    14  )
    15  
    16  type (
    17  	// BillingAddressForm the form for billing address
    18  	BillingAddressForm AddressForm
    19  
    20  	// BillingAddressFormService implements Form(Data)Provider interface of form package
    21  	BillingAddressFormService struct {
    22  		customerApplicationService     *customerApplication.Service
    23  		applicationCartReceiverService *cartApplication.CartReceiverService
    24  	}
    25  
    26  	// BillingAddressFormController the (mini) MVC
    27  	BillingAddressFormController struct {
    28  		responder                      *web.Responder
    29  		applicationCartService         *cartApplication.CartService
    30  		applicationCartReceiverService *cartApplication.CartReceiverService
    31  		logger                         flamingo.Logger
    32  		formHandlerFactory             application.FormHandlerFactory
    33  	}
    34  )
    35  
    36  // Inject dependencies
    37  func (p *BillingAddressFormService) Inject(
    38  	applicationCartReceiverService *cartApplication.CartReceiverService,
    39  	customerApplicationService *customerApplication.Service) {
    40  	p.customerApplicationService = customerApplicationService
    41  	p.applicationCartReceiverService = applicationCartReceiverService
    42  }
    43  
    44  // GetFormData provides form data
    45  func (p *BillingAddressFormService) GetFormData(ctx context.Context, req *web.Request) (interface{}, error) {
    46  	billingAddressForm := AddressForm{}
    47  
    48  	customer, err := p.customerApplicationService.GetForIdentity(ctx, req)
    49  	if err == nil {
    50  		billingAddress := customer.GetDefaultBillingAddress()
    51  		if billingAddress != nil {
    52  			billingAddressForm.LoadFromCustomerAddress(*billingAddress)
    53  		}
    54  	}
    55  
    56  	cart, err := p.applicationCartReceiverService.ViewCart(ctx, req.Session())
    57  	if err == nil {
    58  		if cart.BillingAddress != nil {
    59  			billingAddressForm.LoadFromCartAddress(*cart.BillingAddress)
    60  		}
    61  	}
    62  	return BillingAddressForm(billingAddressForm), nil
    63  }
    64  
    65  // Inject dependencies
    66  func (c *BillingAddressFormController) Inject(
    67  	responder *web.Responder,
    68  	applicationCartService *cartApplication.CartService,
    69  	applicationCartReceiverService *cartApplication.CartReceiverService,
    70  	logger flamingo.Logger,
    71  	formHandlerFactory application.FormHandlerFactory,
    72  ) {
    73  	c.responder = responder
    74  	c.applicationCartReceiverService = applicationCartReceiverService
    75  	c.applicationCartService = applicationCartService
    76  	c.formHandlerFactory = formHandlerFactory
    77  	c.logger = logger.WithField(flamingo.LogKeyModule, "cart").WithField(flamingo.LogKeyCategory, "billingform")
    78  }
    79  
    80  func (c *BillingAddressFormController) getFormHandler() (domain.FormHandler, error) {
    81  	builder := c.formHandlerFactory.GetFormHandlerBuilder()
    82  	err := builder.SetNamedFormService("commerce.cart.billingFormService")
    83  	if err != nil {
    84  		return nil, err
    85  	}
    86  	return builder.Build(), nil
    87  }
    88  
    89  // GetUnsubmittedForm returns unsubmitted form
    90  func (c *BillingAddressFormController) GetUnsubmittedForm(ctx context.Context, r *web.Request) (*domain.Form, error) {
    91  	formHandler, err := c.getFormHandler()
    92  	if err != nil {
    93  		return nil, err
    94  	}
    95  	return formHandler.HandleUnsubmittedForm(ctx, r)
    96  }
    97  
    98  // HandleFormAction return the form or error. If the form was submitted the action is performed
    99  func (c *BillingAddressFormController) HandleFormAction(ctx context.Context, r *web.Request) (form *domain.Form, actionSuccessful bool, err error) {
   100  	session := web.SessionFromContext(ctx)
   101  	formHandler, err := c.getFormHandler()
   102  	if err != nil {
   103  		return nil, false, err
   104  	}
   105  	// ##  Handle the submitted form (validation etc)
   106  	form, err = formHandler.HandleSubmittedForm(ctx, r)
   107  	if err != nil {
   108  		return nil, false, err
   109  	}
   110  	billingAddressForm, ok := form.Data.(BillingAddressForm)
   111  	if !ok {
   112  		return form, false, errors.New("cannot convert to AddressForm ")
   113  	}
   114  	if !form.IsValidAndSubmitted() {
   115  		return form, false, nil
   116  	}
   117  	addressForm := AddressForm(billingAddressForm)
   118  	billingAddress := addressForm.MapToDomainAddress()
   119  
   120  	// update Billing
   121  	err = c.applicationCartService.UpdateBillingAddress(ctx, session, &billingAddress)
   122  	if err != nil {
   123  		c.logger.WithContext(ctx).Error("BillingAddressFormController UpdateBillingAddress Error %v", err)
   124  		return form, false, err
   125  	}
   126  	return form, true, nil
   127  }