// Edit an account for a given shop. func EditAccount(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop, token string) string { var c cart.Customer defer req.Body.Close() data, err := ioutil.ReadAll(req.Body) if err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } if err = json.Unmarshal(data, &c); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } c.ShopId = shop.Id c.Id, err = cart.IdentifierFromToken(token) if err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } if err = c.Update(); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } return encoding.Must(enc.Encode(c)) }
// Edit an existing customer for a // given shop. func EditCustomer(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string { var c cart.Customer defer req.Body.Close() data, err := ioutil.ReadAll(req.Body) if err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } if err = json.Unmarshal(data, &c); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } customerId := params["id"] if !bson.IsObjectIdHex(customerId) { apierror.GenerateError("invalid customer reference", nil, w, req) return "" } c.Id = bson.ObjectIdHex(customerId) c.ShopId = shop.Id if err = c.Update(); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } return encoding.Must(enc.Encode(c)) }
func DeleteAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string { var c cart.Customer customerId := params["id"] if !bson.IsObjectIdHex(customerId) { apierror.GenerateError("invalid customer reference", nil, w, req) return "" } c.Id = bson.ObjectIdHex(customerId) c.ShopId = shop.Id if err := c.Get(); err != nil { apierror.GenerateError("", err, w, req) return "" } addressId := params["address"] if !bson.IsObjectIdHex(addressId) { apierror.GenerateError("invalid address reference", nil, w, req) return "" } // Make sure this isn't the default address if c.DefaultAddress != nil && c.DefaultAddress.Id.Hex() == addressId { apierror.GenerateError("removing a default address is not allowed", nil, w, req) return "" } found := false for i, addr := range c.Addresses { if addr.Id.Hex() == addressId { c.Addresses = append(c.Addresses[:i], c.Addresses[i+1:]...) found = true break } } if !found { apierror.GenerateError("invalid address reference", nil, w, req) return "" } if err := c.Update(); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } return "" }
func AddAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string { var address cart.CustomerAddress var c cart.Customer customerId := params["id"] if !bson.IsObjectIdHex(customerId) { apierror.GenerateError("invalid customer reference", nil, w, req) return "" } c.Id = bson.ObjectIdHex(customerId) c.ShopId = shop.Id if err := c.Get(); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } defer req.Body.Close() data, err := ioutil.ReadAll(req.Body) if err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } if err = json.Unmarshal(data, &address); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } // audit the address if err := address.Validate(); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } addressId := bson.NewObjectId() address.Id = &addressId c.Addresses = append(c.Addresses, address) if err = c.Update(); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } return encoding.Must(enc.Encode(address)) }
func SetDefaultAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string { var c cart.Customer customerId := params["id"] if !bson.IsObjectIdHex(customerId) { apierror.GenerateError("invalid customer reference", nil, w, req) return "" } c.Id = bson.ObjectIdHex(customerId) c.ShopId = shop.Id if err := c.Get(); err != nil { apierror.GenerateError("", err, w, req) return "" } addressId := params["address"] if !bson.IsObjectIdHex(addressId) { apierror.GenerateError("invalid address reference", nil, w, req) return "" } found := false for _, addr := range c.Addresses { if addr.Id.Hex() == addressId { c.DefaultAddress = &addr found = true break } } if !found { apierror.GenerateError("invalid address reference", nil, w, req) return "" } if err := c.Update(); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } return encoding.Must(enc.Encode(c)) }
func EditAddress(w http.ResponseWriter, req *http.Request, params martini.Params, enc encoding.Encoder, shop *cart.Shop) string { var address cart.CustomerAddress var c cart.Customer customerId := params["id"] if !bson.IsObjectIdHex(customerId) { apierror.GenerateError("invalid customer reference", nil, w, req) return "" } c.Id = bson.ObjectIdHex(customerId) c.ShopId = shop.Id if err := c.Get(); err != nil { apierror.GenerateError("", err, w, req) return "" } addressId := params["address"] if !bson.IsObjectIdHex(addressId) { apierror.GenerateError("invalid address reference", nil, w, req) return "" } defer req.Body.Close() data, err := ioutil.ReadAll(req.Body) if err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } if err = json.Unmarshal(data, &address); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } // audit the address if err := address.Validate(); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } found := false for i, addr := range c.Addresses { if addr.Id.Hex() == addressId { c.Addresses[i] = address found = true break } } if !found { apierror.GenerateError("invalid address reference", nil, w, req) return "" } if err = c.Update(); err != nil { apierror.GenerateError(err.Error(), err, w, req) return "" } return encoding.Must(enc.Encode(address)) }