Skip to content

Commit

Permalink
Add add contact function
Browse files Browse the repository at this point in the history
  • Loading branch information
gowizzard committed Jul 15, 2021
1 parent 398106e commit 50bbf55
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 5 deletions.
41 changes: 41 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
```
71 changes: 66 additions & 5 deletions contacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
Expand All @@ -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 {
Expand All @@ -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

}

0 comments on commit 50bbf55

Please sign in to comment.