Skip to content

Commit

Permalink
Creating locations will now accept an array of locations instead of o…
Browse files Browse the repository at this point in the history
…ne, allowing it to work
  • Loading branch information
Tim committed May 31, 2023
1 parent 7a76ed6 commit 69edd7e
Showing 1 changed file with 39 additions and 51 deletions.
90 changes: 39 additions & 51 deletions locations.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bigcommerce
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
Expand All @@ -19,60 +20,34 @@ type Location struct {
TimeZone string `json:"time_zone"`
Address LocationAddress `json:"address"`
StorefrontVisibility bool `json:"storefront_visibility"`
SpecialHours []LocationSpecialHours `json:"special_hours"`
SpecialHours []LocationSpecialHours `json:"special_hours,omitempty"`
}

type LocationAddress struct {
Address1 string `json:"address1"`
Address2 string `json:"address2"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Email string `json:"email"`
Phone string `json:"phone"`
GeoCoordinates struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
} `json:"geo_coordinates"`
CountryCode string `json:"country_code"`
Address1 string `json:"address1"`
Address2 string `json:"address2"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
Email string `json:"email"`
Phone string `json:"phone"`
GeoCoordinates Coordenates `json:"geo_coordinates"`
CountryCode string `json:"country_code"`
}

type Coordenates struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}

type LocationOpeningHours struct {
Sunday struct {
Open bool `json:"open"`
Opening string `json:"opening"`
Closing string `json:"closing"`
} `json:"sunday"`
Monday struct {
Open bool `json:"open"`
Opening string `json:"opening"`
Closing string `json:"closing"`
} `json:"monday"`
Tuesday struct {
Open bool `json:"open"`
Opening string `json:"opening"`
Closing string `json:"closing"`
} `json:"tuesday"`
Wednesday struct {
Open bool `json:"open"`
Opening string `json:"opening"`
Closing string `json:"closing"`
} `json:"wednesday"`
Thursday struct {
Open bool `json:"open"`
Opening string `json:"opening"`
Closing string `json:"closing"`
} `json:"thursday"`
Friday struct {
Open bool `json:"open"`
Opening string `json:"opening"`
Closing string `json:"closing"`
} `json:"friday"`
Saturday struct {
Open bool `json:"open"`
Opening string `json:"opening"`
Closing string `json:"closing"`
} `json:"saturday"`
Sunday Weekday `json:"sunday"`
Monday Weekday `json:"monday"`
Tuesday Weekday `json:"tuesday"`
Wednesday Weekday `json:"wednesday"`
Thursday Weekday `json:"thursday"`
Friday Weekday `json:"friday"`
Saturday Weekday `json:"saturday"`
}

type LocationSpecialHours struct {
Expand All @@ -85,6 +60,12 @@ type LocationSpecialHours struct {
Annual bool `json:"annual"`
}

type Weekday struct {
Open bool `json:"open"`
Opening string `json:"opening"`
Closing string `json:"closing"`
}

// GetLocations returns all locations using filters.
// filters: request query parameters for BigCommerce locations endpoint, for example {"is_active": true}
func (bc *Client) GetLocations(filters map[string]string) ([]Location, error) {
Expand All @@ -111,23 +92,30 @@ func (bc *Client) GetLocations(filters map[string]string) ([]Location, error) {

var locations []Location
err = json.Unmarshal(body, &locations)
print(string(body))
if err != nil {
return nil, err
}
return locations, nil
}

// CreateLocation creates a new location based on the Location struct
func (bc *Client) CreateLocation(location *Location) error {
// CreateLocations creates a new location based on the Location struct
func (bc *Client) CreateLocations(location *[]Location) error {
url := "/v3/inventory/locations"

reqJSON, _ := json.Marshal(location)

fmt.Println(string(reqJSON))
req := bc.getAPIRequest(http.MethodPost, url, bytes.NewReader(reqJSON))
_, err := bc.HTTPClient.Do(req)
res, err := bc.HTTPClient.Do(req)
if err != nil {
return err
}

if res.StatusCode != 200 {
return errors.New(res.Status)
}

return nil
}

Expand Down

0 comments on commit 69edd7e

Please sign in to comment.