12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- package relation
- import (
- "fmt"
- "github.com/gshopify/service-wrapper/model"
- "github.com/mailru/dbr"
- "gshopper.com/gshopify/products/graphql/generated"
- "gshopper.com/gshopify/products/graphql/helper"
- "time"
- )
- type ProductVariant struct {
- Id string `db:"id"`
- ProductId string `db:"product_id"`
- InventoryItemId string `db:"inventory_item_id"`
- Price float64 `db:"price"`
- CompareAtPrice float64 `db:"compare_at_price"`
- Position int8 `db:"position"`
- Image dbr.NullString `db:"image"`
- InventoryManagement string `db:"inventory_management"`
- InventoryPolicy string `db:"inventory_policy"`
- Grams dbr.NullFloat64 `db:"grams"`
- Weight dbr.NullFloat64 `db:"weight"`
- WeightUnit string `db:"weight_unit"`
- CreatedAt time.Time `db:"created_at"`
- UpdatedAt time.Time `db:"updated_at"`
- PublishedAt *time.Time `db:"published_at"`
- DeletedAt *time.Time `db:"deleted_at"`
- Title string `db:"title"`
- Sku dbr.NullString `db:"sku"`
- Barcode dbr.NullString `db:"barcode"`
- RequiresShipping bool `db:"requires_shipping"`
- QuantityAvailable int `db:"available"`
- ForSale bool `db:"for_sale"`
- Options []struct {
- Field0 string
- Field1 int32
- } `db:"options"`
- }
- func (v *ProductVariant) As() *generated.ProductVariant {
- variant := generated.ProductVariant{
- Image: nil, //TODO:
- UnitPrice: nil, //TODO:
- UnitPriceMeasurement: nil, //TODO:
- ID: model.NewId(model.GidVariant, v.Id),
- Product: &generated.Product{ID: model.NewId(model.GidProduct, v.ProductId)},
- Price: helper.NewMoney(v.Price, generated.CurrencyCodeUsd),
- CompareAtPrice: helper.NewMoney(v.CompareAtPrice, generated.CurrencyCodeUsd),
- AvailableForSale: v.ForSale,
- CurrentlyNotInStock: v.QuantityAvailable <= 0,
- QuantityAvailable: &v.QuantityAvailable,
- RequiresShipping: v.RequiresShipping,
- Title: v.Title,
- WeightUnit: generated.WeightUnit(v.WeightUnit),
- }
- variant.Metafields = append(variant.Metafields, &generated.Metafield{
- Namespace: model.GidVariant.String(),
- Key: "position",
- Type: model.MetaFieldTypeNumberInteger.String(),
- Value: model.MetaFieldValue(model.MetaFieldTypeNumberInteger, v.Position),
- })
- for i := range v.Options {
- variant.Metafields = append(variant.Metafields, &generated.Metafield{
- Namespace: model.GidOption.String(),
- Key: model.NewId(model.GidOption, v.Options[i].Field0),
- Type: model.MetaFieldTypeNumberInteger.String(),
- Value: fmt.Sprintf("%d", v.Options[i].Field1),
- })
- }
- if v.Weight.Valid {
- s := v.Weight.Float64
- variant.Weight = &s
- }
- if v.Sku.Valid {
- s := v.Sku.String
- variant.Sku = &s
- }
- if v.Barcode.Valid {
- s := v.Barcode.String
- variant.Barcode = &s
- }
- return &variant
- }
|