diff --git a/README.md b/README.md index 2e276b4..998bbc1 100644 --- a/README.md +++ b/README.md @@ -41,4 +41,45 @@ if err != nil { } else { fmt.Println(contacts) } +``` + +### Add a contact + +If a new contact is to be created, then this can be done as follows. The fields: id, user_id, updated_at & created_at can be ignored. + +```go +// Define body +body := &ContactBody{ + 0, + 0, + nil, + "first_name", + "last_name", + "email", + "email_work", + "company", + "address", + "address_work", + "phone_work", + "mobile_work", + "phone", + "mobile", + "fax", + "fax_work", + "facebook_url", + "linkedin_url", + "xing_url", + "twitter_account", + false, + "", + "", +} + +// Add a new contact +contact, err := AddContact(body, "token") +if err != nil { + fmt.Println(err) +} else { + fmt.Println(contact) +} ``` \ No newline at end of file diff --git a/contacts.go b/contacts.go index e05bea2..22b310e 100644 --- a/contacts.go +++ b/contacts.go @@ -16,8 +16,35 @@ import ( "fmt" ) -// ContactsReturn is to decode the json return -type ContactsReturn struct { +// ContactBody is to build a new contact +type ContactBody struct { + Id int `json:"id,omitempty"` + UserId int `json:"user_id,omitempty"` + Speeddail interface{} `json:"speeddail"` + FirstName string `json:"first_name"` + LastName string `json:"last_name"` + Email string `json:"email"` + EmailWork string `json:"email_work"` + Company string `json:"company"` + Address string `json:"address"` + AddressWork string `json:"address_work"` + PhoneWork string `json:"phone_work"` + MobileWork string `json:"mobile_work"` + Phone string `json:"phone"` + Mobile string `json:"mobile"` + Fax string `json:"fax"` + FaxWork string `json:"fax_work"` + FacebookUrl string `json:"facebook_url"` + LinkedinUrl string `json:"linkedin_url"` + XingUrl string `json:"xing_url"` + TwitterAccount string `json:"twitter_account"` + Blocked bool `json:"blocked"` + UpdatedAt string `json:"updated_at,omitempty"` + CreatedAt string `json:"created_at,omitempty"` +} + +// ContactReturn is to decode the json return +type ContactReturn struct { Id int `json:"id"` UserId int `json:"user_id"` Speeddail interface{} `json:"speeddail"` @@ -44,10 +71,10 @@ type ContactsReturn struct { } // Contacts is to get all contacts from the api -func Contacts(token string) (*[]ContactsReturn, error) { +func Contacts(token string) (*[]ContactReturn, error) { // To decode the json data - var contacts []ContactsReturn + var contacts []ContactReturn // To call the pages page := 1 @@ -65,7 +92,7 @@ func Contacts(token string) (*[]ContactsReturn, error) { } // Decode data - var decode []ContactsReturn + var decode []ContactReturn err = json.NewDecoder(response.Body).Decode(&decode) if err != nil { @@ -91,3 +118,37 @@ func Contacts(token string) (*[]ContactsReturn, error) { return &contacts, nil } + +// AddContact is to add a new contact +func AddContact(body *ContactBody, token string) (*ContactReturn, error) { + + // Convert data + convert, err := json.Marshal(body) + if err != nil { + return nil, err + } + + // Set config for new request + r := Request{"/contacts", "POST", token, convert} + + // Send new request + response, err := r.Send() + if err != nil { + return nil, err + } + + // Close response body after function ends + defer response.Body.Close() + + // Decode data + var decode ContactReturn + + err = json.NewDecoder(response.Body).Decode(&decode) + if err != nil { + return nil, err + } + + // Return data + return &decode, nil + +}