Skip to content

Commit

Permalink
Merge pull request #7 from TerrexTech/inv-model-change
Browse files Browse the repository at this point in the history
Changed inventory model
  • Loading branch information
Jaskaranbir authored Nov 22, 2018
2 parents 18eae9e + c0c910d commit f24c98f
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 56 deletions.
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

129 changes: 74 additions & 55 deletions inventory/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,46 +16,50 @@ const AggregateID int8 = 2

// Inventory defines the Inventory Aggregate.
type Inventory struct {
ID objectid.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"`
ItemID uuuid.UUID `bson:"itemID,omitempty" json:"itemID,omitempty"`
DateArrived int64 `bson:"dateArrived,omitempty" json:"dateArrived,omitempty"`
DateSold int64 `bson:"dateSold,omitempty" json:"dateSold,omitempty"`
DeviceID uuuid.UUID `bson:"deviceID,omitempty" json:"deviceID,omitempty"`
DonateWeight float64 `bson:"donateWeight,omitempty" json:"donateWeight,omitempty"`
Lot string `bson:"lot,omitempty" json:"lot,omitempty"`
Name string `bson:"name,omitempty" json:"name,omitempty"`
Origin string `bson:"origin,omitempty" json:"origin,omitempty"`
Price float64 `bson:"price,omitempty" json:"price,omitempty"`
RSCustomerID uuuid.UUID `bson:"rsCustomerID,omitempty" json:"rsCustomerID,omitempty"`
SalePrice float64 `bson:"salePrice,omitempty" json:"salePrice,omitempty"`
SKU string `bson:"sku,omitempty" json:"sku,omitempty"`
SoldWeight float64 `bson:"soldWeight,omitempty" json:"soldWeight,omitempty"`
Timestamp int64 `bson:"timestamp,omitempty" json:"timestamp,omitempty"`
TotalWeight float64 `bson:"totalWeight,omitempty" json:"totalWeight,omitempty"`
UPC string `bson:"upc,omitempty" json:"upc,omitempty"`
WasteWeight float64 `bson:"wasteWeight,omitempty" json:"wasteWeight,omitempty"`
ID objectid.ObjectID `bson:"_id,omitempty" json:"_id,omitempty"`
ItemID uuuid.UUID `bson:"itemID,omitempty" json:"itemID,omitempty"`
DateArrived int64 `bson:"dateArrived,omitempty" json:"dateArrived,omitempty"`
DateSold int64 `bson:"dateSold,omitempty" json:"dateSold,omitempty"`
DeviceID uuuid.UUID `bson:"deviceID,omitempty" json:"deviceID,omitempty"`
DonateWeight float64 `bson:"donateWeight,omitempty" json:"donateWeight,omitempty"`
Lot string `bson:"lot,omitempty" json:"lot,omitempty"`
Name string `bson:"name,omitempty" json:"name,omitempty"`
Origin string `bson:"origin,omitempty" json:"origin,omitempty"`
Price float64 `bson:"price,omitempty" json:"price,omitempty"`
RSCustomerID uuuid.UUID `bson:"rsCustomerID,omitempty" json:"rsCustomerID,omitempty"`
FlashSaleWeight float64 `bson:"flashSaleWeight,omitempty" json:"flashSaleWeight,omitempty"`
SKU string `bson:"sku,omitempty" json:"sku,omitempty"`
SoldWeight float64 `bson:"soldWeight,omitempty" json:"soldWeight,omitempty"`
Timestamp int64 `bson:"timestamp,omitempty" json:"timestamp,omitempty"`
TotalWeight float64 `bson:"totalWeight,omitempty" json:"totalWeight,omitempty"`
UPC string `bson:"upc,omitempty" json:"upc,omitempty"`
WasteWeight float64 `bson:"wasteWeight,omitempty" json:"wasteWeight,omitempty"`
OnFlashSale bool `bson:"onFlashSale,omitempty" json:"onFlashSale,omitempty"`
FlashSaleTimestamp int64 `bson:"flashSaleTimestamp,omitempty" json:"flashSaleTimestamp,omitempty"`
}

// MarshalBSON returns bytes of BSON-type.
func (i Inventory) MarshalBSON() ([]byte, error) {
in := map[string]interface{}{
"itemID": i.ItemID.String(),
"dateArrived": i.DateArrived,
"dateSold": i.DateSold,
"deviceID": i.DeviceID.String(),
"donateWeight": i.DonateWeight,
"lot": i.Lot,
"name": i.Name,
"origin": i.Origin,
"price": i.Price,
"rsCustomerID": i.RSCustomerID.String(),
"salePrice": i.SalePrice,
"sku": i.SKU,
"soldWeight": i.SoldWeight,
"timestamp": i.Timestamp,
"totalWeight": i.TotalWeight,
"upc": i.UPC,
"wasteWeight": i.WasteWeight,
"itemID": i.ItemID.String(),
"dateArrived": i.DateArrived,
"dateSold": i.DateSold,
"deviceID": i.DeviceID.String(),
"donateWeight": i.DonateWeight,
"lot": i.Lot,
"name": i.Name,
"origin": i.Origin,
"onFlashSale": i.OnFlashSale,
"price": i.Price,
"rsCustomerID": i.RSCustomerID.String(),
"flashSaleWeight": i.FlashSaleWeight,
"sku": i.SKU,
"soldWeight": i.SoldWeight,
"timestamp": i.Timestamp,
"totalWeight": i.TotalWeight,
"upc": i.UPC,
"wasteWeight": i.WasteWeight,
"flashSaleTimestamp": i.FlashSaleTimestamp,
}

if i.ID != objectid.NilObjectID {
Expand All @@ -67,23 +71,25 @@ func (i Inventory) MarshalBSON() ([]byte, error) {
// MarshalJSON returns bytes of JSON-type.
func (i *Inventory) MarshalJSON() ([]byte, error) {
in := map[string]interface{}{
"itemID": i.ItemID.String(),
"dateArrived": i.DateArrived,
"dateSold": i.DateSold,
"deviceID": i.DeviceID.String(),
"donateWeight": i.DonateWeight,
"lot": i.Lot,
"name": i.Name,
"origin": i.Origin,
"price": i.Price,
"rsCustomerID": i.RSCustomerID.String(),
"salePrice": i.SalePrice,
"sku": i.SKU,
"soldWeight": i.SoldWeight,
"timestamp": i.Timestamp,
"totalWeight": i.TotalWeight,
"upc": i.UPC,
"wasteWeight": i.WasteWeight,
"itemID": i.ItemID.String(),
"dateArrived": i.DateArrived,
"dateSold": i.DateSold,
"deviceID": i.DeviceID.String(),
"donateWeight": i.DonateWeight,
"lot": i.Lot,
"name": i.Name,
"origin": i.Origin,
"onFlashSale": i.OnFlashSale,
"price": i.Price,
"rsCustomerID": i.RSCustomerID.String(),
"flashSaleWeight": i.FlashSaleWeight,
"sku": i.SKU,
"soldWeight": i.SoldWeight,
"timestamp": i.Timestamp,
"totalWeight": i.TotalWeight,
"upc": i.UPC,
"wasteWeight": i.WasteWeight,
"flashSaleTimestamp": i.FlashSaleTimestamp,
}

if i.ID != objectid.NilObjectID {
Expand Down Expand Up @@ -205,10 +211,10 @@ func (i *Inventory) unmarshalFromMap(m map[string]interface{}) error {
return err
}
}
if m["salePrice"] != nil {
i.SalePrice, err = util.AssertFloat64(m["salePrice"])
if m["flashSaleWeight"] != nil {
i.FlashSaleWeight, err = util.AssertFloat64(m["flashSaleWeight"])
if err != nil {
err = errors.Wrap(err, "Error while asserting SalePrice")
err = errors.Wrap(err, "Error while asserting FlashSaleWeight")
return err
}
}
Expand Down Expand Up @@ -245,13 +251,26 @@ func (i *Inventory) unmarshalFromMap(m map[string]interface{}) error {
return errors.New("Error while asserting UPC")
}
}
if m["onFlashSale"] != nil {
i.OnFlashSale, assertOK = m["onFlashSale"].(bool)
if !assertOK {
return errors.New("Error while asserting OnFlashSale")
}
}
if m["wasteWeight"] != nil {
i.WasteWeight, err = util.AssertFloat64(m["wasteWeight"])
if err != nil {
err = errors.Wrap(err, "Error while asserting WasteWeight")
return err
}
}
if m["flashSaleTimestamp"] != nil {
i.FlashSaleTimestamp, err = util.AssertInt64(m["flashSaleTimestamp"])
if err != nil {
err = errors.Wrap(err, "Error while asserting FlashSaleTimestamp")
return err
}
}

return nil
}
2 changes: 1 addition & 1 deletion test/inventory_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ var _ = Describe("InventoryAggregate", func() {
Origin: "test-origin",
Price: 13.4,
RSCustomerID: rsCustomerID,
SalePrice: 12.23,
// SalePrice: 12.23,
SKU: "test-sku",
Timestamp: time.Now().Unix(),
TotalWeight: 300,
Expand Down

0 comments on commit f24c98f

Please sign in to comment.