/*
Save the resource on the server. If there is an Id present, send a PATCH
request, otherwise send a POST request. The Attributes and relationships that
will be sent are the ones in the 'fields' argument. If the 'fields' argument is
nil, everything will be saved.
*/
Save the resource on the server. If there is an Id present, send a PATCH
request, otherwise send a POST request. The Attributes and relationships that
will be sent are the ones in the 'fields' argument. If the 'fields' argument is
nil, everything will be saved.
/ * Save the resource on the server . If there is an Id present send a PATCH request otherwise send a POST request . The Attributes and relationships that will be sent are the ones in the ' fields ' argument . If the ' fields ' argument is nil everything will be saved . * /
func (r *Resource) Save(fields []string) error {
	if len(fields) == 0 {
		keys := make([]string, 0,
			len(r.Attributes)+len(r.Relationships))
		for key := range r.Attributes {
			keys = append(keys, key)
		}
		for key := range r.Relationships {
			keys = append(keys, key)
		}
		return r.Save(keys)
	}
	var method, url string
	if r.Id != "" {
		method = "PATCH"
		url = fmt.Sprintf("/%s/%s", r.Type, r.Id)
	} else {
		method = "POST"
		url = fmt.Sprintf("/%s", r.Type)
	}
	payload := PayloadSingular{}
	payload.Data.Type = r.Type
	payload.Data.Id = r.Id
	for _, field := range fields {
		attribute, attributeExists := r.Attributes[field]
		relationship, relationshipsExists := r.Relationships[field]
		if attributeExists {
			if payload.Data.Attributes == nil {
				payload.Data.Attributes = make(map[string]interface{})
			}
			payload.Data.Attributes[field] = attribute
		} else if relationshipsExists && relationship.Type == SINGULAR {
			if payload.Data.Relationships == nil {
				payload.Data.Relationships = make(map[string]interface{})
			}
			payload.Data.Relationships[field] = PayloadRelationshipSingularWrite{
				Data: ResourceIdentifier{
					Type: relationship.DataSingular.Type,
					Id:   relationship.DataSingular.Id,
				},
			}
		} else {
			return fmt.Errorf("field %s is invalid", field)
		}
	}
	body, err := json.Marshal(payload)
	if err != nil {
		return err
	}
	body, err = r.API.request(method, url, body, "")
	if err != nil {
		return err
	}
	err = r.overwrite(body)
	if err != nil {
		return err
	}
	return nil
}
func ( r * Resource ) Save ( fields [ ] string ) error { if len ( fields ) == 0 { keys := make ( [ ] string , 0 , len ( r . Attributes ) + len ( r . Relationships ) ) for key := range r . Attributes { keys = append ( keys , key ) } for key := range r . Relationships { keys = append ( keys , key ) } return r . Save ( keys ) } var method , url string if r . Id != " " { method = " " url = fmt . Sprintf ( " " , r . Type , r . Id ) } else { method = " " url = fmt . Sprintf ( " " , r . Type ) } payload := PayloadSingular { } payload . Data . Type = r . Type payload . Data . Id = r . Id for _ , field := range fields { attribute , attributeExists := r . Attributes [ field ] relationship , relationshipsExists := r . Relationships [ field ] if attributeExists { if payload . Data . Attributes == nil { payload . Data . Attributes = make ( map [ string ] interface { } ) } payload . Data . Attributes [ field ] = attribute } else if relationshipsExists && relationship . Type == SINGULAR { if payload . Data . Relationships == nil { payload . Data . Relationships = make ( map [ string ] interface { } ) } payload . Data . Relationships [ field ] = PayloadRelationshipSingularWrite { Data : ResourceIdentifier { Type : relationship . DataSingular . Type , Id : relationship . DataSingular . Id , } , } } else { return fmt . Errorf ( " " , field ) } } body , err := json . Marshal ( payload ) if err != nil { return err } body , err = r . API . request ( method , url , body , " " ) if err != nil { return err } err = r . overwrite ( body ) if err != nil { return err } return nil }
Save the resource on the server.
Save the resource on the server .