flamingo.me/flamingo-commerce/v3@v3.11.0/category/domain/category.go (about)

     1  package domain
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  )
     7  
     8  type (
     9  	// Category domain model
    10  	Category interface {
    11  		Code() string
    12  		Name() string
    13  		Path() string
    14  		Promoted() bool
    15  		Active() bool
    16  		CategoryType() string
    17  		Media() Medias
    18  		Attributes() Attributes
    19  	}
    20  
    21  	// CategoryData defines the default domain category data model
    22  	CategoryData struct {
    23  		CategoryCode       string
    24  		CategoryName       string
    25  		CategoryPath       string
    26  		IsPromoted         bool
    27  		IsActive           bool
    28  		CategoryMedia      Medias
    29  		CategoryTypeCode   string
    30  		CategoryAttributes Attributes
    31  		Promotion          Promotion
    32  	}
    33  
    34  	// Attributes define additional category attributes
    35  	Attributes map[string]Attribute //@name CategoryAttributes
    36  
    37  	// Attribute instance representation
    38  	Attribute struct {
    39  		Code   string
    40  		Label  string
    41  		Values []AttributeValue
    42  	} //@name CategoryAttribute
    43  
    44  	//AttributeValue represents the value that a Attribute can have
    45  	AttributeValue struct {
    46  		Label    string
    47  		RawValue interface{}
    48  	}
    49  
    50  	// Promotion defines promotion for a category
    51  	Promotion struct {
    52  		LinkType   string
    53  		LinkTarget string
    54  		Media      Medias
    55  	}
    56  )
    57  
    58  // Category Types
    59  const (
    60  	TypeProduct   = "product"
    61  	TypeTeaser    = "teaser"
    62  	TypePromotion = "promotion"
    63  )
    64  
    65  var _ Category = (*CategoryData)(nil)
    66  
    67  // Code gets the category code
    68  func (c CategoryData) Code() string {
    69  	return c.CategoryCode
    70  }
    71  
    72  // Media gets the category media
    73  func (c CategoryData) Media() Medias {
    74  	return c.CategoryMedia
    75  }
    76  
    77  // Name gets the category name
    78  func (c CategoryData) Name() string {
    79  	return c.CategoryName
    80  }
    81  
    82  // Path gets the category path
    83  func (c CategoryData) Path() string {
    84  	return c.CategoryPath
    85  }
    86  
    87  // Promoted gets the category promoted state
    88  func (c CategoryData) Promoted() bool {
    89  	return c.IsPromoted
    90  }
    91  
    92  // Active indicator
    93  func (c CategoryData) Active() bool {
    94  	return c.IsActive
    95  }
    96  
    97  // CategoryType gets the category type code
    98  func (c CategoryData) CategoryType() string {
    99  	return c.CategoryTypeCode
   100  }
   101  
   102  // Attributes gets the additional category attributes
   103  func (c CategoryData) Attributes() Attributes {
   104  	return c.CategoryAttributes
   105  }
   106  
   107  // Get by key
   108  func (a Attributes) Get(code string) *Attribute {
   109  	if att, ok := a[code]; ok {
   110  		return &att
   111  	}
   112  	return nil
   113  }
   114  
   115  // Has by key
   116  func (a Attributes) Has(code string) bool {
   117  	if _, ok := a[code]; ok {
   118  		return true
   119  	}
   120  	return false
   121  }
   122  
   123  // All returns all Attributes as slice
   124  func (a Attributes) All() []Attribute {
   125  	var att []Attribute
   126  	for _, v := range a {
   127  		att = append(att, v)
   128  	}
   129  	return att
   130  }
   131  
   132  // ToString returns a concatenated string of all the values of an attribute
   133  func (a Attribute) ToString() string {
   134  	var attValue []string
   135  	for _, v := range a.Values {
   136  		attValue = append(attValue, v.Value())
   137  	}
   138  	return strings.Join(attValue, ",")
   139  }
   140  
   141  // Value returns string representation of the RawValue
   142  func (av AttributeValue) Value() string {
   143  	if stringer, ok := av.RawValue.(fmt.Stringer); ok {
   144  		return stringer.String()
   145  	}
   146  	string, _ := av.RawValue.(string)
   147  	return string
   148  }