Skip to content

Commit

Permalink
Sort coffee file (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
sven1103 authored Feb 9, 2025
1 parent 244fc14 commit 9b560a8
Showing 1 changed file with 79 additions and 75 deletions.
154 changes: 79 additions & 75 deletions internal/product/coffee.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,31 +65,6 @@ func (c *Coffee) SetCuppingScore(value int) error {
return nil
}

func newCvaProvided(id string, value int) CvaProvided {
return CvaProvided{ID: id, Value: value, OccurredOn: time.Now()}
}

// CvaProvided an event record about change in a coffee's Value assessment.
//
// In this case it is the CuppingScore, represented as a simple integer Value for the event.
type CvaProvided struct {
ID string
Value int
OccurredOn time.Time
}

func (e CvaProvided) AggregateID() string {
return e.ID
}

func (e CvaProvided) Type() string {
return "CvaProvided"
}

func (e CvaProvided) Occurred() time.Time {
return e.OccurredOn
}

// Details enables a containerised description with more detail of a Coffee.
type Details struct {
Origin string `json:"origin"` // The country the coffee has been produced
Expand All @@ -98,41 +73,6 @@ type Details struct {
Misc map[string]string `json:"misc"` // An unstructured collection of key:values to provide more details
}

func (c *Coffee) Details() Details {
return c.details
}

// UpdateDetails sets some more detailed information for the current Coffee.
func (c *Coffee) UpdateDetails(details Details) error {
e := NewDetailsUpdated(c.AggregateID, details)
if err := c.apply(e); err != nil {
return errors.Join(fmt.Errorf("could not update details for %s [id: %s]", c.Type, c.AggregateID), err)
}
return nil
}

type DetailsUpdated struct {
ID string
Details Details
OccurredOn time.Time
}

func (e DetailsUpdated) AggregateID() string {
return e.ID
}

func (e DetailsUpdated) Type() string {
return "DetailsUpdated"
}

func (e DetailsUpdated) Occurred() time.Time {
return e.OccurredOn
}

func NewDetailsUpdated(id string, details Details) DetailsUpdated {
return DetailsUpdated{ID: id, Details: details, OccurredOn: time.Now()}
}

// ChangePrice updates the price of the current Coffee.
//
// Only values greater or equal zero are allowed.
Expand All @@ -154,6 +94,34 @@ func (c *Coffee) Clear() {
c.events = []event.Event{}
}

func (c *Coffee) Details() Details {
return c.details
}

func NewCoffee(coffeeType string, price float64) (*Coffee, error) {
if coffeeType == "" {
return nil, errors.New("beverage type cannot be empty")
}
if price <= 0 {
return nil, errors.New("price must be greater than zero")
}
beverage := &Coffee{}
created := NewCoffeeCreated(uuid.NewString(), coffeeType, price)
if err := beverage.apply(*created); err != nil {
return nil, err
}
return beverage, nil
}

// UpdateDetails sets some more detailed information for the current Coffee.
func (c *Coffee) UpdateDetails(details Details) error {
e := NewDetailsUpdated(c.AggregateID, details)
if err := c.apply(e); err != nil {
return errors.Join(fmt.Errorf("could not update details for %s [id: %s]", c.Type, c.AggregateID), err)
}
return nil
}

// Load sets the state of the current coffee by applying all events iteratively.
//
// After all events have been applied to the account, the event cache is emptied.
Expand Down Expand Up @@ -224,28 +192,39 @@ func (c *Coffee) applyDetails(e DetailsUpdated) error {
return nil
}

/*
All coffee-related events
*/

type DetailsUpdated struct {
ID string
Details Details
OccurredOn time.Time
}

func (e DetailsUpdated) AggregateID() string {
return e.ID
}

func (e DetailsUpdated) Type() string {
return "DetailsUpdated"
}

func (e DetailsUpdated) Occurred() time.Time {
return e.OccurredOn
}

func NewDetailsUpdated(id string, details Details) DetailsUpdated {
return DetailsUpdated{ID: id, Details: details, OccurredOn: time.Now()}
}

type CoffeeCreated struct {
ID string
BeverageType string
Price float64
OccurredOn time.Time
}

func NewCoffee(coffeeType string, price float64) (*Coffee, error) {
if coffeeType == "" {
return nil, errors.New("beverage type cannot be empty")
}
if price <= 0 {
return nil, errors.New("price must be greater than zero")
}
beverage := &Coffee{}
created := NewCoffeeCreated(uuid.NewString(), coffeeType, price)
if err := beverage.apply(*created); err != nil {
return nil, err
}
return beverage, nil
}

func NewCoffeeCreated(id string, coffeeType string, price float64) *CoffeeCreated {
return &CoffeeCreated{id, coffeeType, price, time.Now()}
}
Expand Down Expand Up @@ -284,3 +263,28 @@ func (e PriceUpdated) Type() string {
func (e PriceUpdated) Occurred() time.Time {
return e.OccurredOn
}

func newCvaProvided(id string, value int) CvaProvided {
return CvaProvided{ID: id, Value: value, OccurredOn: time.Now()}
}

// CvaProvided an event record about change in a coffee's Value assessment.
//
// In this case it is the CuppingScore, represented as a simple integer Value for the event.
type CvaProvided struct {
ID string
Value int
OccurredOn time.Time
}

func (e CvaProvided) AggregateID() string {
return e.ID
}

func (e CvaProvided) Type() string {
return "CvaProvided"
}

func (e CvaProvided) Occurred() time.Time {
return e.OccurredOn
}

0 comments on commit 9b560a8

Please sign in to comment.